Skip to content

Grouping Tables (to show duplicates)

This page shows how to group a table to count the duplicates of one field, and then carry a value from the grouped rows back onto every row.

Counting duplicates and finding joining dates

Section titled “Counting duplicates and finding joining dates”

The example is a list of employee records. Each row holds an employee’s code, name and position, with the start and end dates of that position, so an employee who has held two positions appears twice.

An Excel table with columns Employee Code, First Name, Last Name, Position, Start Date and Last Date, where employees 001 and 004 each appear on two rows

There are two things to add:

  • a count of the rows for each employee, which shows who is duplicated
  • each employee’s joining date, which is their earliest start date, in a column of its own
  • Load the table into Power Query.
  • Select the Employee Code column, and click Group By on the Home tab.
  • In the dialog, keep the Count output with the Count Rows operation.
  • Click Add aggregation, name the new output Details, and choose the All Rows operation.

The Group By dialog in Advanced mode, grouping by Employee Code, with outputs Count as Count Rows and Details as All Rows

Click OK. The table now has one row per employee, with a Table object in each Details cell.

The grouped query with columns Employee Code, Count and Details, where employees 1 and 4 have a count of 2

On the Add Column tab, click Custom Column. This formula takes the row with the earliest Start Date from each employee’s table:

The Custom Column dialog creating a column called Custom with the formula Table.Min([Details], “Start Date”)

Each cell of the new column holds a Record object, which is that employee’s earliest row.

The grouped query with a new Custom column holding a Record value in each row

  • Click the expand icon on the Custom column and take only the Start Date field from each record.
  • Rename the resulting column to Joining Date.

The grouped query with a Joining Date column, showing 03/02/2013 for employee 1 and 24/04/2014 for employee 4

Click the expand icon on the Details column, and select every field except Employee Code, because the query already has that column.

The expand menu on the Details column, with First Name, Last Name, Position, Start Date and Last Date ticked and Employee Code unticked

Reorder the columns, and the table is back to one row per record with two new columns. Count shows how many rows each employee code has, and Joining Date shows the date each employee first started.

The finished query with Count, Employee Code, First Name, Last Name, Position, Joining Date, Start Date and Last Date, where both of employee 1’s rows show a joining date of 03/02/2013

With the joining date on every row, measures such as each employee’s total length of service can now be worked out correctly.