Skip to content

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.

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:

An Excel table of car sales with columns Date, Product ID, Product Name, Product Category and OTR Price, covering seven cars in the Sports, SUV and Saloon categories

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

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.

The sales table in the Power Query editor, with Date typed as a date and Product ID and OTR Price as whole numbers

  • 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.

The Group By dialog in Advanced mode, grouping by Date, with one output column called Count using Count Rows

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

The Group By dialog grouping by Date and Product Category

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.

The Group By dialog with two outputs, Products Sold as Count Rows and Sales GBP as the Sum of OTR Price

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

The grouped table, with columns Date, Product Category, Products Sold and Sales GBP, and five rows

To load this result to Excel:

  • Rename the query to Cars_Grouped.
  • On the Home tab, click Close & Load.

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 Group By dialog with a third output, Details, using the All Rows operation

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.

The grouped table with a Details column of Table values, and a preview below showing the two Sports rows for 03/06/2020, Lambourghini and Ferrari

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.

The grouped table with a new MaxRecord column holding a Record value in each row

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.

The grouped table with the Details column removed and two new columns, Top Seller and Top Seller GBP, showing the highest-priced car in each group

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:

The loaded Excel table with Date, Product Category, Products Sold, Sales GBP, Top Seller, Top Seller GBP and TS% of Sales, where Ferrari makes up 56% of Sports sales on 03/06/2020