Power Query to Add Subtotal & Grand Total Row

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,214
Office Version
  1. 365
Platform
  1. Windows
StationAmountUnitsCost
5102.70
5204.70
5 Total306.70
629.70
6 Total29.70
753.70
7107.70
7 Total1510.70
Grand Total4725.70
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hello All,

I'm looking for a power query solution to add subtotal row and a grand total row like the above. Note the last column the value stays the same. Prefer using the Power Query interface for the solution if possible else a custom code will suffice. Thanks!
 
Upvote 0
Why not close and load Power Query as a pivot table ?
Yeah I know I could probably use that but I have a lot of power query tables that gets loaded directly so wanted to avoid creating any pivot tables. Besides, not sure if pivot tables can get me that last column with the same subtotal and grand total amount
 
Upvote 0
@legalhustler, try this below
Power Query:
let
  Source = Excel.CurrentWorkbook(){[Name = "Tabela1"]}[Content], 
  #"Changed Type" = Table.TransformColumnTypes(
    Source, 
    {{"Station", Int64.Type}, {"Amount", Int64.Type}, {"Units", Int64.Type}, {"Cost", type number}}
  ), 
  #"Grouped Rows" = Table.Group(
    #"Changed Type", 
    {"Station"}, 
    {
      {"tbl", each _, type table}, 
      {"Amount", each List.Sum([Amount]), type nullable number}, 
      {"Units", each List.Sum([Units]), type nullable number}, 
      {"Cost", each List.First([Cost]), type nullable number}
    }
  ), 
  #"Added Custom" = Table.AddColumn(
    #"Grouped Rows", 
    "Tables", 
    each Table.Combine(
      {
        [tbl], 
        Table.FromRecords(
          {Record.RemoveFields(_, "tbl") & [Station = Text.From([Station]) & " Total"]}
        )
      }
    )
  ), 
  Grand = Table.Group(
    #"Changed Type", 
    {}, 
    {
      {"Station", each "Grand Total", type nullable text}, 
      {"Amount", each List.Sum([Amount]), type nullable number}, 
      {"Units", each List.Sum([Units]), type nullable number}, 
      {"Cost", each List.First([Cost]), type nullable number}
    }
  ), 
  Result = Table.Combine(#"Added Custom"[Tables] & {Grand})
in
  Result
 
Upvote 0
Please try


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Grouped = Table.Group(Source, {"Station"}, {{"T", each  Table.InsertRows(_,Table.RowCount(_),{[Station= Text.From([Station]{0})&" Total",Amount= List.Sum([Amount]),Units=List.Sum([Units]),Cost=List.Average([Cost])]})}}),
    Combine = Table.Combine(Grouped[T]),
    GrandTotal = Table.InsertRows(Combine,Table.RowCount(Combine),{[Station= "Grand Total",Amount= List.Sum(Source[Amount]),Units=List.Sum(Source[Units]),Cost=List.Average(Source[Cost])]})
in
    GrandTotal
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,957
Members
452,539
Latest member
delvey

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