Forum Discussion
Chrisjr
Helper IV
3 years agoUnpivot multiple groups
Hello All, I have the following source table . Volvo Peugeot Mercedez Audi Volvo v40 Peugeot 308 A class A1 IndexA 0,5 0,2 0,1 0,1 IndexB 0,7 0,9 0,3 0,1 ...
- 3 years ago
Hi Chrisjr
Here are my transformation steps. PBIX file has been attached at bottom.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRCsvPKcsH0gGppemp+SVAlm9qUXJqSmoVkOlYmpKpFKsTraQAU6lQZmKAUK1gbGABUqaQnJNYXAxiGYKVe+alpFY4AvkGOqZg0ghMGkJJuBInsIA5mLQEk8ZISpAsNUO1FKTDCWGpEbqlRihmQZyBZilE0gRJiRGSpUA7g9E8CnKfM8JOY+weNUPykQW6nZZQYQQb6KxYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t]), #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]), #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"", type text}, {"Volvo", type text}, {"Peugeot", type text}, {"Mercedez", type text}, {"Audi", type text}}), #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type), #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 3), Int64.Type}}), #"Renamed Columns" = Table.RenameColumns(#"Integer-Divided Column",{{"", "Brand"}}), #"Trimmed Text" = Table.TransformColumns(#"Renamed Columns",{{"Brand", Text.Trim, type text}}), #"Replaced Value" = Table.ReplaceValue(#"Trimmed Text","","Model",Replacer.ReplaceValue,{"Brand"}), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Replaced Value", {"Brand", "Index"}, "Attribute", "Value"), #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Model", each if [Brand] = "Model" then [Value] else null), #"Sorted Rows" = Table.Sort(#"Added Custom",{{"Index", Order.Ascending}, {"Attribute", Order.Ascending}, {"Brand", Order.Descending}}), #"Filled Down" = Table.FillDown(#"Sorted Rows",{"Model"}), #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Brand] <> "Model")), #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Index"}), #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Attribute", "Model", "Brand", "Value"}), #"Renamed Columns1" = Table.RenameColumns(#"Reordered Columns",{{"Attribute", "Car Brand"}, {"Brand", "Index"}}), #"Sorted Rows1" = Table.Sort(#"Renamed Columns1",{{"Car Brand", Order.Ascending}, {"Model", Order.Ascending}, {"Index", Order.Ascending}}) in #"Sorted Rows1"Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it. - 3 years ago
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUlDSUQrLzynLVygzMQCyA1JL01PzSxSMDSyAPEeF5JzE4mIQy1ApVidayTMvJbXCEcg30DEFk0Zg0hBKwpU4gQXMwaQlmDRGUoJkqRmqpSAdTghLjdAtNUIxC+IMNEshkiZISoyQLAXaGYzmUZD7nBF2GmP3qBmSjyzQ7bSECiPYQGfFAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column1" = _t, Volvo = _t, Peugeot = _t, Mercedez = _t, Audi = _t]), Custom1 = let brands=List.Skip(Table.ColumnNames(Source)) in Table.Combine(Table.Group(Source,"Column1",{"n",each let a=Table.ToColumns(Table.DemoteHeaders(_)),b=Table.RowCount(_)-1 in Table.Combine(List.Transform(List.Skip(a),each Table.FromColumns(List.Zip(List.Repeat({List.FirstN(_,2)},2))&{List.Skip(a{0},2),List.Skip(_,2)},{"Car Brand","Model","Index","Value"})))},0,(x,y)=>Byte.From(Text.Remove(y," ")="" or y=null))[n]) in Custom1
wdx223_Daniel
Community Champion
3 years agolet
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUlDSUQrLzynLVygzMQCyA1JL01PzSxSMDSyAPEeF5JzE4mIQy1ApVidayTMvJbXCEcg30DEFk0Zg0hBKwpU4gQXMwaQlmDRGUoJkqRmqpSAdTghLjdAtNUIxC+IMNEshkiZISoyQLAXaGYzmUZD7nBF2GmP3qBmSjyzQ7bSECiPYQGfFAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column1" = _t, Volvo = _t, Peugeot = _t, Mercedez = _t, Audi = _t]),
Custom1 = let brands=List.Skip(Table.ColumnNames(Source)) in Table.Combine(Table.Group(Source,"Column1",{"n",each let a=Table.ToColumns(Table.DemoteHeaders(_)),b=Table.RowCount(_)-1 in Table.Combine(List.Transform(List.Skip(a),each Table.FromColumns(List.Zip(List.Repeat({List.FirstN(_,2)},2))&{List.Skip(a{0},2),List.Skip(_,2)},{"Car Brand","Model","Index","Value"})))},0,(x,y)=>Byte.From(Text.Remove(y," ")="" or y=null))[n])
in
Custom1