Forum Discussion
Create a custom column that evaluates data in 3 different columns and returns the common value
- 2 years ago
Hi Anonymous ,
It's better to do this transformation in Power Query and find the common values form your three columns. Here's my M code :
let
Source = Excel.Workbook(File.Contents("C:\Users\aliom\OneDrive\Power BI Samples\Extract Commom String\Sample.xlsx"), null, true),
Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Job Title", type text}, {"Shift_Code_S", type text}, {"Shift_Code_O", type text}, {"Shift_Code_D", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Raw_Shift Code (Desired Result)"}),
// Custom function to find the common string prefix
FindCommonPrefix = (s1 as text, s2 as text, s3 as text) as text =>
let
len = List.Min({Text.Length(s1), Text.Length(s2), Text.Length(s3)}),
commonPrefixList = List.Select({0..len - 1}, each Text.Start(s1, _ + 1) = Text.Start(s2, _ + 1) and Text.Start(s1, _ + 1) = Text.Start(s3, _ + 1)),
commonPrefix = if List.IsEmpty(commonPrefixList) then "" else Text.Start(s1, List.Max(List.Transform(commonPrefixList, each _ + 1)))
in
commonPrefix,
// Add a Custom Column using the custom function
#"Added CommonCode" = Table.AddColumn(#"Removed Columns", "CommonCode", each FindCommonPrefix([Shift_Code_S], [Shift_Code_O], [Shift_Code_D]), type text)
in
#"Added CommonCode"You can download my test files form this link:
https://1drv.ms/f/s!Aq3n-sopiGyqgolf8Zmm9dD2akM7wg?e=t2a9L4
If I answered your question, please mark this thread as accepted and Thums Up!
Follow me on LinkedIn:
https://www.linkedin.com/in/mustafa-ali-70133451/
Hi Anonymous ,
It's better to do this transformation in Power Query and find the common values form your three columns. Here's my M code :
let
Source = Excel.Workbook(File.Contents("C:\Users\aliom\OneDrive\Power BI Samples\Extract Commom String\Sample.xlsx"), null, true),
Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Job Title", type text}, {"Shift_Code_S", type text}, {"Shift_Code_O", type text}, {"Shift_Code_D", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Raw_Shift Code (Desired Result)"}),
// Custom function to find the common string prefix
FindCommonPrefix = (s1 as text, s2 as text, s3 as text) as text =>
let
len = List.Min({Text.Length(s1), Text.Length(s2), Text.Length(s3)}),
commonPrefixList = List.Select({0..len - 1}, each Text.Start(s1, _ + 1) = Text.Start(s2, _ + 1) and Text.Start(s1, _ + 1) = Text.Start(s3, _ + 1)),
commonPrefix = if List.IsEmpty(commonPrefixList) then "" else Text.Start(s1, List.Max(List.Transform(commonPrefixList, each _ + 1)))
in
commonPrefix,
// Add a Custom Column using the custom function
#"Added CommonCode" = Table.AddColumn(#"Removed Columns", "CommonCode", each FindCommonPrefix([Shift_Code_S], [Shift_Code_O], [Shift_Code_D]), type text)
in
#"Added CommonCode"
You can download my test files form this link:
https://1drv.ms/f/s!Aq3n-sopiGyqgolf8Zmm9dD2akM7wg?e=t2a9L4
If I answered your question, please mark this thread as accepted and Thums Up!
Follow me on LinkedIn:
https://www.linkedin.com/in/mustafa-ali-70133451/
- Anonymous2 years agoNot applicable
amustafa Thank you! I've been working on this for the past few days, I should have posted sooner, your solution worked great! Thanks so much!
- Anonymous2 years agoNot applicable
amustafa Hoping you can help again. I have received additional requirements which I built out in separate queries but the queries are not merging properly. Ultimately what I need to do after adding the custom column for Common Code is split shift code column based on the value in the Raw_Shift Code amd then add rows for each item instead of having in columns. Please see current table and desired tables below.
Current Table
Job Title Shift_Code_S Shift_Code_S_Price Shift_Code_O Shift_Code_O_Price Shift_Code_D Shift_Code_D_Price Raw_Shift Code (Desired new column) Location Vendor Waiter LCFNAPS 15.57 LCFNAPO 22.28 LCFNAPD 32.99 LCFNAP Dallas Company A Server 8147R 15.81 8147RO 22.62 8147RP 33.44 8147R Houston Company B Janitor FOHG24AFTLS 12.65 FOHG24AFTLH 23.65 FOHG24AFTLP 34.65 FOHG24AFTL San Francisco Company C Janitor FOHG24AFTLSN 13.22 FOHG24AFTLHN 24.22 FOHG24AFTLPN 35.22 FOHG24AFTL San Francisco Company C Desired Table
Job Title Original_Shift_Code Raw_Shift_Code (new) Shift_Type (new) Price Location Vendor Waiter LCFNAPS LCFNAP S 15.57 Dallas Company A Waiter LCFNAPO LCFNAP O 22.28 Dallas Company A Waiter LCFNAPD LCFNAP D 32.99 Dallas Company A Server 8147RS 8147R S 15.81 Houston Company B Server 8147RO 8147R O 22.62 Houston Company B Server 8147RP 8147R P 33.44 Houston Company B Janitor FOHG24AFTLS FOHG24AFTL S 12.65 San Francisco Company C Janitor FOHG24AFTLH FOHG24AFTL H 23.65 San Francisco Company C Janitor FOHG24AFTLP FOHG24AFTL P 34.65 San Francisco Company C Janitor FOHG24AFTLSN FOHG24AFTL SN 13.22 San Francisco Company C Janitor FOHG24AFTLHN FOHG24AFTL HN 24.22 San Francisco Company C Janitor FOHG24AFTLPN FOHG24AFTL PN 35.22 San Francisco Company C