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.

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.
1. Query the two tables
Section titled “1. Query the two tables”- 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.
2. Build one list of all customers
Section titled “2. Build one list of all customers”- Click Append Queries, choose the two queries in the Append dialog as below, and click OK.

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

Customers who bought in both years still appear twice.
- On the Home tab, click Remove Duplicates.

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

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

4. Classify each customer
Section titled “4. Classify each 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"
- Finally, select the This Year and Last Year columns, right-click and choose Remove Columns. The query now lists each customer with their classification.

5. The M code
Section titled “5. The M code”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).