Skip to content

Grouping Tables (to show row detail)

This page shows how to group a table and still show some of the detail from the rows behind each total, which a standard PivotTable cannot do.

A PivotTable is good at summarising. It adds up a numeric field, and it counts a text field.

The Excel table below has both kinds of field. Each row records a sale of cars of one make, with its group, its colour and the quantity sold:

An Excel table of car sales with columns Date, Item, Group, Color and Qty, listing makes such as Porsche, Ford, Honda and VW in the Luxury, Mid-range and Sport groups

A PivotTable summarises it by Group and Item, with the quantities summed:

A PivotTable with Group and Item as row fields and Sum of Qty as the value, totalling 56

Double-clicking the value for VW opens a new sheet with the row detail behind its quantity of 5, which turns out to be two rows:

The drill-down sheet for VW, showing two Mid-range rows, one Orange with a quantity of 4 and one Grey with a quantity of 1

That detail stays on the drill-down sheet, though. The PivotTable itself cannot list VW’s colours, Orange and Grey, beside its total. Power Query can, as the steps below show.

Create a new query from the source table:

  • Click anywhere in the table.
  • On Excel’s Data tab, click From Table/Range.

In the Power Query editor:

  • Remove the Date column.
  • Select the Item and Group columns, holding Shift to select both, and click Group By on the Transform tab.
  • Set up the dialog as below, with Qty summed into a Qty column and a second output, ColorList, using the All Rows operation.

The Group By dialog in Advanced mode, grouping by Item and Group, with outputs Qty as the Sum of Qty and ColorList as All Rows

The result has one row per make, and the ColorList column holds a Table object in each row:

The grouped query with columns Item, Group, Qty and ColorList, where every ColorList cell shows Table

Add a custom column named ColorList2, with this formula:

The Custom Column dialog creating ColorList2 with a formula that nests Table.ToList, Table.Transpose, Table.Distinct, Table.FromList and Table.Column, combining the colours with a comma and a space

Reading the formula from the inside out, it works through these steps for each row:

  • Table.Column takes the Color column from the row’s table as a list.
  • Table.FromList turns that list into a one-column table.
  • Table.Distinct removes the duplicate colours from the table.
  • Table.Transpose turns the column into a single row, so that each colour sits in its own column.
  • Table.ToList turns the row back into a list, and Combiner.CombineTextByDelimiter joins the colours into one piece of text, separated by a comma and a space.

The new column holds a List object in each row. Selecting one shows its contents below the grid, such as Red, Yellow for Porsche:

The query with a new ColorList2 column of List values, and a preview below showing Red, Yellow

Click the expand icon in the ColorList2 header and choose Expand to New Rows. Each list holds a single piece of text, so the column now shows the colours for each make in place of the lists.

The query with ColorList2 expanded, showing text such as Red, Yellow for Porsche and Grey, Orange for VW

Reorder and rename the columns, delete the ones that are no longer needed, and use Close & Load To to load the query to a worksheet.

The Power Query table has the same totals as the PivotTable, and it also lists the colours behind each one:

The PivotTable on the left beside the loaded Power Query table on the right, which has Group, Item, Qty and Colors columns and the same total of 56