Forum Discussion

nerdlump's avatar
nerdlump
Frequent Visitor
7 years ago
Solved

How do I keep what was the column headers after transposing a table

I am tring to transpose a table from this:   Brand    | Purchase intent 1 | Purchase intent 2 | Purchase intent 3  Brand 1 |100                        | 95                         | 65 Brand 2 | ...
  • MFelix's avatar
    7 years ago

    Hi nerdlump ,

     

    You need to use the Unpivot / Pivot options:

    • Select the 3 Purchase columns
    • Unpivot
    • Select the Brand Column
    • Pivot - By Value

    Check the M code below:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcipKzEtRMFTSUTI0MACSlqZAwsxUKVYHJmcEErUEEhYgKRNkKWOQqAVEA9AAoFQsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Brand = _t, #"Purchase Intent 1" = _t, #"Purchase Intent 2" = _t, #"Purchase Intent 3" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Brand", type text}, {"Purchase Intent 1", Int64.Type}, {"Purchase Intent 2", Int64.Type}, {"Purchase Intent 3", Int64.Type}}),
        #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Changed Type", {"Purchase Intent 1", "Purchase Intent 2", "Purchase Intent 3"}, "Attribute", "Value"),
        #"Pivoted Column" = Table.Pivot(#"Unpivoted Only Selected Columns", List.Distinct(#"Unpivoted Only Selected Columns"[Brand]), "Brand", "Value", List.Sum)
    in
        #"Pivoted Column"

    Regards,

    MFelix