Power Query Group By: Sum, Count, Average, and Custom Aggregations

Power Query Group By feature image showing a grouped sales summary table with North GBP 88,500 revenue, 342 orders, and GBP 258.77 average order value, alongside the Table.Group M code for multiple aggregations, the advanced multi-column grouping pattern using Region and Product, the distinct count formula using Table.Distinct inside the group expression, and six example pills including SUMIF equivalent and weighted average.
Fifty thousand transaction rows need to become a clean summary — total revenue by region, order count by product, average order value by month. Power Query’s Group By builds this in a few clicks and delivers a real flat table you can load anywhere, merge into other queries, or use as a lookup source. Advanced mode lets you add multiple group columns and multiple aggregations simultaneously. The All Rows aggregation type unlocks custom M expressions for patterns the UI doesn’t natively support — distinct customer counts, SUMIF-style conditional sums, and weighted averages. This guide covers both Basic and Advanced mode, six worked examples including regional revenue summaries, multi-metric aggregations, distinct customer counts, conditional aggregation using Table.SelectRows inside a group, and a revenue-weighted average price pattern.

Your raw data has 50,000 transaction rows. You need total revenue by region, average order value by product, and customer counts by month — all in one clean summary table. Power Query Group By builds this summary in a few clicks. It collapses your detailed data into aggregated rows, applying SUM, COUNT, AVERAGE, MIN, MAX, or custom M expressions to each group. Unlike PivotTables, the result is a real table you can join, extend, and load anywhere.

This guide covers the UI dialog, Advanced mode for multiple aggregations, All Rows for custom M calculations, and six worked examples. These include regional revenue summaries, customer order frequency, multi-column grouping, conditional aggregation, and a weighted average pattern.

What Does Group By Do in Power Query?

Group By collapses a table so that rows sharing the same group value become one summary row. For each group, you specify one or more aggregations. Each becomes a separate output column. The output table has one row per unique group combination.

Group By is not a PivotTable replacement. PivotTables are interactive and visual. Group By produces a flat table you can load, merge, or use as a lookup source. Specifically, Group By is the right tool when you need an aggregated table as an input to further transformations, not just a visual summary.

AggregationWhat it computesM function
SumTotal of all values in the groupList.Sum
AverageMean of all values in the groupList.Average
MinLowest value in the groupList.Min
MaxHighest value in the groupList.Max
Count RowsNumber of rows in the groupTable.RowCount
Count Distinct RowsUnique row count in the groupTable.Distinct, Table.RowCount
All RowsNested table of all rows (for custom M)Table reference

How to Use Group By in Power Query

1
Load your data into Power Query. Go to Home → Group By. The Group By dialog opens.
2
In Basic mode: select the column to group by, then choose one aggregation operation and the column to apply it to. Click OK.
3
In Advanced mode: click the Advanced radio button. Add multiple group-by columns using "+ Add Grouping". Add multiple aggregations using "+ Add Aggregation". Each aggregation creates a separate output column.
4
Click OK. The table collapses to one row per unique combination of the grouping columns, with one column per aggregation.

Examples 1–4: Group By in Practice

1
Sum revenue by region — the standard Group By use case

The simplest case is collapsing a transaction table to total revenue per region. Group By on Region with a Sum aggregation on Amount produces a clean two-column output — one row per region.

Before: Region
Before: Amount
After: TotalRevenue
North
£4,200
£88,500
North
£3,100
(grouped)
South
£5,800
£62,300
Home → Group By → Basic. Group by: Region. New column name: TotalRevenue. Operation: Sum. Column: Amount. Click OK. M code: Table.Group(Source, {"Region"}, { {"TotalRevenue", each List.Sum([Amount]), type number} })
2
Multiple aggregations — sum, count, and average in one step

Advanced mode lets you add several aggregations at once. A single Group By step can show Total Revenue, Order Count, and Average Order Value per region. The result is a four-column table: Region plus the three metrics.

Home → Group By → Advanced. Group by: Region. Add three aggregations: TotalRevenue | Sum | Amount OrderCount | Count Rows AvgOrder | Average | Amount Click OK. M code: Table.Group(Source, {"Region"}, { {"TotalRevenue", each List.Sum([Amount]), type number}, {"OrderCount", each Table.RowCount(_), type number}, {"AvgOrder", each List.Average([Amount]), type number} })
3
Group by multiple columns — Region and Product summary

Grouping by two columns gives one row per unique combination of both. Grouping by Region and Product gives one row per Region-Product pair. This is more granular than Region alone but more compact than the original detail rows.

Home → Group By → Advanced. Click "+ Add Grouping" to add a second group column. Group by: Region, then Product. Aggregation: TotalRevenue | Sum | Amount. M code: Table.Group(Source, {"Region", "Product"}, { {"TotalRevenue", each List.Sum([Amount]), type number} }) Result: one row per Region-Product combination. North + Widget A: £28,400 North + Widget B: £19,600 South + Widget A: £22,100 etc.
4
Count distinct values — unique customers per region

Count Rows counts all rows in each group, including duplicate customer IDs. To count distinct customers — unique CustomerIDs only — use the All Rows aggregation type and apply a custom M expression that deduplicates the nested table before counting.

Home → Group By → Advanced. Group by: Region. New column: UniqueCustomers. Operation: All Rows (returns the full nested sub-table for each group). Click OK. Then add a custom column on the result that applies distinct-count logic: Table.AddColumn(Grouped, "UniqueCustomers", each Table.RowCount(Table.Distinct([AllRows], {"CustomerID"})) ) Or combine in a single Group By using All Rows and a custom M expression: Table.Group(Source, {"Region"}, { {"UniqueCustomers", each Table.RowCount(Table.Distinct(_, {"CustomerID"})), type number} })

Examples 5–6: Conditional Aggregation and Weighted Average

5
Conditional aggregation — sum only rows meeting a condition

Power Query’s Group By dialog does not have a native SUMIF equivalent. However, the All Rows aggregation combined with a Table.SelectRows filter achieves conditional aggregation inside the grouped sub-table. This produces a SUMIF-style result: sum of Amount where Status = "Completed", grouped by Region.

Goal: sum only Completed orders per region. Using All Rows aggregation with M filtering: Table.Group(Source, {"Region"}, { {"CompletedRevenue", each List.Sum( Table.SelectRows(_, each [Status] = "Completed")[Amount] ), type number} }) This is equivalent to =SUMIF(Region, "North", Amount) but for every region simultaneously. Result: one row per region with CompletedRevenue = sum of Completed orders only.
6
Weighted average — revenue-weighted average price per region

A simple average of price per region treats a £1 order the same as a £100,000 order. A weighted average uses quantity or revenue as the weight. In Power Query, this requires summing (Price * Quantity) and dividing by total Quantity for each group — both calculated inside the All Rows aggregation.

Weighted average price = SUM(Price * Quantity) / SUM(Quantity) per region. Table.Group(Source, {"Region"}, { {"WeightedAvgPrice", each List.Sum(List.Transform( Table.ToRows(_), each _{1} * _{2} ← Price (col 1) * Quantity (col 2) )) / List.Sum(_[Quantity]), type number} }) Alternatively, add a helper column (Price * Quantity = WeightedValue) before grouping, then group with Sum(WeightedValue) / Sum(Quantity). This approach is more readable and easier to maintain.

Common Issues and How to Fix Them

Group By shows fewer rows than expected

The likely cause is two values that look identical but differ in spacing, capitalisation, or hidden characters. Apply Trim, Clean, and Text.Lower to the grouping column(s) before applying Group By. After normalising, previously distinct values collapse into the correct group.

Group By lost the original row detail

Group By is a destructive aggregation — the grouped output only contains the aggregated values, not the original rows. To keep both detail and summary, create two queries from the same source. Then merge the grouped query back to add group-level metrics as extra columns on each row.

Frequently Asked Questions

  • How does Group By work in Power Query?+
    Group By collapses a table to one row per unique combination of the grouping columns. For each group, you specify one or more aggregations — Sum, Average, Count, Min, Max, or a custom M expression. The result has one column per aggregation. Use Basic mode for a single aggregation and Advanced mode for multiple aggregations or multiple grouping columns. The M function is Table.Group(table, {"GroupColumn"}, {"NewColumn", each List.Sum([ValueColumn]), type number}).
  • How do I count distinct values within each group?+
    Use the All Rows aggregation type and apply a custom M expression. In the M code: Table.Group(Source, {"Region"}, {"UniqueCustomers", each Table.RowCount(Table.Distinct(_, {"CustomerID"})), type number}). The underscore represents the nested sub-table for each group. Table.Distinct removes duplicate CustomerID rows within that sub-table, and Table.RowCount counts the remaining unique rows.
  • How do I do a SUMIF equivalent in Power Query Group By?+
    Use All Rows aggregation with a Table.SelectRows filter inside the M expression: each List.Sum(Table.SelectRows(_, each [Status] = "Completed")[Amount]). This sums the Amount column only for rows where Status equals "Completed" within each group. It is equivalent to SUMIF but applies to every group simultaneously in one step.
  • Can I group by multiple columns?+
    Yes. In Advanced mode, click "+ Add Grouping" to add more group-by columns. Each additional column further subdivides the groups. For example, grouping by Region and Product produces one row per Region-Product combination. In M code, pass a list of column names as the second argument: Table.Group(Source, {"Region", "Product"}, {aggregations}).