Power Query Group by and Sum

DanleyL

New Member
Joined
Jan 17, 2008
Messages
4
I've had a few instances where I am grouping by 4 or 5 columns but then need to sum maybe 12 to 15 columns. Is there a way to accomplish this without having to enter the sum fields one at a time thru all sum columns?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Can you post some sample data and the results you want to see, based on that sample data?

Hi Ron. See below sample data. I am grouping by Dept/Class/Vendor. Then I will sum the remaining columns. So I highlight the first 3 columns and select "Group By". The grouping automatically shows completed. Then below that I have to enter each field to sum one at a time. Lets say below data has 35 columns to sum. I am now entering those one at a time in the group by function. The question is, is there a way to accomplish this without having to enter each sum field one at a time? I can get the results I want. Just looking for a time saver. Thanks for your time.

DEPTCLASSVENDORINVSALESPURCHRTVUNITADPRCCHGALLOW
11361122713541807944763931651
11361122695971508660579907848
11361313766977739503536795971
11361313856558758684507981702
22102247534779874595959828812
22102247948722685762681623586
22102888922927974574806897910
22102888764618612661825780633

<tbody>
</tbody>
 
Upvote 0
I put your sample data in an Excel Table named Table1 and connected it to Power Query.
In the query editor:
- Select the first 3 columns...Transform.Unpivot.Unpivot_Other_Columns
- Select the first 3 columns...Group by...Sum...Value

This is the M-Code:
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
       {"DEPT", Int64.Type}, 
       {"CLASS", Int64.Type}, 
       {"VENDOR", Int64.Type}, 
       {"INV", Int64.Type}, 
       {"SALES", Int64.Type}, 
       {"PURCH", Int64.Type}, 
       {"RTV", Int64.Type}, 
       {"UNITAD", Int64.Type}, 
       {"PRCCHG", Int64.Type}, 
       {"ALLOW", Int64.Type}}),

    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", 
       {"DEPT", "CLASS", "VENDOR"}, 
       "Attribute", 
       "Value"),

    #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", 
       {"DEPT", "CLASS", "VENDOR"}, 
       {{"Totals", each List.Sum([Value]), type number}})
in
    #"Grouped Rows"
These are the results:
Code:
DEPT    CLASS     VENDOR     Totals
1       136       1122       10518
1       136       1313       10333
2       210       2247       10388
2       210       2888       10903

Is that something you can work with?
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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