Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel Power Query Trim vs Clean: Key Differences

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Trim and Clean both tidy text in Power Query, but they solve different problems: Trim removes leading and trailing spaces only, while Clean strips non-printable characters (such as line breaks, tabs, and control codes) and then trims extra spaces between words. Use Trim when the only issue is stray spaces at the ends; use Clean when text pasted from PDFs, emails, or web pages contains hidden characters that break matching, filtering, or lookups.

What You'll Need

  • Excel with Power Query (Excel 2016 or later, or Microsoft 365). The steps below use the Power Query Editor; menu labels can differ slightly by version and region.
  • A table or range of text data loaded into Power Query (Data > From Table/Range).
  • Basic familiarity with adding a custom column or transforming an existing column.

Step-by-Step

  1. Load your data into Power Query. Select your table, then go to Data > From Table/Range. The Power Query Editor opens.
  2. Identify the problem. Look at the column values. If text has visible spaces at the start or end, Trim is usually enough. If you see odd symbols, line breaks, or text that will not match a lookup, suspect non-printable characters and plan to use Clean.
  3. Apply Trim to a column. Select the column, then on the Home tab click Transform > Trim. This removes leading and trailing spaces but leaves internal spacing untouched. To trim all text columns at once, select multiple columns (Ctrl+click) before clicking Trim.
  4. Apply Clean to a column. Select the column, then on the Transform tab click Clean. This removes non-printable characters and also collapses extra spaces between words to a single space. It does not remove leading or trailing spaces by itself, so follow with Trim if needed.
  5. Combine both when necessary. Select the column, click Clean, then click Trim. Order matters: Clean first removes hidden characters, then Trim removes the remaining edge spaces.
  6. Use a custom column for more control. If you want to keep the original and create a cleaned copy, go to Add Column > Custom Column and enter a formula such as Text.Trim(Text.Clean([ColumnName])). Replace [ColumnName] with your actual column name. This applies Clean then Trim in one step.
  7. Check the result. Scroll through the column. Use the column filter dropdown to look for blanks or unexpected values. If the data still looks wrong, compare the original and cleaned columns side by side.
  8. Load the cleaned data. On the Home tab, click Close & Load to send the result to a worksheet or the Data Model.

Common Mistakes

  • Assuming Trim removes all extra spaces. Trim only removes spaces at the beginning and end of the text. Multiple spaces between words remain. Clean collapses internal spaces, but it also removes non-printable characters, which may be more than you want.
  • Using Clean when Trim is enough. Clean can alter text in ways you did not intend, such as removing line breaks you wanted to keep. If only edge spaces are the problem, use Trim.
  • Forgetting that Clean does not trim. After Clean, leading and trailing spaces may still exist. Add a Trim step after Clean.
  • Applying steps to the wrong column. Power Query applies transformations to the selected column(s). Double-check the selection before clicking Trim or Clean.
  • Expecting these functions to fix numbers stored as text. Trim and Clean work on text. If a column contains numbers with spaces, convert the data type after cleaning.

Tips

  • Use the formula bar to see the exact M code Power Query generates. For Trim it is Text.Trim([Column]); for Clean it is Text.Clean([Column]).
  • To apply both in one custom column, use Text.Trim(Text.Clean([Column])).
  • If you need to remove only specific characters, use Replace Values instead of Clean.
  • Preview changes in the data preview before loading. You can always delete a step from the Applied Steps pane.
  • For columns that will be used in merges or relationships, clean and trim both sides to avoid mismatches.

Troubleshooting

  • Trim did not remove spaces. The spaces may be non-breaking spaces (character 160) or other whitespace that Trim does not treat as a space. Use Clean or a Replace Values step to remove them.
  • Clean removed characters you wanted. Clean removes all non-printable characters, including line breaks. If you need line breaks, use Replace Values to target only the unwanted characters, or skip Clean.
  • The column still will not match. Check for case differences, trailing spaces on the other table, or data type mismatches. Clean and trim both columns being compared.
  • Custom column formula errors. Ensure the column name is spelled exactly as in the query, including spaces and capitalization, and is enclosed in square brackets.
  • Changes do not appear after loading. Make sure you clicked Close & Load and that the query is refreshed. If you loaded to the Data Model, the worksheet may not show the changes directly.

Frequently Asked Questions

Does Trim remove all spaces in Power Query?

No. Trim removes only leading and trailing spaces. Spaces between words are left as they are.

What exactly does Clean remove?

Clean removes non-printable characters such as line breaks, tabs, and control codes, and it collapses multiple spaces between words into a single space.

Can I use both Trim and Clean on the same column?

Yes. Apply Clean first, then Trim. Or use a custom column with Text.Trim(Text.Clean([Column])).

Why does my text still have spaces after Trim?

The spaces may be non-breaking spaces or other whitespace characters that Trim does not recognize. Use Clean or Replace Values to remove them.

Does Clean change numbers?

Clean works on text. If a column contains numbers, Power Query may treat them as text. After cleaning, set the correct data type.

Is there a way to trim all columns at once?

Yes. Select multiple columns (Ctrl+click) and then click Trim on the Home tab. The same applies to Clean on the Transform tab.