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.
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
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Order | Date | Region | Customer | Amount |
| 2 | SO-7001 | 2026-03-02 | North | Nordic Foods | 1200 |
| 3 | SO-7002 | 2026-03-02 | South | Agora Market | 450 |
| 4 | SO-7003 | 2026-03-05 | North | Baltic Trade | 980 |
| 5 | SO-7004 | 2026-03-09 | North | Nordic Foods | 2300 |
| 6 | SO-7005 | 2026-03-11 | South | Sol Bakery | 310 |
| 7 | SO-7006 | 2026-03-12 | South | Agora Market | 760 |
Before: six orders in March.
Basic Group By
- Select the
Regioncolumn. - Choose Home › Group By (also Transform › Group By).
- Leave Basic selected. Group by shows
Region. - Type
Revenueas New column name, choose OperationSumand ColumnAmount. - Click OK.
| A | B | |
|---|---|---|
| 1 | Region | Revenue |
| 2 | North | 4480 |
| 3 | South | 1520 |
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.
| Operation | Result | Excel equivalent |
|---|---|---|
| Sum | Total of a numeric column | SUMIFS |
| Average | Mean of a numeric column | AVERAGEIFS |
| Median | Median of a numeric column | MEDIAN with FILTER |
| Min / Max | Smallest / largest value (works on dates too) | MINIFS / MAXIFS |
| Count Rows | Number of rows in the group | COUNTIFS |
| Count Distinct Rows | Number of different whole rows in the group | COUNTA of UNIQUE |
| All Rows | A nested table with every row of the group | none |
Example: group by Region with three aggregations: Revenue = Sum of Amount, Orders = Count Rows, Last order = Max of Date.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Region | Revenue | Orders | Last order |
| 2 | North | 4480 | 3 | 2026-03-09 |
| 3 | South | 1520 | 3 | 2026-03-12 |
After: several aggregates in one Group By step.
= 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}
})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
To count customers per region, add an aggregation with any operation, then edit its function in the formula bar:
= Table.Group(#"Changed Type", {"Region"}, {
{"Revenue", each List.Sum([Amount]), type nullable number},
{"Customers", each List.Count(List.Distinct([Customer])), Int64.Type}
})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.
| A | B | C | |
|---|---|---|---|
| 1 | Month start | Region | Revenue |
| 2 | 2026-03-01 | North | 4480 |
| 3 | 2026-03-01 | South | 1520 |
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
- Group by
Customerwith two aggregations:Max amount= Max of Amount, andDetails= All Rows. - Add a custom column (Add Column › Custom Column) named
Top orderwith the formulaTable.SelectRows([Details], (r) => r[Amount] = [Max amount]). - Remove the
Detailscolumn, then click the expand icon onTop orderand tickOrderandDate.
= Table.AddColumn(#"Grouped Rows", "Top order", each Table.SelectRows([Details], (r) => r[Amount] = [Max amount]))| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer | Max amount | Order | Date |
| 2 | Nordic Foods | 2300 | SO-7004 | 2026-03-09 |
| 3 | Agora Market | 760 | SO-7006 | 2026-03-12 |
| 4 | Baltic Trade | 980 | SO-7003 | 2026-03-05 |
| 5 | Sol Bakery | 310 | SO-7005 | 2026-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%.
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.
Check yourself
1. After a Group By on Region, what happens to the Customer column?
A grouped table contains only grouping and aggregate columns.
2. Inside a Group By aggregation, what is [Amount]?
The group's table is _, and [Amount] returns its Amount column as a list.
3. Which formula counts different customers per group?
List.Distinct removes repeats before counting.
4. What does the All Rows operation produce?
All Rows keeps the details of each group in a table cell.
5. You need monthly totals but the source has daily dates. What do you do first?
Group by a month column derived from the date.