Search code examples
sqldatabasereportrelational-databasepowerbi

SQL : how do I populate a custom table/column based on the value in a different column


Sorry in advance if this is a dumb question. I am not very skilled in SQL/Power BI.

I have a table that I am working with which has a column Job Names.

I want to populate either a custom table/column (I think a separate table might be best for my needs but any progress in the right direction is extremely welcomed) based on a substring found within this column.

The column will always contain 1 of 4 potential numbers but the number can be located in various spots within the name and there is a chance that another number found in the job name might be representing one of the numbers unintentionally.

Here is an example:

enter image description here

I have highlighted in red the numeric string which is important and added a few example of how it could be duplicated unintentionally in the job name and the associated desired output value. I feel like maybe a case statement here would be the best route but as I mentioned before, I am not very skilled in this area.

The reason for this is I have a datastream I am trying to use in Power BI, but I want to create a page filter based on these custom values and since I have no column which contains just these specific numbers I am unable to create a slicer visualization to filter the data shown on the report.

If any clarification is needed just let me know I am happy to try and rephrase anything or be more specific where needed :)


Solution

  • In the power query window, select the job name column, and then add add a letter on the front and back of the text. Transform Tab > Text Column Group > Format > Add Prefix/Add Suffix Then use split column (also in the Text Column Group) to split Digit to Non-Digit and then Non-Digit to Digit. Then delete the extra columns and there you are! Here is the M code:

    #"Added Prefix" = Table.TransformColumns(#"Changed Type", {{"JOB_NAME", each "x" & _, type text}}),
    #"Added Suffix" = Table.TransformColumns(#"Added Prefix", {{"JOB_NAME", each _ & "x", type text}}),
    #"Split Column by Character Transition" = Table.SplitColumn(#"Added Suffix", "JOB_NAME", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"JOB_NAME.1", "JOB_NAME.2"}),
    #"Split Column by Character Transition1" = Table.SplitColumn(#"Split Column by Character Transition", "JOB_NAME.2", Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c)), {"JOB_NAME", "JOB_NAME.2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Character Transition1",{"JOB_NAME.1", "JOB_NAME.2"})