Skip to content

New, lost, and returning customers

This page compares two customer lists and labels every customer by whether they bought in one year, the other or both. The example starts from two Excel tables: one of the customers who bought something this year, and one of the customers who bought something last year.

Two Excel tables side by side: Customers This Year lists six names and Customers Last Year lists four, with three names appearing in both

The query sorts the customers into three groups:

  • Returning customers bought something both this year and last year.
  • Lost customers bought something last year but not this year.
  • New customers bought something this year but not last year.
  • Click inside each table in turn, and on Excel’s Data tab click From Table/Range to create a query for it.
  • Name the queries ThisYearCustomers and LastYearCustomers.
  • Click Append Queries, choose the two queries in the Append dialog as below, and click OK.

The Append dialog with Two tables selected, ThisYearCustomers as the primary table and LastYearCustomers as the table to append

Power Query creates a new query and opens it in the Power Query Editor.

  • Name the new query Customers. It holds the rows of both tables, one under the other. Because the two tables have different column names, each name sits in its own column, with null in the other.

The appended table, with names from this year in the Customers This Year column, names from last year in the Customers Last Year column, and null filling the gaps

The next job is to turn the two columns into a single list of names.

  • Select both columns, right-click and choose Merge Columns.
  • Leave the separator set to None, and click OK.

Because each row has a name in only one column, the merged column holds one name per row.

A single Merged column listing ten names, some of which appear twice

Customers who bought in both years still appear twice.

  • On the Home tab, click Remove Duplicates.

The Merged column with the duplicates removed, leaving seven distinct names

  • Double-click the column header and rename the column from Merged to Customer.

3. Count each customer’s appearances in each year

Section titled “3. Count each customer’s appearances in each year”

With a complete list of customers, the query next finds which of them appear in this year’s list. It does this by merging the Customers query with ThisYearCustomers.

  • On the Home tab, click Merge Queries.
  • In the Merge dialog, choose ThisYearCustomers as the second table, select the Customer column in the first table and the Customers This Year column in the second, leave the join kind as Left Outer, and click OK.

The Merge dialog joining Customers on Customer to ThisYearCustomers on Customers This Year with a Left Outer join, matching 6 of 7 rows

The merge adds a column of tables, where each table holds the rows of ThisYearCustomers that match that customer.

  • Click the expand icon in the new column, and choose Aggregate rather than Expand. Open the drop-down list next to the column name, tick Count (Not Blank), and click OK.

The aggregate menu for the ThisYearCustomers column, with Count (Not Blank) ticked for Customers This Year

The column of tables becomes a count of the matching rows. It holds 1 where the customer bought something this year, and 0 where they did not.

  • Right-click the header of the new column and rename it This Year.

The Customers query with a This Year column holding 1 for six customers and 0 for the seventh

  • Repeat the merge and the aggregation with the LastYearCustomers query, and name the new column Last Year.

The Customers query with This Year and Last Year columns, each holding 1 or 0 for every customer

  • On the Add Column tab, click Custom Column, name the new column Classification, and enter this formula:
if [This Year]=1 and [Last Year]=1 then "Returning" else if [This Year]=1 and [Last Year]=0 then "New" else "Lost"

The Custom Column dialog with the new column named Classification and the if-then-else formula referring to This Year and Last Year

  • Finally, select the This Year and Last Year columns, right-click and choose Remove Columns. The query now lists each customer with their classification.

The final query with a Customer column and a Classification column showing Returning, New or Lost for each customer

let
//Append the LastYearCustomers table to the ThisYearCustomers table
Source = Table.Combine({ThisYearCustomers, LastYearCustomers}),
//Merge the two columns into one
#"Merged Columns" = Table.CombineColumns(Source,{"Customers This Year", "Customers Last Year"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
//Remove duplicate customers
#"Removed Duplicates" = Table.Distinct(#"Merged Columns"),
#"Renamed Columns" = Table.RenameColumns(#"Removed Duplicates",{{"Merged", "Customer"}}),
//Merge this query with the ThisYearCustomers table
#"Merged Queries" = Table.NestedJoin(#"Renamed Columns", {"Customer"}, ThisYearCustomers, {"Customers This Year"}, "ThisYearCustomers", JoinKind.LeftOuter),
//Aggregate the resulting column of tables by Count (Not Blank)
#"Aggregated ThisYearCustomers" = Table.AggregateTableColumn(#"Merged Queries", "ThisYearCustomers", {{"Customers This Year", List.NonNullCount, "Count (Not Blank) of Customers This Year"}}),
#"Renamed Columns1" = Table.RenameColumns(#"Aggregated ThisYearCustomers",{{"Count (Not Blank) of Customers This Year", "This Year"}}),
//Merge this query with the LastYearCustomers table
#"Merged Queries1" = Table.NestedJoin(#"Renamed Columns1", {"Customer"}, LastYearCustomers, {"Customers Last Year"}, "LastYearCustomers", JoinKind.LeftOuter),
//Aggregate the resulting column of tables by Count (Not Blank)
#"Aggregated LastYearCustomers" = Table.AggregateTableColumn(#"Merged Queries1",
"LastYearCustomers", {{"Customers Last Year", List.NonNullCount, "Count (Not Blank) of Customers Last Year"}}),
//Rename the new column to LastYear
#"Renamed Columns2" = Table.RenameColumns(#"Aggregated LastYearCustomers",{{"Count (Not Blank) of Customers Last Year", "Last Year"}}),
//Use the ThisYear and LastYear columns to classify each customer
#"Added Custom" = Table.AddColumn(#"Renamed Columns2", "Classification", each if [This Year]=1 and [Last Year]=1 then "Returning" else if [This Year]=1 and [Last Year]=0 then "New" else "Lost"),
//Remove unwanted columns
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"This Year", "Last Year"})
in
#"Removed Columns"

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