Excel Power Query: Trim Characters the Right Way
In this article
Power Query's Text.Trim function removes leading and trailing whitespace, not arbitrary characters. To trim a specific character from the start or end of a text value, use Text.TrimStart or Text.TrimEnd with a list of characters, or Text.Trim with a second argument. The quickest route is to select your column, then use Transform > Format > Trim to clean spaces, or add a custom column with a formula such as Text.TrimEnd([Column], {"*"}) to strip a specific character. This guide walks through both the menu method and the custom formula method, plus how to handle case sensitivity and mixed characters.
What You'll Need
- Excel for Microsoft 365, Excel 2021, or Excel 2019 with Power Query built in (Data > Get & Transform Data). Power Query is also available in Excel 2016 as an add-in, but menu labels differ slightly.
- A table or range of text data loaded into Power Query. If your data is already in a worksheet, select it and use Data > From Table/Range.
- Basic familiarity with the Power Query Editor: the ribbon, the Queries pane, and the Applied Steps list on the right.
Menu labels in Power Query can differ slightly by Excel version, device, and region. The steps below use the desktop Power Query Editor in Excel for Microsoft 365; Power BI Desktop uses the same engine with a similar ribbon.
Step-by-Step
- Load your data into Power Query. Select your table or range, then go to Data > From Table/Range. If the range has headers, tick My table has headers in the dialog. The Power Query Editor opens in a separate window.
- Select the column to clean. Click the column header of the text column that contains the characters you want to remove. To clean several columns at once, hold Ctrl and click each header.
- Trim whitespace with the built-in command. With the column selected, go to Transform > Format > Trim. This removes leading and trailing spaces only. It does not remove tabs, line breaks, or punctuation. The step appears in Applied Steps as Trimmed Text.
- Trim a specific character with a custom column (recommended for punctuation). Go to Add Column > Custom Column. Give it a name such as CleanedText. In the formula box, enter a formula using Text.TrimStart or Text.TrimEnd with a list of characters. For example, to remove a leading asterisk:
Text.TrimStart([Column], {"*"}). To remove a trailing percent sign:Text.TrimEnd([Column], {"%"}). To remove both a leading and trailing character in one go, useText.Trim([Column], {"*", " "}). Click OK. - Trim multiple different characters. List each character inside the curly braces, separated by commas:
Text.TrimEnd([Column], {"*", "#", " "}). The order in the list does not matter; Power Query removes any of the listed characters from the end until it hits one that is not in the list. - Trim characters from both ends in one step. Use Text.Trim with a character list as the second argument:
Text.Trim([Column], {"*", " "}). Without the second argument, Text.Trim removes only whitespace. - Check the result. Scroll through the new column. If the original column is still visible, compare a few rows side by side. If the new column looks right, you can remove the original column by right-clicking its header and choosing Remove.
- Load the cleaned data. Go to Home > Close & Load. Choose Close & Load To if you want the result in a specific worksheet or as a connection only. The cleaned table appears in Excel, and the query is saved so you can refresh it later.
Common Mistakes
- Expecting Text.Trim to remove punctuation. Text.Trim with no second argument removes only whitespace. Punctuation and other characters need Text.TrimStart, Text.TrimEnd, or Text.Trim with a character list.
- Forgetting that character lists are case-sensitive. Text.TrimStart([Column], {"A"}) removes uppercase A but not lowercase a. Include both if needed: {"A", "a"}.
- Using the wrong function for the job. Text.TrimStart removes only from the beginning; Text.TrimEnd only from the end. Using Text.TrimStart when the character is at the end will do nothing.
- Overwriting the original column by mistake. If you use Transform instead of Add Column, the original values are replaced. That is fine if you have a backup, but add a custom column first if you want to compare.
- Assuming the step will update automatically. Power Query steps apply to the data as loaded. If the source data changes, use Home > Refresh to re-run the query and re-apply the trim.
- Typing the formula in the wrong syntax. Character lists use curly braces and double quotes: {"*"}. Using parentheses or single quotes causes an error.
Tips
- Use Add Column > Custom Column rather than Transform when you want to keep the original values for comparison.
- To remove all non-alphanumeric characters from the middle of a string as well, combine Text.Select with a character range, for example
Text.Select([Column], {"a".."z", "A".."Z", "0".."9"}). This keeps only letters and digits. - If you only need to remove spaces, the Transform > Format > Trim command is faster than writing a formula.
- Rename your custom column immediately so the Applied Steps list stays readable.
- Test on a small sample first: duplicate the query (right-click the query in the Queries pane > Duplicate) and experiment there before changing the main query.
- If your data contains line breaks or tabs, Text.Trim with no argument may not remove them. Use Text.Clean first to strip non-printing characters, then trim.
Troubleshooting
- The formula returns an error. Check that the column name in square brackets matches exactly, including spaces. Column names with spaces need
[#"Column Name"]. Check that the character list uses curly braces and double quotes. - Nothing changes after the trim step. Confirm the character you are trimming is actually at the start or end of the value. A character in the middle of the string is not removed by TrimStart or TrimEnd. Use Text.Replace or Text.Select for middle characters.
- The trim removes too much. Text.TrimStart and Text.TrimEnd remove every listed character until they hit one that is not in the list. If your data has multiple leading asterisks, all of them are removed. If you need to remove only one, use Text.Range or a more specific formula.
- The query is slow on large data. Custom columns with Text.Select or complex formulas can be slower than built-in transforms. Apply the trim after filtering out unneeded rows, and avoid adding many custom columns if performance matters.
- Refresh does not update the cleaned column. Check that the query is loaded as a table, not just a connection. Go to Data > Queries & Connections, right-click the query, and choose Refresh. If the source range has changed size, edit the source step in Power Query.
- The Applied Steps list shows an error on a later step. Click the step with the error to see which row and column caused it. Often a null value or a number in a text column triggers the error; use Table.TransformColumnTypes to set the column to Text before trimming.
Frequently Asked Questions
Does Text.Trim remove punctuation?
No. Text.Trim with no second argument removes only leading and trailing whitespace. To remove punctuation, supply a character list as the second argument or use Text.TrimStart / Text.TrimEnd.
Can I trim different characters from the start and end?
Yes. Use Text.TrimStart with one list and Text.TrimEnd with another, or nest them: Text.TrimEnd(Text.TrimStart([Column], {"*"}), {"%"}).
Is the character list case-sensitive?
Yes. Include both uppercase and lowercase versions of a letter if you need to remove either case.
What is the difference between Trim and Clean in Power Query?
Text.Trim removes leading and trailing whitespace. Text.Clean removes non-printing characters such as line breaks and tabs from the entire string. Use Clean first if your data has hidden characters, then Trim.
Can I trim characters in Power Query without writing a formula?
For whitespace only, yes: select the column and use Transform > Format > Trim. For specific characters, a custom column formula is required because the ribbon has no built-in command for arbitrary character trimming.
Will the trim apply to new rows added later?
Yes, as long as the new rows are within the source range or table that the query points to. Refresh the query after adding rows, and if the source range has grown, update the source step in Power Query.