Excel Power Query Trim Spaces: Step-by-Step Guide
In this article
To trim spaces in Excel Power Query, select the column (or columns) you want to clean, then go to Transform > Trim (or Format > Trim in the Power Query Editor). This removes leading and trailing spaces from text values. For spaces inside the text, use Replace Values with a space as the value to find and nothing as the replacement. You can also use the Text.Trim function in a custom column for more control. No add-ins are needed—Power Query is built into Excel 2016 and later, and available as a free add-in for Excel 2010/2013.
What You'll Need
- Excel 2016 or later (Power Query is on the Data tab). For Excel 2010/2013, install the Microsoft Power Query add-in from Microsoft's official download page.
- Data loaded into Power Query: select your table and go to Data > From Table/Range (or Data > Get Data for external sources).
- Basic familiarity with the Power Query Editor window.
Step-by-Step
- Load your data into Power Query. Select a cell in your table, then go to Data > From Table/Range. The Power Query Editor opens. If you're pulling from a file or database, use Data > Get Data and pick your source. Note that menu labels can differ slightly by Excel version, device, or region.
- Select the column(s) to clean. Click the column header. Hold Ctrl to select multiple non-adjacent columns, or Shift for a range. To clean every text column, click the first column header, then Shift+click the last—but be aware this also selects numeric columns, where Trim has no effect.
- Apply Trim. On the Home tab, go to Transform group and click Trim. In some versions it's under Format > Trim. This removes leading and trailing spaces only. A new step appears in the Applied Steps pane on the right.
- Remove internal spaces (if needed). Trim does not touch spaces between words. To collapse double spaces, select the column, go to Transform > Replace Values. In Value To Find, type a single space; leave Replace With blank. Click OK. Repeat if there are triple spaces (or use the
Text.Replacefunction with a loop for many). - Use a custom column for advanced trimming. Go to Add Column > Custom Column. Enter a formula such as
= Text.Trim([ColumnName])to trim leading/trailing spaces, or= Text.Trim(Text.Replace([ColumnName], " ", " "))to also collapse double spaces. Replace[ColumnName]with your actual column name. Click OK. - Check the result. Scroll through the preview. Spaces are invisible, so use a helper column with
= Text.Length([ColumnName])to verify lengths changed as expected. Remove the helper column afterward. - Load the cleaned data. Go to Home > Close & Load (or Close & Load To… to choose a location). The cleaned table appears in Excel. Refresh later via Data > Refresh All.
Common Mistakes
- Using Trim on numbers. Trim only affects text. If a column is typed as a number, the step does nothing. Convert the column to Text first (right-click header > Change Type > Text) if it contains numbers stored as text with spaces.
- Expecting Trim to remove all spaces. Trim removes only leading and trailing spaces, not internal ones. Use Replace Values or
Text.Replacefor internal spaces. - Forgetting non-breaking spaces. Excel's Trim handles regular spaces (ASCII 32) but not non-breaking spaces (ASCII 160), which often come from web copy. Use
Text.Replace([Column], Character.FromNumber(160), " ")before trimming, orText.Trimwon't catch them. - Applying steps to the wrong column. Always confirm the selected column in the Applied Steps description. If you selected multiple columns, the step name lists them; check it.
- Not refreshing after changes. If you edit the source data, the query won't update until you refresh. Use Data > Refresh All.
Tips
- Combine Trim with Clean (Transform > Clean) to remove non-printable characters as well.
- Use Column From Examples to show Power Query the desired output; it writes the M code for you.
- Rename steps in the Applied Steps pane for clarity—right-click a step and choose Rename.
- For recurring imports, save the query as a connection only and load to the Data Model for reuse.
- Test on a copy of your workbook first, especially if you're replacing values across many columns.
Troubleshooting
- Trim button is greyed out: The selected column is not text type. Change the type to Text first (right-click header > Change Type > Text).
- Spaces remain after Trim: They may be non-breaking spaces or other whitespace characters. Use
Text.ReplacewithCharacter.FromNumber(160)orText.Cleanbefore trimming. - Query errors after adding a custom column: Check the column name in brackets matches exactly (case-sensitive). Use the formula bar to correct it.
- Data doesn't update in Excel: Ensure the query is loaded to a table, then refresh. If using Close & Load To…, confirm you chose Table.
- Trim affects only some rows: The column may have mixed types. Set the type explicitly to Text for the whole column.
Frequently Asked Questions
Does Power Query Trim remove all spaces?
No. Trim removes only leading and trailing spaces. Internal spaces between words remain. Use Replace Values or Text.Replace to remove or collapse internal spaces.
Can I trim spaces in multiple columns at once?
Yes. Select multiple columns (Ctrl+click or Shift+click) before clicking Trim. The step applies to all selected text columns.
What is the difference between Trim and Clean in Power Query?
Trim removes leading/trailing spaces. Clean removes non-printable characters (like line breaks and tabs). They are often used together.
Why doesn't Trim work on my numbers?
Trim only works on text. If a column is numeric, convert it to Text first, then trim, then convert back if needed.
How do I remove non-breaking spaces in Power Query?
Use a custom column with = Text.Replace([Column], Character.FromNumber(160), " ") then apply Trim. Non-breaking spaces are common when copying from websites.
Can I undo a Trim step?
Yes. In the Applied Steps pane, click the X next to the Trim step to delete it. The data reverts to the previous step.