Smart Systems for Home

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

Power Query Trim Text: Step-by-Step Guide

· 4 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

Power Query's Trim function removes leading and trailing spaces from text values, so " Hello " becomes "Hello". It does not remove spaces inside the text. You can use it through the Transform tab, the right-click menu, or by typing Text.Trim in a custom column formula. The steps below work in Excel and Power BI Desktop; menu labels can differ slightly by version and region.

What You'll Need

  • Excel (Microsoft 365, Excel 2019 or later) or Power BI Desktop with Power Query available.
  • A table or range of data that includes text columns with unwanted leading or trailing spaces.
  • Basic familiarity with the Power Query Editor window.

Step-by-Step

  1. Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI Desktop, use Home > Transform data to open the Power Query Editor.
  2. Select the text column you want to clean. Click the column header. To trim several columns at once, hold Ctrl and click each header, or select all columns with Ctrl+A.
  3. Use the Transform tab. With the column selected, go to Transform > Trim. This removes leading and trailing spaces from every selected text column. If the Trim button is greyed out, the column is not text type; change the type first (see step 5).
  4. Alternatively, right-click the column header and choose Transform > Trim from the context menu. The result is the same.
  5. Check the data type. If Trim is unavailable, the column may be typed as a number or date. Go to the Home tab, open the Data Type dropdown, and set it to Text. Then repeat step 3.
  6. For a custom column, use Text.Trim. Go to Add Column > Custom Column and enter a formula such as Text.Trim([CustomerName]). This creates a new cleaned column and leaves the original intact. Use Text.TrimEnd or Text.TrimStart if you only want to remove spaces from one side.
  7. Preview the result. Scroll through the column to confirm spaces are gone. The Query Settings pane records each step; you can click a step to review it or delete it with the X.
  8. Apply and close. In Excel, click Close & Load > Close & Load to send the cleaned table to a worksheet. In Power BI Desktop, click Close & Apply to load the changes into the model.

Common Mistakes

  • Expecting Trim to remove inner spaces. Text.Trim only removes spaces at the start and end. To collapse double spaces inside text, use Text.Combine(Text.Split([Column], " "), " ") or clean the text before loading.
  • Trimming a non-text column. Trim is only available for text columns. Change the data type to Text first, then trim.
  • Forgetting to apply changes. In Excel, closing the Power Query Editor without Close & Load leaves the original data unchanged.
  • Trimming the wrong column. With multiple columns selected, Trim applies to all of them. Deselect any column you did not mean to change.
  • Assuming Trim removes non-breaking spaces. Standard Trim handles regular spaces. Non-breaking spaces (character 160) may remain; use Text.Replace to remove them explicitly.

Tips

  • Trim early in your query so later steps, such as merges and filters, work on clean values.
  • Use Text.TrimStart or Text.TrimEnd when only one side needs cleaning.
  • Combine Trim with Text.Proper or Text.Upper to standardise case at the same time.
  • Name your steps clearly in the Applied Steps pane so you can find and adjust the trim later.
  • If you trim a column used in a relationship or merge, refresh dependent queries afterwards.

Troubleshooting

  • Trim is greyed out: the column is not text. Set the data type to Text on the Home tab, then try again.
  • Spaces still appear: they may be non-breaking spaces or other whitespace characters. Use a custom column with Text.Replace([Column], Character.FromNumber(160), " ") followed by Text.Trim.
  • Changes do not appear in the worksheet: click Close & Load and refresh the query. In Power BI, use Close & Apply and refresh the dataset.
  • Error in a custom column: check the column name is spelled exactly as in the header and that the formula uses square brackets, e.g. [CustomerName].
  • Trim removed spaces you wanted: delete the Trim step in Applied Steps or use Text.TrimStart/Text.TrimEnd instead.

Frequently Asked Questions

Does Power Query Trim remove all spaces?

No. It removes leading and trailing spaces only. Spaces between words remain.

Can I trim multiple columns at once?

Yes. Select several text columns with Ctrl+click, then use Transform > Trim.

What is the difference between Trim and Clean?

Trim removes spaces at the ends. Clean removes non-printable characters. You can use both on the same column.

Why is Trim not working on my column?

The column is probably not typed as Text. Change the data type to Text first.

Does Trim change the original data?

No. Power Query works on a copy; the source data stays unchanged until you load the results.

Can I use Trim in a custom column?

Yes. Enter Text.Trim([ColumnName]) in the Custom Column dialog.