Forum Discussion

MichaelF1's avatar
MichaelF1
Helper III
3 years ago
Solved

Help needed with PIVOT/UNPIVOT in Power Query

Hi everyone,

 

I have the following data structure in Power Query:

date categorynum1num2
01/01/2022A17
01/01/2022B28
02/01/2022A39
02/01/2022B410
03/01/2022A511
03/01/2022B612

 

I need to get it into this form:

 

datenum1Anum2Anum1Bnum2B
01/01/20221728
02/01/202239410
03/01/2022511612

 

I've tried various iterations of Pivot and Unpivot but can't seem to get it right!

 

Is anyone able to help please? 🙂

 

Thank you

 

Michael

 

 

 

  • MichaelF1 ,

    Hi

    Try following steps.

    Step 1.  Select num1 and num2 columns then click unpivot column.

     

    Step 2. First select Attribute column then select Value column then click MergeColumns.

     

    Step 3. First select Merged column then select Value column then click Pivet Column.

    Finally you will get table as follow.

     

    M code.

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyMjJR0lRyA2BGJzpVgdNCknIAbRFhApIzRdxkBsiSkF0mUCMtQAImeMps0UJGeIKQfSZwaSM1KKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"date " = _t, category = _t, num1 = _t, num2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"date ", type date}, {"category", type text}, {"num1", Int64.Type}, {"num2", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"date ", "category"}, "Attribute", "Value"),
    #"Merged Columns" = Table.CombineColumns(#"Unpivoted Columns",{"Attribute", "category"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
    #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Merged]), "Merged", "Value", List.Sum)
    in
    #"Pivoted Column"

     

    Thank you.

3 Replies

  • Dinesh_Suranga's avatar
    Dinesh_Suranga
    Continued Contributor

    MichaelF1 ,

    Hi

    Try following steps.

    Step 1.  Select num1 and num2 columns then click unpivot column.

     

    Step 2. First select Attribute column then select Value column then click MergeColumns.

     

    Step 3. First select Merged column then select Value column then click Pivet Column.

    Finally you will get table as follow.

     

    M code.

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyMjJR0lRyA2BGJzpVgdNCknIAbRFhApIzRdxkBsiSkF0mUCMtQAImeMps0UJGeIKQfSZwaSM1KKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"date " = _t, category = _t, num1 = _t, num2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"date ", type date}, {"category", type text}, {"num1", Int64.Type}, {"num2", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"date ", "category"}, "Attribute", "Value"),
    #"Merged Columns" = Table.CombineColumns(#"Unpivoted Columns",{"Attribute", "category"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
    #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Merged]), "Merged", "Value", List.Sum)
    in
    #"Pivoted Column"

     

    Thank you.

  • SivaMani's avatar
    SivaMani
    Resident Rockstar

    MichaelF1 Follow the below steps,

    1. UNPIVOT num1 and num2 columns
    2. Merge category and attribute( has num1 and num2 as values) columns
    3. PIVOT the merged column

     

     #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"date ", "category"}, "Attribute", "Value"),
        #"Merged Columns" = Table.CombineColumns(#"Unpivoted Columns",{"Attribute", "category"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
        #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Merged]), "Merged", "Value", List.Sum)

     

     

    If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

    Appreciate your Kudos!

    MCT | Power BI Analyst | Azure Data Engineer

    LinkedIn