Forum Discussion
KasperJ90
4 years agoHelper III
Multiple columns shown in pivot with only one value
Hi, I have a table looking like below showing a vendor and certificates (SA8000 etc.) for that vendor in independent columns: Vendor SA8000 Økotex BSCI Amount 1 Økotex 1...
- 4 years ago
See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQKiwzOy80tSKyAcQwMDpVidaCUjIDvY0cIAyEVTYQRVYYysAoicgp09gZQxSDoWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Vendor = _t, SA8000 = _t, Økotex = _t, BSCI = _t, Amount = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Vendor", Int64.Type}, {"SA8000", type text}, {"Økotex", type text}, {"BSCI", type text}, {"Amount", Int64.Type}}), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Amount", "Vendor"}, "Attribute", "Value"), Custom1 = Table.ReplaceValue(#"Unpivoted Other Columns",each [Amount],each if [Value]="" then 0 else [Amount],Replacer.ReplaceValue,{"Amount"}), #"Removed Columns" = Table.RemoveColumns(Custom1,{"Value"}), #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Amount") in #"Pivoted Column"
Vijay_A_Verma
4 years agoMost Valuable Professional
See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQKiwzOy80tSKyAcQwMDpVidaCUjIDvY0cIAyEVTYQRVYYysAoicgp09gZQxSDoWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Vendor = _t, SA8000 = _t, Økotex = _t, BSCI = _t, Amount = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Vendor", Int64.Type}, {"SA8000", type text}, {"Økotex", type text}, {"BSCI", type text}, {"Amount", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Amount", "Vendor"}, "Attribute", "Value"),
Custom1 = Table.ReplaceValue(#"Unpivoted Other Columns",each [Amount],each if [Value]="" then 0 else [Amount],Replacer.ReplaceValue,{"Amount"}),
#"Removed Columns" = Table.RemoveColumns(Custom1,{"Value"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Amount")
in
#"Pivoted Column"