Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.
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 | 100 | ||
2 | SA8000 | Økotex | 200 | |
3 | SA8000 | BSCI | 300 |
How can I consolidate to show amount pr. vendor in each category like below?
Vendor | SA8000 | Økotex | BSCI |
1 | 0 | 100 | 0 |
2 | 200 | 200 | |
3 | 300 | 300 |
Solved! Go to Solution.
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"
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"
Unpivot the 3 middle columns.
Remove the Attribute column.
Filter out blank values from Value column.
Pivot the Value column, with Amount in Values (and Do Not Aggregate in Advanced section)