Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Anonymous
Not applicable

Transform multiple columns based on values in another column

Hello,

 

I am trying to group, split, or somehow transform multiple columns (Years 2020-2023) based on values in another column (Source) for visualization in Power BI. The original data comes from Excel. How can I do the following?

 

This is the raw data I have right now, where I have multiple orders per customer, and each order being estimated based on Source A, B, and C. Specifically, I have 3 orders from Customer 1, and rows 2-4 are about the 1st order. In these 3 rows, values are the same for columns aaa, bbb, ddd, eee, fff, and ggg, so it is essentially a repetition. I have numbers in columns 2020-2023, but I don't want to view Source horizontally. Instead, I would like to incorporate Source into the Year columns

 

Screenshot 2024-07-08 123335.png

 

This is the end product I want. Each order is only 1 row, and Source is incorporated into the Year columns such that I have 2020A, 2020B, 2020C, 2021A... and so on.

Screenshot 2024-07-08 123823.png

 

Really appreciate your help. Thanks!

7 REPLIES 7
Anonymous
Not applicable

** Update **

 

Raw Excel data I have currently:

aaabbbCustomerdddeeefffgggSource2020202120222023
a1b1Customer 1d1e1f1g1Source A    
a1b1Customer 1d1e1f1g1Source B    
a1b1Customer 1d1e1f1g1Source C    
a2b2Customer 1d2e2f2g2Source A    
a2b2Customer 1d2e2f2g2Source B    
a2b2Customer 1d2e2f2g2Source C    
a3b3Customer 1d3e3f3g3Source A    
a3b3Customer 1d3e3f3g3Source B    
a3b3Customer 1d3e3f3g3Source C    
a4b4Customer 2d4e4f4g4Source A    
a4b4Customer 2d4e4f4g4Source B    
a4b4Customer 2d4e4f4g4Source C    
a5b5Customer 2d5e5f5g5Source A    
a5b5Customer 2d5e5f5g5Source B    
a5b5Customer 2d5e5f5g5Source C    
a6b6Customer 2d6e6f6g6Source A    
a6b6Customer 2d6e6f6g6Source B    
a6b6Customer 2d6e6f6g6Source C    
a7b7Customer 2d7e7f7g7Source A    
a7b7Customer 2d7e7f7g7Source B    
a7b7Customer 2d7e7f7g7Source C    
a8b8Customer 3d8e8f8g8Source A    
a8b8Customer 3d8e8f8g8Source B    
a8b8Customer 3d8e8f8g8Source C    

 

And this is the end result I am looking for:

aaabbbCustomerdddeeefffggg2020 A2020 B2020 C2021 A2021 B2021 C2022 A2022 B2022 C2023 A2023 B2023 C
a1b1Customer 1d1e1f1g1            
a2b2Customer 1d2e2f2g2            
a3b3Customer 1d3e3f3g3            
a4b4Customer 2d4e4f4g4            
a5b5Customer 2d5e5f5g5            
a6b6Customer 2d6e6f6g6            
a7b7Customer 2d7e7f7g7            
a8b8Customer 3d8e8f8g8           

 

 

 

 

Thank you!

hi @Anonymous ,

 

Create a blank query , copy and paste the below code into the advanced editor.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rdPNCoJQEIbhWxHXbvJ/W15CS3FROXNWIVjef72rpDMcmAjkQ2fxwLtwHPPLIS/yKzNsj+dylzXjY2aEUSYw52Vbb5Id36+fZyp+MU5/MIbYKDHKyOAijDKhTLa4DaPFbRgtFUYVGVyEUSZUyRa3YbS4DaOlxqj3Bu0zF2GUCXWyxW0YLW7DaGkwmsjgIowyoUm2uA2jxW0YLS1GGxlchFEmtMkWt2G0uA2jpcPoIoOLMMqELtniNowWt2G09Bj93uD/mrkIo0zoky1uw2hxG18t0ws=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [aaa = _t, bbb = _t, Customer = _t, ddd = _t, eee = _t, fff = _t, ggg = _t, Source = _t, #"2020" = _t, #"2021" = _t, #"2022" = _t, #"2023" = _t]),
    Columns_not_to_Unpivot = Table.AddIndexColumn ( Table.FromList ( List.FirstN ( Table.ColumnNames( Source) , 8))  , "Index" ,1,1),
    #"Removed Other Columns" = Table.SelectColumns(Columns_not_to_Unpivot,{"Column1"}),
    Columns_not_to_Unpivot_List = #"Removed Other Columns"[Column1],
    Refer_to_Source = Source,
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Refer_to_Source, Columns_not_to_Unpivot_List, "Year", "Value"),
    #"Merged Source and Year" = Table.CombineColumns(#"Unpivoted Other Columns",{"Source", "Year"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged")
in
    #"Merged Source and Year"

 

Output: 

 

adudani_0-1720468929580.png

 

 

The last step would be the structure the Merge and Value column as needed etc.

 

However, I recommend you load it as is in Power query and use a matrix visual instead for the same.

 

If the goal is to create a pivot table using Power query or similar , refer to Subtotal and Column Total in Power Query - YouTube 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash
Anonymous
Not applicable

Thank you! I would need to use a table instead of matrix for the visualization. 

 

Based on the output you provided, how can I merge the rows in the same order (ex: the a1s, the a2s) into one row, with the Merged column expanded as multiple columns?

 

This is what I'm referring to:

aaabbbCustomerdddeeefffA2020A2021A2022A2023B2020B2021B2022B2023...
a1 Customer 1            
a2 Customer 1            

 

paste the following codes into 2 different blank queries

query1

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rdPNCoJQEIbhWxHXbvJ/W15CS3FROXNWIVjef72rpDMcmAjkQ2fxwLtwHPPLIS/yKzNsj+dylzXjY2aEUSYw52Vbb5Id36+fZyp+MU5/MIbYKDHKyOAijDKhTLa4DaPFbRgtFUYVGVyEUSZUyRa3YbS4DaOlxqj3Bu0zF2GUCXWyxW0YLW7DaGkwmsjgIowyoUm2uA2jxW0YLS1GGxlchFEmtMkWt2G0uA2jpcPoIoOLMMqELtniNowWt2G09Bj93uD/mrkIo0zoky1uw2hxG18t0ws=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [aaa = _t, bbb = _t, Customer = _t, ddd = _t, eee = _t, fff = _t, ggg = _t, Source = _t, #"2020" = _t, #"2021" = _t, #"2022" = _t, #"2023" = _t]),
    Columns_not_to_Unpivot = Table.AddIndexColumn ( Table.FromList ( List.FirstN ( Table.ColumnNames( Source) , 8))  , "Index" ,1,1),
    #"Removed Other Columns" = Table.SelectColumns(Columns_not_to_Unpivot,{"Column1"}),
    Columns_not_to_Unpivot_List = #"Removed Other Columns"[Column1],
    Refer_to_Source = Source,
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Refer_to_Source, Columns_not_to_Unpivot_List, "Year", "Value"),
    #"Merged Source and Year" = Table.CombineColumns(#"Unpivoted Other Columns",{"Source", "Year"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged"),
    #"Replaced Value" = Table.ReplaceValue(#"Merged Source and Year","Source","",Replacer.ReplaceText,{"Merged"}),
    #"Removed Columns" = Table.RemoveColumns(#"Replaced Value",{"aaa", "bbb", "Customer", "ddd", "eee", "fff", "ggg"}),
    #"Transposed Table" = Table.Transpose(#"Removed Columns"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{" A 2020", type text}, {" A 2021", type text}, {" A 2022", type text}, {" A 2023", type text}, {" B 2020", type text}, {" B 2021", type text}, {" B 2022", type text}, {" B 2023", type text}, {" C 2020", type text}, {" C 2021", type text}, {" C 2022", type text}, {" C 2023", type text}, {" A 2020_1", type text}, {" A 2021_2", type text}, {" A 2022_3", type text}, {" A 2023_4", type text}, {" B 2020_5", type text}, {" B 2021_6", type text}, {" B 2022_7", type text}, {" B 2023_8", type text}, {" C 2020_9", type text}, {" C 2021_10", type text}, {" C 2022_11", type text}, {" C 2023_12", type text}, {" A 2020_13", type text}, {" A 2021_14", type text}, {" A 2022_15", type text}, {" A 2023_16", type text}, {" B 2020_17", type text}, {" B 2021_18", type text}, {" B 2022_19", type text}, {" B 2023_20", type text}, {" C 2020_21", type text}, {" C 2021_22", type text}, {" C 2022_23", type text}, {" C 2023_24", type text}, {" A 2020_25", type text}, {" A 2021_26", type text}, {" A 2022_27", type text}, {" A 2023_28", type text}, {" B 2020_29", type text}, {" B 2021_30", type text}, {" B 2022_31", type text}, {" B 2023_32", type text}, {" C 2020_33", type text}, {" C 2021_34", type text}, {" C 2022_35", type text}, {" C 2023_36", type text}, {" A 2020_37", type text}, {" A 2021_38", type text}, {" A 2022_39", type text}, {" A 2023_40", type text}, {" B 2020_41", type text}, {" B 2021_42", type text}, {" B 2022_43", type text}, {" B 2023_44", type text}, {" C 2020_45", type text}, {" C 2021_46", type text}, {" C 2022_47", type text}, {" C 2023_48", type text}, {" A 2020_49", type text}, {" A 2021_50", type text}, {" A 2022_51", type text}, {" A 2023_52", type text}, {" B 2020_53", type text}, {" B 2021_54", type text}, {" B 2022_55", type text}, {" B 2023_56", type text}, {" C 2020_57", type text}, {" C 2021_58", type text}, {" C 2022_59", type text}, {" C 2023_60", type text}, {" A 2020_61", type text}, {" A 2021_62", type text}, {" A 2022_63", type text}, {" A 2023_64", type text}, {" B 2020_65", type text}, {" B 2021_66", type text}, {" B 2022_67", type text}, {" B 2023_68", type text}, {" C 2020_69", type text}, {" C 2021_70", type text}, {" C 2022_71", type text}, {" C 2023_72", type text}, {" A 2020_73", type text}, {" A 2021_74", type text}, {" A 2022_75", type text}, {" A 2023_76", type text}, {" B 2020_77", type text}, {" B 2021_78", type text}, {" B 2022_79", type text}, {" B 2023_80", type text}, {" C 2020_81", type text}, {" C 2021_82", type text}, {" C 2022_83", type text}, {" C 2023_84", type text}})
in
    #"Changed Type"

 

 

query2

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rdPNCoJQEIbhWxHXbvJ/W15CS3FROXNWIVjef72rpDMcmAjkQ2fxwLtwHPPLIS/yKzNsj+dylzXjY2aEUSYw52Vbb5Id36+fZyp+MU5/MIbYKDHKyOAijDKhTLa4DaPFbRgtFUYVGVyEUSZUyRa3YbS4DaOlxqj3Bu0zF2GUCXWyxW0YLW7DaGkwmsjgIowyoUm2uA2jxW0YLS1GGxlchFEmtMkWt2G0uA2jpcPoIoOLMMqELtniNowWt2G09Bj93uD/mrkIo0zoky1uw2hxG18t0ws=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [aaa = _t, bbb = _t, Customer = _t, ddd = _t, eee = _t, fff = _t, ggg = _t, Source = _t, #"2020" = _t, #"2021" = _t, #"2022" = _t, #"2023" = _t]),
    Columns_not_to_Unpivot = Table.AddIndexColumn ( Table.FromList ( List.FirstN ( Table.ColumnNames( Source) , 8))  , "Index" ,1,1),
    #"Removed Other Columns" = Table.SelectColumns(Columns_not_to_Unpivot,{"Column1"}),
    Columns_not_to_Unpivot_List = #"Removed Other Columns"[Column1],
    Refer_to_Source = Source,
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Refer_to_Source, Columns_not_to_Unpivot_List, "Year", "Value"),
    #"Merged Source and Year" = Table.CombineColumns(#"Unpivoted Other Columns",{"Source", "Year"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged"),
    #"Replaced Value" = Table.ReplaceValue(#"Merged Source and Year","Source","",Replacer.ReplaceText,{"Merged"}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Replaced Value",{"aaa", "bbb", "Customer", "ddd", "eee", "fff", "ggg"}),
    #"Appended Query" = Table.Combine({#"Removed Other Columns1", Query1})
in
    #"Appended Query"

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a Kudos 🙂
Kind Regards,
Avinash
Anonymous
Not applicable

Sorry for the back and forth... I received an expression error saying "Expression.Error: A cyclic reference was encountered during evaluation."

foodd
Super User
Super User

Hello @Anonymous , and thank you for sharing a question with the Community.  This reply is informational. Please follow the decorum of the Community Forum when asking a question.

Please share your work-in-progress Power BI Desktop file (with sensitive information removed) and any source files in Excel format that fully address your issue or question in a usable format (not as a screenshot). You can upload these files to a cloud storage service such as OneDrive, Google Drive, Dropbox, or to a Github repository, and then provide the file's URL.

https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216

Please show the expected outcome based on the sample data you provided.

https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523/highlight/true#M607150

This allows members of the Forum to assess the state of the model, report layer, relationships, and any DAX applied.

 

If your requirement is solved, please make THIS ANSWER a SOLUTION ✔️ and help other users find the solution quickly. Please hit the LIKE 👍 button if this comment helps you.  Proud to be a Super User!

Anonymous
Not applicable

Thank you for the information! Have provided the sample data in my update. Thank you!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.