Skip to content

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

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.

An Excel table of thirteen names, each with a country, where the countries include Aust., Deutschland, US., U.K. and US of A beside their usual spellings

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

The Import Data dialog with Only Create Connection selected

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.

The right-click menu on the Names query in the Queries & Connections pane, with Reference highlighted

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

The Country query with ten distinct country entries, and Applied Steps listing Source, Removed Columns and Removed Duplicates

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

The source table beside a green table that lists the ten distinct countries in alphabetical order

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.

The green table with a Substitute column typed beside it, mapping Aust. to Australia, Deutschland to Germany, U.K. to UK, and US of A and US. to USA

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.

The Merge dialog joining the Country query to the Substitutes query on their Country columns with a Left Outer join, where 10 of 10 rows match

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

The expand menu on the Substitutes column, with only Substitute ticked and the original column name not used as a prefix

  • 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 green table with its Country and Substitute columns, both now produced by the query

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.

When new rows are added to the source table, any new countries appear in the green table after a refresh:

The source table with three new names from France, Italia and Espana, and the green table showing those three countries with no substitute

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:

The green table with a substitute on every row, including Australia for Australia, Spain for Espana and Italy for Italia

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.

The Merge dialog joining the Names query to the Substitutes query on their Country columns with a Left Outer join, where 16 of 16 rows match

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

The merged query with Name, Country and Substitute columns, where Aust. becomes Australia, Deutschland becomes Germany and Espana becomes Spain

  • 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:

The source table, the green substitution table and an orange output table in which every country is spelt consistently

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:

A new row for Mikko from Nippon in the source, where Nippon has no substitute in the green table and Mikko’s country is blank in the orange table

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:

The Add Conditional Column dialog, which returns MAINTAIN SUBSTITUTE TABLE where Country equals null and returns the Country column 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:

The orange table after a refresh, where Mikko’s country reads MAINTAIN SUBSTITUTE TABLE

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