Inserting Summation Rows in Power Query

everiwa

New Member
Joined
Oct 23, 2020
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
I am using Office 2019 and i want to add a summation row after every last unique ref "GRV" and also the cell below the last unique reference "GRV', i want to sum totals of Columns "Tpt Charge","Offloading Fee" & "Tollgate Fee". How do i do that?
 

Attachments

  • After.PNG
    After.PNG
    55.5 KB · Views: 18
  • Before.PNG
    Before.PNG
    50.2 KB · Views: 19

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You make a second query, referring to this one and do a group by "GRV", doing the sum on those columns. Give your your totals columns the same names. Add a Custom Column with "Index" 1,000,000.
Refer again to the first query, add an Index and append the second one with the totals.
Sort on GRV and Index.
 
Upvote 0
[EDIT] Example in 1 Query, same idea, including both total now.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type any}, {"Site", type any}, {"SBU", type any}, {"Order", type any}, {"Tonnes", type number}, {"Tpt Charge", type number}, {"Offloading Fee", type number}, {"Tollgate Fee", type any}, {"GRV", type text}, {"p/order", type any}}),
#"Replaced Value" = Table.ReplaceValue(#"Changed Type",null,0,Replacer.ReplaceValue,{"Tonnes", "Tpt Charge", "Offloading Fee", "Tollgate Fee"}),
#"Grouped Rows" = Table.Group(#"Replaced Value", {"GRV"}, {{"Tonnes", each List.Sum([Tonnes]), type number}, {"Tpt Charge", each List.Sum([Tpt Charge]), type number}, {"Offloading Fee", each List.Sum([Offloading Fee]), type number}, {"Tollgate Fee", each List.Sum([Tollgate Fee]), type none}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "GRV1", each [Tonnes]+[Tpt Charge]+[Offloading Fee]+[Tollgate Fee]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Index", each 1000000),
#"Grouped Rows1" = Table.Group(#"Added Custom1", {}, {{"Tonnes", each List.Sum([Tonnes]), type number}, {"Tpt Charge", each List.Sum([Tpt Charge]), type number}, {"Offloading Fee", each List.Sum([Offloading Fee]), type number}, {"Tollgate Fee", each List.Sum([Tollgate Fee]), type number}, {"GRV1", each List.Sum([GRV1]), type number}, {"Index", each List.Sum([Index]), type number}}),
Custom1 = #"Changed Type",
#"Added Index" = Table.AddIndexColumn(Custom1, "Index", 1, 1),
#"Added Custom2" = Table.AddColumn(#"Added Index", "GRV1", each [GRV]),
Custom2 = Table.Combine({#"Added Custom2",#"Added Custom1",#"Grouped Rows1"}),
#"Replaced Value1" = Table.ReplaceValue(Custom2,null,"ZZZ99999999",Replacer.ReplaceValue,{"GRV"}),
#"Sorted Rows" = Table.Sort(#"Replaced Value1",{{"GRV", Order.Ascending}, {"Index", Order.Ascending}}),
#"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"GRV", "p/order", "Index"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"GRV1", "GRV"}})
in
#"Renamed Columns"
 
Last edited:
Upvote 0
Please try this:

VBA Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    Custom1 = Table.Combine(Table.Group(Source,"GRV",{"n",each let c=Table.ToColumns(_) in #table(Table.ColumnNames(_),Table.ToRows(_)&{{null,null,null,null}&Record.ToList([m=List.Sum(c{4}),n=List.Sum(c{5}),o=List.Sum(c{6}),p=List.Sum(c{7}),q=m+n+o+p,r=null])})})[n])
in
    Custom1

1603717085545.png

供参考.xlsx
ABCDEFGHIJ
1Despath DateSiteSBUOrder NumberTonnesTpt ChargeOffloading FeeTollgate FeeGRVp/order
24538745Q116759
39487672Q116759
484355462Q116759
579324278Q216759
699334398Q216760
786602749Q216760
840124978Q216760
96234302Q216763
1093753369Q316763
1150392737Q316763
12Despath DateSiteSBUOrder NumberTonnesTpt ChargeOffloading FeeTollgate FeeGRVp/order
134538745Q116759
149487672Q116759
1584355462Q116759
1622381204139647
1779324278Q216759
1899334398Q216760
1986602749Q216760
2040124978Q216760
216234302Q216763
223661711913051033
2393753369Q316763
2450392737Q316763
2514311460106423
Sheet2
 
Upvote 0
Rather advanced with the let inside a function and the compressed code for grouping. Impressive. Not sure if less advanced user can adopt this easily.
 
Upvote 0
Rather advanced with the let inside a function and the compressed code for grouping. Impressive. Not sure if less advanced user can adopt this easily.
Yes, for beginners it is not easy to understand and apply. GUI+M function would be a better way.
 
Upvote 0
Another 1 step M-Code

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Grouped = Table.Combine(Table.Group(Source,"GRV",{"C",each let a={List.Sum([Tonnes]),List.Sum([Tpt Charge]),List.Sum([Offloading Fee]),List.Sum([Tollgate Fee])} in Table.Combine({_,#table( List.Range(Table.ColumnNames(_),4,5),{a&{List.Sum(a)}})})})[C])
in
    Grouped
 
Upvote 0
Also nice one. Though one can argue it is a one line solution. It is nesting functions. According to some experts that is not always better then adding extra steps. Yet as always it depends. Might well be the case these short codes also run way faster. I did not test.
Just shows PQ offers ways that appeal to both non programmers like myself and to professionals having code writing kills like you guys being able to come up with these elegant pieces of work.
 
Upvote 0

Forum statistics

Threads
1,223,761
Messages
6,174,342
Members
452,555
Latest member
colc007

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