Power Query: Group by - Output Multiple Sum Columns

takoyaki

New Member
Joined
Oct 24, 2022
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I'm currently working on a query that will eventually feed into a chart. The original table was too large and exceeded the chart's maximum 255 data series so I wanted to rewrite the query to group the rows by multiple columns with constant names. The issue is that the header names of the columns with values to be summed are variable, as is the amount of columns to sum.

Illustration of my desired result:

1667323034403.png


Here is the code so far; it is currently able to create a list of the DateHeaders and divide the values in them by the Hours in the Lookup table but doesn't currently have the capability of grouping rows.

Power Query:
let
    Table1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content]
    DateHeaders = List.Buffer(List.Select(Table.ColumnNames(Table1), each try Value.Is(Date.From(_), type date) otherwise false)),
    Hours = Table.TransformColumnTypes(Excel.CurrentWorkbook(){[Name="Hours"]}[Content],{{"Period", type date}}),
    Division = List.Accumulate(DateHeaders, Table1, (s,c) =>
        Table.TransformColumns(s, {{c, each _/Hours{[Period = Date.From(c)]}[Hours]}}))

in
    Division

I can easily use the UI to Group By but I am unsure how to rewrite the generated M-Code to create sum columns for all of my DateHeaders columns.

Power Query:
Group = Table.Group(Table1, {"Column1", "Column2", "Column2"}, {{"1/1/2022", each List.Sum([#"1/1/2022"]), type nullable number}})

Any help would be very much appreciated!
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I did a bit more research and found the solution:

Power Query:
let
    Table1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content]
    DateHeaders = List.Buffer(List.Select(Table.ColumnNames(Table1), each try Value.Is(Date.From(_), type date) otherwise false)),
    Hours = Table.TransformColumnTypes(Excel.CurrentWorkbook(){[Name="Hours"]}[Content],{{"Period", type date}}),
    Group = Table.Group(Table1, {"Column1", "Column2", "Column3"}, List.Transform(DateHeaders, (C) => {C, each List.Sum(Table.Column(_,C))})),
    Division = List.Accumulate(DateHeaders, Group, (s,c) =>
        Table.TransformColumns(s, {{c, each _/Hours{[Period = Date.From(c)]}[Hours]}}))

in
    Division
 
Upvote 0
Solution

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

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