Smart Systems for Home

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

Power Query Text.TrimEnd Function: Usage and Examples

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

The Text.TrimEnd function in Power Query is used to remove all trailing whitespace or a specific set of characters from the end of a text string. To use it, you wrap your column or text value in the function: Text.TrimEnd([ColumnName], "character"). If no second argument is provided, it defaults to removing all whitespace characters from the right side of the string.

What You'll Need

  • Microsoft Excel or Power BI Desktop with Power Query (Get & Transform) enabled.
  • A dataset containing text columns that require cleaning (e.g., data imported from CSVs, web scrapes, or legacy databases).
  • Basic familiarity with the Power Query Editor interface.

Step-by-Step

  1. Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
  2. Identify the Target Column: Locate the column containing the trailing characters or spaces you wish to remove. Note that Power Query is case-sensitive, so ensure you are targeting the correct data type (Text).
  3. Create a Custom Column: Since Text.TrimEnd is an M-formula function and not a direct button in the Transform menu (which only offers a general "Trim" that handles both start and end), you must use a custom column. Go to the Add Column tab on the ribbon and select Custom Column.
  4. Enter the Formula for Basic Whitespace Removal: In the Custom Column dialog box, give your new column a name (e.g., "Cleaned Text"). In the Custom column formula box, type: = Text.TrimEnd([YourColumnName]). Replace [YourColumnName] by double-clicking the column from the available columns list on the right.
  5. Enter the Formula for Specific Character Removal: If you need to remove characters other than spaces (such as trailing dots, commas, or specific symbols), add a second argument. For example, to remove trailing periods, use: = Text.TrimEnd([YourColumnName], ".").
  6. Handle Multiple Characters: If you need to remove any combination of several different characters from the end, list them all within the second argument's quotes. For example, = Text.TrimEnd([YourColumnName], ".,! ") will remove any combination of periods, commas, exclamation points, and spaces from the end of the string until it hits a character not in that list.
  7. Validate the Results: Compare the original column with the new custom column. Look closely at the end of the strings to ensure the unwanted characters are gone while the desired text remains intact.
  8. Clean Up the Query: Right-click the original messy column and select Remove. Then, right-click the new "Cleaned Text" column and select Rename to give it the original column's name.
  9. Apply Changes: Click File > Close & Apply to load the cleaned data back into your Excel sheet or Power BI model.

Common Mistakes

One of the most frequent errors is confusing Text.TrimEnd with Text.Trim. The standard Trim function removes whitespace from both the beginning and the end of a string. If you only want to preserve leading spaces for indentation reasons but remove trailing spaces for data integrity, Text.TrimEnd is the correct choice. Using the wrong one can lead to unintended formatting changes in your final report.

Another common mistake is ignoring case sensitivity. While Text.TrimEnd is primarily used for symbols and spaces, if you are trimming specific letters (e.g., removing a trailing "s" from a word), Power Query treats "S" and "s" as different characters. If your data has inconsistent casing, the function may leave some characters behind.

Users also frequently forget that Text.TrimEnd does not remove characters from the middle of the string. It only operates from the very last character moving backward. Once the function encounters a character that is NOT in your trim list, it stops immediately, even if there are more target characters further left in the string.

Lastly, attempting to use the function on a non-text column (such as a Number or Date type) will result in an Expression.Error. You must ensure the column is explicitly cast as Text before applying the function.

Tips

To maximize efficiency, combine Text.TrimEnd with Text.TrimStart or Text.Trim if you are performing a comprehensive data cleanup. If you find yourself creating many custom columns for different trim operations, consider creating a custom M-function in the Power Query editor that you can reuse across multiple queries.

When dealing with "invisible" characters that Text.TrimEnd doesn't seem to catch, you may be dealing with non-breaking spaces (ASCII 160) or carriage returns. In these cases, use the "Replace Values" feature first to replace the non-breaking space with a standard space, or use Text.Clean to remove non-printable characters before applying Text.TrimEnd.

If you are trimming a large volume of characters, remember that the second argument is a list of characters, not a specific phrase. If you need to remove a specific word from the end of a string, Text.TrimEnd is not the right tool; instead, use Text.RemoveRange or a combination of Text.Length and Text.Start.

Troubleshooting

If the function returns an error, the first thing to check is the data type of the column. Ensure the icon next to the column header is "ABC". If it is "1.2" or a calendar icon, right-click the column, select Change Type, and choose Text. This is the most common cause of failure.

If the trailing spaces are still there after applying the function, you are likely dealing with non-standard whitespace. To test this, create a custom column using = Text.Length([ColumnName]). If the length is longer than it appears visually, there are hidden characters. Use Text.Clean([ColumnName]) to remove control characters, then try Text.TrimEnd again.

If you see a "Token Literal Expected" error, check your syntax. Ensure that the characters you are trimming are enclosed in double quotes (e.g., ".") and that you have used a comma to separate the column reference from the trim characters. Ensure you are using the correct capitalization; text.trimend (lowercase) will not work because M is case-sensitive.

If the function is removing too many characters, remember that it removes all occurrences of any character in your list from the end. If you only want to remove a single trailing character regardless of what it is, Text.TrimEnd is not the correct tool; you should instead use Text.Start([ColumnName], Text.Length([ColumnName]) - 1).

Frequently Asked Questions

Does Text.TrimEnd remove spaces from the beginning of the text?

No, Text.TrimEnd only removes characters from the end (the right side) of the string. To remove spaces from the beginning, use Text.TrimStart, or use Text.Trim to remove them from both sides.

Can I remove multiple different characters at once?

Yes. By providing a string of characters in the second argument, such as "!?. ", Power Query will remove any of those characters it finds at the end of the string until it hits a character not included in that list.

Is Text.TrimEnd case-sensitive?

Yes, it is. If you specify "a" as the character to trim, it will not remove a trailing "A". You must include both uppercase and lowercase versions in the trim string if you want to remove both.

What is the difference between Text.TrimEnd and Text.Clean?

Text.TrimEnd removes specific characters or whitespace from the end of a string. Text.Clean removes non-printable control characters (like line breaks and tabs) from anywhere within the entire string.

Can I use Text.TrimEnd in a calculated column in Power BI DAX?

No, Text.TrimEnd is a Power Query (M) function. If you are working in DAX (the formula language for measures and calculated columns), you should use the TRIM function, though DAX's TRIM behaves differently by removing all leading and trailing spaces and reducing internal spaces to a single space.