Cleaning data
This page builds a repeatable way to clean a column of inconsistent entries. Power Query lists every distinct entry in a table on the worksheet, you type the correct value beside each one, and a second query uses that table to correct the source.
It uses three techniques:
- joining tables with Merge Queries
- a self-referencing table, which is a query whose output table you edit by hand and which reads those edits back in on the next refresh
- a conditional column
1. Load the sample data
Section titled “1. Load the sample data”The sample data is a list of names, each with a country. The Country column needs cleaning, because the same country has been typed in several ways, such as Aust. and Australia, or US. and US of A.

- Load the table into Power Query as a query called Names. Choose Close & Load To, and then Only Create Connection, so that no copy of it is written to a worksheet.

2. List every country entered
Section titled “2. List every country entered”The next query lists each distinct entry in the Country column once.
- In the Queries & Connections pane, right-click the Names query and choose Reference. This starts a new query whose source is the output of Names.

- In the new query, remove the Name column, and then remove duplicates from the Country column that remains.
- Name the query Country.

- Sort the column, and then load the query to a table on the worksheet, shown here in green beside the source table.

3. Add substitutes with a self-referencing table
Section titled “3. Add substitutes with a self-referencing table”Type a new column, called Substitute, beside the green table, and enter the correct country wherever an entry needs replacing. Leave the cell blank where the entry is already correct.

As it stands, the typed column is not linked to the query. If the source data changes and the green table is refreshed, the countries move to new rows while the typed substitutes stay where they are, so the two fall out of line. The fix is to make the query read the substitutes back in:
- Load the green table, with both of its columns, into Power Query as a new query called Substitutes, again as a connection only.
- Edit the Country query that produces the green table. On the Home tab, choose Merge Queries, pick the Substitutes query, select the Country column in both tables, and choose a left outer join.

- Expand the new column, and keep only its Substitute field.

- Choose Close & Load. The green table now has a Substitute column of its own from the query, so delete the old typed column beside it, which leaves this:

The table now refers to itself. Each refresh reads the substitutes typed into it and writes them back beside the right country, so they stay aligned however the source table changes.
4. Add new data
Section titled “4. Add new data”When new rows are added to the source table, any new countries appear in the green table after a refresh:

The new entries are hard to pick out, because a blank substitute could mean either an unchecked entry or one that needs no change. It is clearer to give every checked entry a substitute, even where the substitute is the same as the entry.
- Fill in the correct country for every row of the green table.
Any blank substitute now marks an entry that nobody has checked yet:

5. Apply the substitutes to the source
Section titled “5. Apply the substitutes to the source”With every country mapped, the last query is a copy of the source data that uses the substitute in place of the country as typed.
- Create another query that references Names.
- Merge it with Substitutes as before, joining on the Country column.

- Expand the new column to bring in the Substitute field.

- Remove the original Country column, and rename Substitute to Country.
Loaded to the worksheet, shown here in orange, the query gives every name a consistent country:

When a new country appears in the source, it has no substitute yet, so the output shows a blank until someone fills in the green table:

6. Flag entries that need a substitute
Section titled “6. Flag entries that need a substitute”A blank is easy to miss, so a final touch is to replace it with a message. In the final query, choose Conditional Column on the Add Column tab, and return a message where Country is null and the country otherwise:

Use the new column as the country column in place of the old one. After a refresh, any row whose country still needs a substitute says so:

This example follows Matt Allington’s post Cleansing Data with Power Query on Excelerator BI.