Smart Systems for Home

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

Power Query Text.TrimStart: How to Use It

· 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

Text.TrimStart is a Power Query function that removes leading characters from the start of a text value. Used on its own it strips spaces from the beginning of a string, which fixes the very common problem of text that looks identical but does not match in joins, filters, or pivot tables because of an invisible leading space. You can also pass a second argument to remove other characters, such as zeros or dashes, instead of spaces. This guide walks through using it in both Excel and Power BI, since the Power Query editor is the same in both.

What You'll Need

  • Excel (2016 or later, or Microsoft 365) or Power BI Desktop — both include the Power Query editor
  • A table loaded into Power Query (select your data, then go to Data > From Table/Range in Excel, or Home > Transform Data in Power BI)
  • A text column you want to clean

Menu labels can differ slightly between Excel and Power BI versions, but the Power Query editor steps below are the same in both.

Step-by-Step

  1. Open the Power Query editor. In Excel: select your data, go to Data > From Table/Range, and confirm the dialog. In Power BI Desktop: click Home > Transform Data. The Power Query editor window opens.
  2. Select the text column you want to clean by clicking its header.
  3. Use the ribbon for the simple case. Go to Transform > Format > Trim. Note: the built-in Trim button actually trims both ends. To trim only the start, use a custom step as described next.
  4. Add a custom column (Add Column > Custom Column) if you want to keep the original values, or replace the column in place (Add Column vs Transform depends on your need — replacing in place keeps your table narrower).
  5. Enter the formula. To remove leading spaces only, type: Text.TrimStart([YourColumnName]) — replacing YourColumnName with your actual column name. To remove a different leading character, add it as a second argument: Text.TrimStart([Code], "0") removes leading zeros, and removes leading dashes.
  6. Check the preview. The new column should show the cleaned values with no leading spaces or specified characters.
  7. Remove the original column if you created a replacement: right-click the old column header and choose Remove.
  8. Load the results. Click Home > Close & Load in Excel, or Home > Close & Apply in Power BI, to send the cleaned data back to your workbook or model.
  9. Save your work so the query refreshes automatically next time your source data updates — the TrimStart step will reapply on every refresh.

Common Mistakes

  • Using the ribbon Trim button expecting start-only trimming. Excel's Transform > Format > Trim removes whitespace from both ends. Only the Text.TrimStart function trims the start alone.
  • Forgetting the square brackets around the column name. Text.TrimStart(MyColumn) fails; it must be Text.TrimStart([MyColumn]).
  • Passing multiple characters as separate arguments. The second argument is a single text value. To trim several characters, combine functions or use Text.TrimStart(Text.TrimStart([Col], "0"), "-"), or better, use Text.RemoveRange or Splitter functions for complex cases.
  • Applying it to a non-text column. If the column is typed as a number, Text.TrimStart throws an error. Change the column type to Text first (right-click the header > Change Type > Text).
  • Case sensitivity in the function name. M is case-sensitive: text.trimstart will not be recognized. Type it exactly as Text.TrimStart.

Tips

  • Use Text.TrimEnd for trailing characters and Text.Trim for both ends — they share the same syntax.
  • To trim leading spaces from every text column at once, use Table.TransformColumns with a list of column names, or select multiple columns and apply a transform from the ribbon.
  • Combine with Text.Upper or Text.Lower in the same custom column when preparing keys for a merge, for example: Text.Upper(Text.TrimStart([ID])).
  • Non-breaking spaces from web or PDF sources are not removed by Text.TrimStart. Clean those with Text.Replace([Col], "#(00A0)", " ") first, then trim.
  • Click the fx button next to the formula bar to add a clean custom step instead of editing an existing one — it keeps your applied steps readable.

Troubleshooting

  • Expression.Error: We cannot convert the value to type Text. The column is not text. Change its type to Text before applying the function.
  • Token Comma expected / Token RightParen expected. Check your syntax: column reference in brackets, quotes around any literal character, and parentheses closed.
  • Leading spaces still there after loading. The characters may be non-breaking spaces or tabs. Inspect the value in the formula bar with Text.ToList([Col]) or replace character 160 as shown in Tips.
  • The formula works but the column still does not match in a merge. Check for trailing spaces too — use Text.Trim instead, or trim both ends before merging.
  • The step disappears on refresh. You likely typed into the formula bar of an existing step and overwrote it. Undo (Ctrl+Z in the editor) or delete the step and re-add it via a custom column.

Frequently Asked Questions

What is the difference between Text.Trim and Text.TrimStart?

Text.Trim removes characters from both the beginning and end of a string; Text.TrimStart removes them only from the beginning. Text.TrimEnd handles only the end.

Can Text.TrimStart remove leading zeros?

Yes. Use Text.TrimStart([Column], "0"). Note this removes all leading zeros, so a value like 007 becomes 7 — convert to a number type afterwards if needed.

Does Text.TrimStart remove tabs and line breaks?

Yes. With no second argument, it removes all leading whitespace, including spaces, tabs, and line breaks. Non-breaking spaces are the exception.

Can I use it on multiple columns at once?

Not directly — it works on one text value at a time. Wrap it in Table.TransformColumns or List.Transform to apply it across several columns.

Does it change the original data?

No. Power Query never modifies the source. The trimming is recorded as a step and applied whenever the query refreshes.