Grouping Tables (to show summary statistics)
This page shows how to group a table and then add a calculated column that gives a summary statistic for each group.
Summary statistics from a grouped table
Section titled “Summary statistics from a grouped table”Group By summarises a table by one or more columns. When it is combined with a custom column, it can also produce statistics that a plain total cannot, such as which item was largest in each group.
The example is a car dealer’s sales list, with one row per car sold:

The aim is to find two things for each day and Product Category:
- the total sales
- the top-selling product, and its share of that day’s sales in the category
1. Load the data
Section titled “1. Load the data”Bring the table into Power Query and set the date type:
- Select any cell in the table, then go to the Data tab and click From Table/Range.
- Right-click the Date column and choose Change Type, then Date.
The table is now ready to group.

2. Group by Date and Product Category
Section titled “2. Group by Date and Product Category”- Select the Date column.
- On the Transform tab, click Group By.
The Group By dialog sets which columns to group on and what to calculate for each group. With Advanced selected, it starts with Date as the only grouping column and a single Count Rows output.

Click Add grouping and choose Product Category in the new drop-down, so that the table is grouped by both columns.

Then set the outputs:
- Rename the Count column to Products Sold, keeping Count Rows as its operation.
- Click Add aggregation to add a second output.
- Name it Sales GBP, and set it to Sum the OTR Price column.

Click OK, and the table collapses to one row per date and category.

To load this result to Excel:
- Rename the query to Cars_Grouped.
- On the Home tab, click Close & Load.
3. Add the summary statistic
Section titled “3. Add the summary statistic”The next task is to find the top-selling product in each group, and what share of the group’s sales it accounts for. This needs the rows behind each total, so reopen the query and edit its Grouped Rows step:
- Click the gear icon beside Grouped Rows in Applied Steps.
- Click Add aggregation to add a third output.
- Name it Details and set its operation to All Rows.
- Click OK.

The new Details column holds a Table object in each row. Each table contains the original rows, from before the grouping, that make up that row’s totals.
Click in the white space beside the word Table in any cell, and Power Query previews that table below the grid.

To pick out the top-selling item in each group, add a custom column:
- On the Add Column tab, click Custom Column.
- Name the column MaxRecord and enter this formula:
= Table.Max ([Details], "OTR Price")- Click OK.

Table.Max returns the row with the highest OTR Price from each group’s table, so each cell of MaxRecord is a Record object. Expand it and tidy the columns:
- Click the expand icon in the MaxRecord header.
- Tick Product Name and OTR Price, untick the option to use the original column name as a prefix, and click OK.
- Right-click the Details column and choose Remove.
- Right-click the Product Name column, choose Rename, and call it Top Seller.
- Right-click the OTR Price column, choose Rename, and call it Top Seller GBP.

Finally, work out the top seller’s share:
- On the Add Column tab, click Custom Column.
- Name the column TS% of Sales and enter this formula:
= [#"Top Seller GBP"] / [#"Sales GBP"]- Select the new column, then on the Transform tab click Rounding, choose Round, and enter 2 decimal places.
- Change the column’s data type to Percentage.
- Close & Load the query, and apply the Percentage style to the column in Excel.
The finished table answers both questions:
