Skip to content

Distinct customers

This page counts how many different customers bought something on each day. A plain count of rows per day would overstate the number, because some customers made more than one purchase on the same day, so the duplicates are removed first.

The example starts from this Excel table of sales, which has one row per transaction.

An Excel table named Customers with Date, Customer and Sales columns, holding fourteen transactions over three days in January 2020, with some customers appearing more than once on a day

  • Click inside the table, and on Excel’s Data tab click From Table/Range. The Power Query Editor opens with a new query.
  • Select the Date column, and set its data type to Date from the Data Type list.

2. Remove duplicate combinations of date and customer

Section titled “2. Remove duplicate combinations of date and customer”
  • Hold down Shift or Ctrl, and click the Date and Customer column headers to select both.
  • Click Remove Duplicates, either on the Home tab or by right-clicking one of the selected headers, as below.

The Power Query Editor with the Date and Customer columns selected and the right-click menu open at Remove Duplicates

Power Query keeps the first row for each combination of date and customer, and removes the rest. The table now has 9 rows instead of 14.

The table after removing duplicates, with nine rows and each customer appearing at most once per date

Each row now stands for one customer on one date, so counting the rows for a date gives the number of distinct customers on that date.

  • Select the Date column, and on the Home tab click Group By.
  • Check that Date is the column to group by, type DistinctCustomers as the new column name, leave the operation as Count Rows, and click OK.

The Group By dialog, grouping by Date, with a new column named DistinctCustomers and the operation Count Rows

The query now has one row per date, with the number of distinct customers beside it.

  • Click Close & Load to load the result to a table on a new worksheet.

An Excel table with Date and DistinctCustomers columns, showing three distinct customers on each of the three dates

let
//Load data from the Excel worksheet
Source = Excel.CurrentWorkbook(){[Name="Customers"]}[Content],
//Set the data type of the Date column to date
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Customer", type text}, {"Sales", Int64.Type}}),
//Remove all duplicate combinations of Date and Customer
#"Removed Duplicates" = Table.Distinct(#"Changed Type", {"Date", "Customer"}),
//Find the count of rows per date
#"Grouped Rows" = Table.Group(#"Removed Duplicates", {"Date"}, {{"DistinctCustomers", each Table.RowCount(_), Int64.Type}})
in
#"Grouped Rows"

This example follows one in Chris Webb’s Power Query for Power BI and Excel (Apress).