New Tool Radar

AI for Data & Spreadsheets

How to Trim and Clean Text Columns in Power Query

Remove leading and trailing spaces and hidden characters from text columns in Power Query, so lookups match after loading.

Schematic with three steps: Select the text columns; Apply Trim, then Clean; Fix the case, then check the row.

In Power Query, Transform, Format, Trim removes spaces at the start and end of a value, and Clean removes non-printing characters. Cleaning in the query means the fix is recorded as a step and re-applied on every refresh, instead of being a one-off formula.

What you need

  • Excel with the data loaded in the Power Query Editor
  • A note of which columns are text and should be cleaned
  • A copy of the source file

Step 1: Select the text columns

In the Power Query Editor, select the text columns that need cleaning. Clean name and code columns first, because spaces there break lookups and grouping.

Step 2: Apply Trim, then Clean

Choose Transform, Format, Trim to remove leading and trailing spaces, then Transform, Format, Clean to remove non-printing characters. Check the preview after each step; each one appears in the Applied Steps list on the right.

Step 3: Fix the case, then check the row count

Use Transform, Format, Capitalize Each Word or lowercase to make the case consistent. Confirm the row count at the bottom of the editor has not changed; cleaning should change values, not remove rows.

Worked example: synthetic 25-row customer list

The table below is a synthetic example. It shows the expected result of trimming and cleaning in Power Query; no file was imported.

Check Count Result
Rows loaded 25 Source
Names trimmed 25 All rows
Names matching the lookup list 25 Pass after cleaning
Rows removed by cleaning 0 Cleaning keeps rows

Expected result (UNTESTED). all 25 names are trimmed and cleaned, all 25 now match the lookup list, and no rows are removed by the cleaning steps. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • Power Query Trim leaves repeated spaces inside the text, unlike Excel TRIM which collapses them.
  • Fix case and spaces before removing duplicates, because Power Query treats case-different values as different.
  • Click any step in the Applied Steps list to see the data as it looked at that moment.

Sources

See all