Power Query course · Lesson 8 of 17

Group By: totals, counts and the All Rows trick

Summarise rows with Group By: sums, averages, counts and distinct counts by one or more columns, plus All Rows to keep details such as the latest order.

Intermediate 15 min read

Group By collapses many rows into one row per group and calculates aggregates for each group, like a PivotTable or the GROUPBY function, but as a step in a query. Use it when the output you need is already a summary (monthly totals per region for a management file), when you must reduce millions of rows before loading, or when a later step needs a total per key, for example to compute each order's share of its customer's revenue.

The sample: an order export

ABCDE
1OrderDateRegionCustomerAmount
2SO-70012026-03-02NorthNordic Foods1200
3SO-70022026-03-02SouthAgora Market450
4SO-70032026-03-05NorthBaltic Trade980
5SO-70042026-03-09NorthNordic Foods2300
6SO-70052026-03-11SouthSol Bakery310
7SO-70062026-03-12SouthAgora Market760

Before: six orders in March.

Basic Group By

  1. Select the Region column.
  2. Choose Home › Group By (also Transform › Group By).
  3. Leave Basic selected. Group by shows Region.
  4. Type Revenue as New column name, choose Operation Sum and Column Amount.
  5. Click OK.
AB
1RegionRevenue
2North4480
3South1520

After: one row per region. North = 1200 + 980 + 2300, South = 450 + 310 + 760.

All other columns disappear: a grouped table contains only the grouping columns and the new aggregate columns. That is why it is often wise to create a summary as a separate query (right-click the query › Reference, then group the reference) and keep the detailed query intact.

Advanced: several keys and several aggregates

Choose Advanced in the dialog to group by more than one column and to add several aggregations. Click Add grouping and Add aggregation to add rows.

OperationResultExcel equivalent
SumTotal of a numeric columnSUMIFS
AverageMean of a numeric columnAVERAGEIFS
MedianMedian of a numeric columnMEDIAN with FILTER
Min / MaxSmallest / largest value (works on dates too)MINIFS / MAXIFS
Count RowsNumber of rows in the groupCOUNTIFS
Count Distinct RowsNumber of different whole rows in the groupCOUNTA of UNIQUE
All RowsA nested table with every row of the groupnone

Example: group by Region with three aggregations: Revenue = Sum of Amount, Orders = Count Rows, Last order = Max of Date.

ABCD
1RegionRevenueOrdersLast order
2North448032026-03-09
3South152032026-03-12

After: several aggregates in one Group By step.

Power Query M
= Table.Group(#"Changed Type", {"Region"}, {
    {"Revenue", each List.Sum([Amount]), type nullable number},
    {"Orders", each Table.RowCount(_), Int64.Type},
    {"Last order", each List.Max([Date]), type nullable date}
})
Each aggregation is a list of {name, function, type}. Inside each, _ is the group's table and [Amount] is that table's Amount column as a list.

Reading this code is the key to customising Group By. For every group, Power Query builds a small table containing that group's rows and passes it as _ to each function. [Amount] on that table returns a list of amounts, so any list function works: List.Sum, List.Average, List.Max, List.Count, List.Distinct, List.StandardDeviation…

Distinct count of customers

Watch out. Count Distinct Rows counts different whole rows of the group, not different values of one column. Each order row is unique, so on the sample it returns 3 for each region, not the number of different customers.

To count customers per region, add an aggregation with any operation, then edit its function in the formula bar:

Power Query M
= Table.Group(#"Changed Type", {"Region"}, {
    {"Revenue", each List.Sum([Amount]), type nullable number},
    {"Customers", each List.Count(List.Distinct([Customer])), Int64.Type}
})
List.Distinct removes repeated customers in the group; List.Count counts what is left. North has 2 customers, South has 2.

Grouping by month

You can only group by columns that exist. To get monthly totals from daily dates, first add a month column: select Date and choose Add Column › Date › Month › Start of Month (or Add Column › Date › Year › Year plus Month › Month for separate numbers). Then group by the new column. Grouping by a real date such as 2026-03-01 is better than grouping by the text Mar, because it sorts and filters correctly.

ABC
1Month startRegionRevenue
22026-03-01North4480
32026-03-01South1520

After grouping by Month start and Region (only March in the sample).

All Rows: keep the details of each group

The All Rows operation stores the group's rows as a nested table in each cell. On its own that looks odd (cells showing Table), but it opens powerful patterns: you can work on each small table and then expand the result.

Example: the largest order per customer

  1. Group by Customer with two aggregations: Max amount = Max of Amount, and Details = All Rows.
  2. Add a custom column (Add Column › Custom Column) named Top order with the formula Table.SelectRows([Details], (r) => r[Amount] = [Max amount]).
  3. Remove the Details column, then click the expand icon on Top order and tick Order and Date.
Power Query M
= Table.AddColumn(#"Grouped Rows", "Top order", each Table.SelectRows([Details], (r) => r[Amount] = [Max amount]))
Inside the outer each, [Details] and [Max amount] belong to the grouped row; the inner function (r) => … looks at each order in the nested table. Two different variables avoid the each/_ clash.
ABCD
1CustomerMax amountOrderDate
2Nordic Foods2300SO-70042026-03-09
3Agora Market760SO-70062026-03-12
4Baltic Trade980SO-70032026-03-05
5Sol Bakery310SO-70052026-03-11

After: each customer's largest order. If two orders tie, both are kept.

Example: share of customer revenue

Another classic: for each order, what percentage of its customer's total does it represent? Group by Customer with Total = Sum of Amount and Details = All Rows, expand Details back to Order and Amount, then add a custom column [Amount] / [Total] and set its type to Percentage. Nordic Foods' SO-7004 is 2300 / 3500, about 65.7%.

Tip. For the share-of-total pattern you can also merge the grouped query back into the detailed one (Merge queries: every join kind explained). The All Rows method avoids a second query and is easy to read.

Group By and refresh performance

  • Group as early as possible when the details are not needed: loading 50 summary rows is much faster than loading 500,000 detail rows to a sheet.
  • With a database source, Group By usually folds into a SQL GROUP BY and runs on the server (Performance, query folding and refresh options). Custom list functions in the aggregation may stop folding.
  • If you need both the detail and the summary, load the detail to the Data Model and use a PivotTable rather than two separate queries.
Note. The Group By dialog is the same on Windows, Mac and the web. In very old versions (Excel 2016 without updates) the Basic/Advanced switch is missing and all options are shown at once.

Check yourself

1. After a Group By on Region, what happens to the Customer column?

2. Inside a Group By aggregation, what is [Amount]?

3. Which formula counts different customers per group?

4. What does the All Rows operation produce?

5. You need monthly totals but the source has daily dates. What do you do first?