Forum Discussion
Anonymous
2 years agoNot 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 ...
adudani
2 years agoMemorable Member
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"
Anonymous
2 years agoNot applicable
Sorry for the back and forth... I received an expression error saying "Expression.Error: A cyclic reference was encountered during evaluation."