Power Query: Pivot Column With Multiple Values Columns OR Hierarchical Header

DerekK

Board Regular
Joined
Jun 18, 2007
Messages
93
Office Version
  1. 2003 or older
Platform
  1. Windows
Good day,

In Power Query, I need to go from this:

Serial NumberDate/TimeData Pont 1Data Point 2
000108/14/2024 10:00DP1ADP2A
000108/14/2024 10:10DP1BDP2B
000108/14/2024 10:20DP1CDP2C
000108/14/2024 10:30DP1DDP2D
000208/14/2024 10:00DP1ADP2A
&c.

to this:

Serial Number08/14/2024 10:0008/14/2024 10:0008/14/2024 10:1008/14/2024 10:1008/14/2024 10:2008/14/2024 10:2008/14/2024 10:3008/14/2024 10:30
Data Point 1Data Point 2Data Point 1Data Point 2Data Point 1Data Point 2Data Point 1Data Point 2
0001DP1ADP2ADP1BDP2BDP1CDP2CDP1DDP2D
0002DP1ADP2ADP1BDP2BDP1CDP2CDP1DDP2D
&c.

Basically, pivoting the "Date/Time" column with both the "Data Point 1" and "Data Point 2" columns as values.

I hope this makes sense; hope you can help!

Sincerely,



Derek
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangeType = Table.TransformColumnTypes(Source,{{"Date/Time", type datetime}}),
    AddCustom = Table.AddColumn(ChangeType, "Data", each {[Data Point 1],[Data Point 2]}),
    RemoveColumns = Table.RemoveColumns(AddCustom,{"Data Point 1", "Data Point 2"}),
    ExpandData = Table.ExpandListColumn(RemoveColumns, "Data"),
    AddIndex = Table.AddIndexColumn(ExpandData, "Index", 0, 1, Int64.Type),
    AddCustom1 = Table.AddColumn(AddIndex, "Custom", each Text.From([#"Date/Time"]) & (if Number.IsEven([Index]) then " DP1" else " DP2")),
    RemoveColumns1 = Table.RemoveColumns(AddCustom1,{"Date/Time", "Index"}),
    PivotColumn = Table.Pivot(RemoveColumns1, List.Distinct(RemoveColumns1[Custom]), "Custom", "Data")
in
    PivotColumn
 
Upvote 0
Solution
Good day,

In Power Query, I need to go from this:

Serial NumberDate/TimeData Pont 1Data Point 2
000108/14/2024 10:00DP1ADP2A
000108/14/2024 10:10DP1BDP2B
000108/14/2024 10:20DP1CDP2C
000108/14/2024 10:30DP1DDP2D
000208/14/2024 10:00DP1ADP2A
&c.

to this:

Serial Number08/14/2024 10:0008/14/2024 10:0008/14/2024 10:1008/14/2024 10:1008/14/2024 10:2008/14/2024 10:2008/14/2024 10:3008/14/2024 10:30
Data Point 1Data Point 2Data Point 1Data Point 2Data Point 1Data Point 2Data Point 1Data Point 2
0001DP1ADP2ADP1BDP2BDP1CDP2CDP1DDP2D
0002DP1ADP2ADP1BDP2BDP1CDP2CDP1DDP2D
&c.

Basically, pivoting the "Date/Time" column with both the "Data Point 1" and "Data Point 2" columns as values.

I hope this makes sense; hope you can help!

Sincerely,



Derek
Hi Derek,
Another solution for fun.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    S_cols = Table.ColumnNames(Source),
    Grouped = Table.Group(Source, S_cols{0}, {{"x", each
        let
        a = List.Union(Table.ToColumns (Table.SelectColumns(_, List.Skip(S_cols,2)))), 
        b = List.Union(List.Accumulate(List.Skip(S_cols,2), {},(s,c)=> s & {List.Transform(Table.Column(_,S_cols{1}), each Text.From(_) & " - " & c) }))
        in Table.FromRows({a},b)}}),
    Result = let F_cols = List.Sort(List.Distinct(List.Combine(List.Transform(Grouped[x], Table.ColumnNames)))) 
             in Table.ExpandTableColumn(Grouped, "x", F_cols)
in
    Result

Regards,
 
Upvote 0
Appreciate it!

Now, would some patient soul be kind enough to explain exactly what took place to accomplish this, viz., what each step did?

I tried to reproduce it just by doing individual steps instead of pasting in a slightly revised version of the code but I was not able.
 
Upvote 0
Please see attempt to split code with explanation step by step.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    // list of Source's columns name
    S_cols = Table.ColumnNames(Source),
    // grouped Source per "Serial Number" - 1st item of S_cols list (S_cols{0})
    Grouped = Table.Group(Source, S_cols{0}, 
              {
                  // col a: list of values for 2 last Source's columns per group
                  {"a", each List.Union(Table.ToColumns (Table.SelectColumns(_, List.Skip(S_cols,2))))}, 
                  // col b: list of columns name through list.accumulate to add the column name suffix for each date per group
                  {"b", each List.Union(List.Accumulate(List.Skip(S_cols,2), {},(s,c)=> s & {List.Transform(Table.Column(_,S_cols{1}), each Text.From(_) & " - " & c)}))}
              }),
    // create table from columns a and b          
    #"Added Custom" = Table.AddColumn(Grouped, "x", each Table.FromRows({[a]},[b])),
    // list of sorted columns name to dynamically expand column x in next step
    F_cols = List.Sort(List.Distinct(List.Combine(List.Transform(#"Added Custom"[x], Table.ColumnNames)))),
    Expanded = Table.ExpandTableColumn (#"Added Custom", "x", F_cols),
    #"Removed Columns" = Table.RemoveColumns(Expanded,{"a", "b"})
in
    #"Removed Columns"

Hopefully this helps.

Regards,
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top