Leo Tulipan
Leo Tulipan

Reputation: 320

Powerquery pivot & unpivot columns with different contents (headers)

I need to clean up a report with 45.000 lines of poorly formated sales data, that looks like this:

Col1 Col2 Col3 Col4
00/01 Week ProductA ProductB
00/01 1 5 5
00/01 2 5 5
00/02 Week ProductA ProductB
00/02 1 5 5
00/02 2 5 5
00/03 Week ProductC ProductB
00/03 2 5 5

The Problems:

I need to unpivot Col3 (and actually all product columns somehow to arrive at proper table I can work with like:

StoreNo Week ProductA ProductB ProductC
00/01 1 5 5
00/01 2 5 5
00/02 1 5 5
00/02 2 5 5
00/03 2 5 5

I looked far and wide, but either don't know enough PowerQuery M and/or don't know how to call this problem to find a proper solutions. Note: The real table has 88 columns like this with various mixed products (about 30 different products all in all)

Upvotes: 1

Views: 95

Answers (2)

horseyride
horseyride

Reputation: 21318

NOTE: Use David answer as the better one

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Base=({"Col1","Col2"}), //names of columns to keep
repeating_groups=1,  // then stack repeating groups of xx columns
Combo = List.Transform(List.Split(List.Difference(Table.ColumnNames(Source),Base),repeating_groups), each Base & _),
#"Added Custom" =List.Accumulate(
        Combo, 
        #table({"Column1"}, {}),
        (state,current)=> state & Table.Skip(Table.DemoteHeaders(Table.SelectColumns(Source, current)),1)
),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each if [Column2]="Week" then [Column3] else null),
#"Filled Down" = Table.FillDown(#"Added Custom1",{"Custom"}),
#"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Column2] <> "Week")),
#"Pivoted Column1" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Custom]), "Custom", "Column3", List.Sum),
#"Renamed Columns1" = Table.RenameColumns(#"Pivoted Column1",{{"Column2", "Week"}, {"Column1", "StoreNo"}})
in  #"Renamed Columns1"

enter image description here

Upvotes: 3

davidebacci
davidebacci

Reputation: 30219

enter image description here

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDQNzBUUNJRCk9NzQbRAUX5KaXJJY5IbCelWB0klWDCFEygShhhkTAi2mwjXGYb4TLbGIvZzljNNsYwIhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Col1 " = _t, #"Col2 " = _t, #"Col3 " = _t, Col4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1 ", type text}, {"Col2 ", type text}, {"Col3 ", type text}, {"Col4", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Col1 "}, {{"All", each _, type table [#"Col1 "=nullable text, #"Col2 "=nullable text, #"Col3 "=nullable text, Col4=nullable text]}}),
    Custom1 =  Table.TransformColumns(#"Grouped Rows", {{"All", each Table.PromoteHeaders(_) }}),
    #"Expanded All" = Table.ExpandTableColumn(Custom1, "All", {"Week ", "ProductA ", "ProductB", "ProductC "}, {"Week ", "ProductA ", "ProductB", "ProductC "})
in
    #"Expanded All"

Upvotes: 3

Related Questions