Forum Discussion

KasperJ90's avatar
KasperJ90
Helper III
4 years ago
Solved

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  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 
101000
2200200 
3300 300
  • 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"

     

2 Replies

  • HotChilli's avatar
    HotChilli
    Community Champion

    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)

     

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most 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"