Forum Discussion

jeggen's avatar
jeggen
Icon for Helper II rankHelper II
2 years ago
Solved

Unpivot data and combine by year

This seems like it should be simple but can't figure it out.

 

I need to unpivot my data from this:

Customer IDYear 1 RevenueYear 1 CategoryYear 2 RevenueYear 2 CategoryYear 3 RevenueYear 3 Category
1313468C2483B456B
2374B9187B3417B
3428246C371304C977A

 

To this:

Cutomer IDYearRevenue

Customer

Category

11313468C
21374B
31428246C
122483B
229187B
32371304C
13456B
233417B
33977A

 

I assume there is some intervening step wehre my data would retain the original column headers that I would need to clean up that might look something like this.

Cutomer IDLabel 1Label 2RevenueRevenue Category
1Year 1 RevenueYear 1 Category313468C
2Year 1 RevenueYear 1 Category374B
3Year 1 RevenueYear 1 Category428246C
1Year 2 RevenueYear 2 Category2483B
2Year 2 RevenueYear 2 Category9187B
3Year 2 RevenueYear 2 Category371304C
1Year 3 RevenueYear 3 Category456B
2Year 3 RevenueYear 3 Category3417B
3Year 3 RevenueYear 3 Category977A

 

If I can get this far I can cleanup to get the above table. I can't figure out how to pivot into this in way that keeps the years together across one line. Any suggestions on how to unpivot this data to get basically three lines for each customer, one per year, with corresponding revenue and category for that donor and year combination?

  • You won't get that 'intermediate' table.

    Unpivot everything apart from Customer ID.

    Split the Attribute column so that you get a column with  only 'Revenue' or 'Category' in it.

    Pivot that column with the Value as Values.  Use "Don't Aggregate"

    That should be it.

    Let me know.

3 Replies

  • HotChilli's avatar
    HotChilli
    Icon for Community Champion rankCommunity Champion

    You won't get that 'intermediate' table.

    Unpivot everything apart from Customer ID.

    Split the Attribute column so that you get a column with  only 'Revenue' or 'Category' in it.

    Pivot that column with the Value as Values.  Use "Don't Aggregate"

    That should be it.

    Let me know.

  • Nothing to do with pivoting,

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PYyxDcAwCAR3oXYR4GNwmWQMxP5rBEcoxYt7cRBBTIOUFdMLnorAtcZdwTk/yhEk2zP0ZrFbo4Ltt/YlxAWzv6mxHuiybJsXZb4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer ID" = _t, #"Year 1 Revenue" = _t, #"Year 1 Category" = _t, #"Year 2 Revenue" = _t, #"Year 2 Category" = _t, #"Year 3 Revenue" = _t, #"Year 3 Category" = _t]),
        Cols = Table.ToColumns(Source),
        Yearly = let customer = Cols{0} in List.Transform(List.Split(List.Skip(Cols), 2), each Table.FromColumns({customer} & _, {"Customer","Revenue","Category"})),
        ToTable = Table.FromColumns({{1 .. List.Count(Yearly)}, Yearly},{"Year","yearly"}),
        #"Expanded yearly" = Table.ExpandTableColumn(ToTable, "yearly", {"Customer", "Revenue", "Category"})
    in
        #"Expanded yearly"