Need Advice on Unpivot steps

*** READ FIRST ***
TEST - Unpivot.pbix (38.3 KB)

Hi, I’m looking for help with unpivoting in PQ.

I’ve attached a small sample file that contains a transformation problem I’m working with. In reality, I’m working with a new data provider that is sending over data across a very large number of columns that we need to have in rows. I can tell I need to unpivot the relevant columns but somehow I’m not ending up with the result similar to this desired table that I’ve included (the outcome). I can’t clearly tell what I’m doing incorrectly, partly because in my actual file, I’m working with a huge number of columns.

I’m hoping someone can look at the sample .pbix file with current sample data and compare to my screencap of the desired data outcome and help me see the steps I need to take in PQ to accomplish this transformation. Any advice would be greatly appreciated!

2022-10-19_13-52-14 Power Query - Desired Table

Hey Lisaeu,

This looks like a fun table to clean up. I’m sure there are multiple ways to achieve your end result, here’s my solution:

  1. Select the date column and unpivot the other columns
  2. Add a new column by extracting the first word of the “Attribute” column, this will be your cancel type. In my example I called this “Type”.
  3. Add another new column by extracting the last word of the “Attribute” column to define count vs. value rows. In my example I called this “Cat”.
  4. Remove the Attribute column (it’s irrelevant after adding the new columns)
  5. Pivot the “Cat” column and select the Values column.

I hope this works for you.


Here’s the M Code:

let
    Source = Excel.Workbook(File.Contents("G:\My Drive\Learning\Enterprise DNA Master Class\Forum Questions\Power Query - Unpivot.xlsx"), null, true),
    #"Current Data Table_Sheet" = Source{[Item="Current Data Table",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(#"Current Data Table_Sheet", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Blue Cancel Count", Int64.Type}, {"Blue Cancel Value", Int64.Type}, {"Red Cancel Count", Int64.Type}, {"Red Cancel Value", Int64.Type}, {"Yellow Cancel Count", Int64.Type}, {"Yellow Cancel Value", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date"}, "Attribute", "Value"),
    #"Inserted Text Before Delimiter" = Table.AddColumn(#"Unpivoted Other Columns", "Type", each Text.BeforeDelimiter([Attribute], " "), type text),
    #"Inserted Text After Delimiter" = Table.AddColumn(#"Inserted Text Before Delimiter", "Cat", each Text.AfterDelimiter([Attribute], " ", {0, RelativePosition.FromEnd}), type text),
    #"Removed Columns" = Table.RemoveColumns(#"Inserted Text After Delimiter",{"Attribute"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Cat]), "Cat", "Value", List.Sum)
in
    #"Pivoted Column"

Hi Lisa,

  1. get rid of this hardcoded data type changes:
    {“Blue Cancel Count”, Int64.Type}, {“Blue Cancel Value”, Int64.Type}, {“Red Cancel Count”, Int64.Type}, {“Red Cancel Value”, Int64.Type}, {“Yellow Cancel Count”, Int64.Type}, {“Yellow Cancel Value”, Int64.Type}
    => by this you avoid trouble if you get data e.g. without “Blue Cancel Count” / “Blue Cancel Value” and instead e.g. with “Pink Cancel Count” / “Pink Cancel Value”
  2. Unpivot, Split the Attribute, Change the data type of Value and Pivot the column with Count/Value:
let
    Source = Excel.Workbook(File.Contents("G:\My Drive\Learning\Enterprise DNA Master Class\Forum Questions\Power Query - Unpivot.xlsx"), null, true),
    #"Current Data Table_Sheet" = Source{[Item="Current Data Table",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(#"Current Data Table_Sheet", [PromoteAllScalars=true]),

    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}}),

    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByEachDelimiter({" Cancel "}, QuoteStyle.Csv, false), {"Cancel Type", "Attribute"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Value", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Attribute]), "Attribute", "Value", List.Sum)
in
    #"Pivoted Column"

Regards,
Matthias

Hello @Lisaeu It’s been a while since we got a response from you.

Did the responses above help solve your query?
If not, how far did you get, and what kind of help do you need further?
If yes, kindly mark as the solution the answer that solved your query.

Hi @Lisaeu

Checking if you were able to check the solutions provided to your inquiry.

Did it help you solve your inquiry?

If it does, kindly mark the answer as the solution that solved your query.

If not, how far did you get, and what kind of help do you need further?

Hello @Lisaeu , did the response above help solve your query? It’s been a while since we got a response from you. In case there won’t be any activity on it in the next few days, we’ll be tagging this post as Solved.

Hello @Lisaeu, Due to the length by which this post has been active, we are tagging it as Solved. Thanks to the contributors to this post. For further questions related to this post, please make a new thread. Feel free to reopen this thread if anyone would like to answer the pending inquiry above.