Power Query Replacer.ReplaceText Function Guide
In this article
The Replacer.ReplaceText function in Power Query is used to find a specific sequence of characters within a text string and replace them with a different sequence of characters. It is the underlying engine for the "Replace Values" dialogue in the Power Query Editor, allowing you to clean data, standardize naming conventions, or remove unwanted characters from your columns. To use it, you need a text-based column in a Power BI or Excel Power Query environment.
What You'll Need
- Microsoft Excel or Power BI Desktop installed.
- A dataset loaded into the Power Query Editor.
- A column formatted as "Text" (the function will return an error if applied to numbers, dates, or binary data).
Step-by-Step
- 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.
- Select Your Column: Click on the header of the column containing the text you wish to modify. This ensures the function targets the correct data series.
- Use the Interface Method (Standard): Right-click the column header and select Replace Values. In the dialog box, enter the "Value To Find" (the text you want to remove) and the "Replace With" value (the new text). Click OK. Power Query automatically generates the
Table.ReplaceValuewrapper which utilizesReplacer.ReplaceText. - Use the Manual M-Code Method (Advanced): If you need more control or are writing a custom column, go to the Add Column tab and select Custom Column.
- Enter the Function Syntax: In the Custom Column formula box, you can use the function within a transformation. However, because
Replacer.ReplaceTextis a function used as an argument forTable.ReplaceValue, the syntax typically looks like this in the formula bar:= Table.ReplaceValue(PreviousStepName, "OldText", "NewText", Replacer.ReplaceText, {"ColumnName"}). - Define the Arguments:
- PreviousStepName: This is the name of the step immediately preceding this one in the Applied Steps pane (e.g., "Changed Type").
- OldText: The exact string you are searching for, enclosed in double quotes.
- NewText: The string you want to insert, enclosed in double quotes. Use two double quotes ("") for an empty string if you want to delete the text.
- Replacer.ReplaceText: This tells Power Query to look for the text anywhere within the cell, rather than requiring the entire cell to match the "OldText".
- ColumnName: The name of the column to be transformed, placed inside curly brackets {}.
- Verify the Results: Look at the preview window to ensure the replacements occurred as expected. If the text didn't change, check for trailing spaces or case-sensitivity issues.
- Apply Changes: Click Close & Apply in the top left corner to load the cleaned data into your data model or worksheet.
Common Mistakes
The most frequent error when using Replacer.ReplaceText is ignoring case sensitivity. Power Query is case-sensitive by default. If you search for "apple" but the data contains "Apple", the function will ignore those cells entirely. To fix this, you must either perform two separate replace operations or transform the entire column to lowercase using Transform > Format > lowercase before running the replacement.
Another common mistake is attempting to use the function on non-text data types. If your column is set to "Decimal Number" or "Date", the function will fail or return an error. You must change the data type to Text using the icon next to the column header before applying the replacement.
Users also frequently forget the curly brackets around the column name in the M-code. The Table.ReplaceValue function expects a list of columns to operate on, and in M, lists are defined by curly brackets {}. Writing "ColumnName" instead of {"ColumnName"} will result in a syntax error.
Lastly, there is the issue of partial matches. Because Replacer.ReplaceText looks for a substring, replacing "cat" with "dog" will also change "category" to "dogegory". If you only want to replace whole words, you cannot use this function alone; you would need to split the column by delimiter, replace the value, and then merge the columns back together.
Tips
To perform a bulk replacement of multiple different terms, do not create ten separate "Replace Value" steps. This can slow down the refresh speed of your report. Instead, create a separate "Mapping Table" with two columns: "OldValue" and "NewValue". You can then create a custom function or use a loop to iterate through the mapping table and apply the replacements in a single step.
When you want to remove a character entirely, leave the "Replace With" field completely blank. This effectively tells Power Query to replace the found text with nothing, which is the standard way to strip out symbols like hashtags, currency signs, or asterisks from a dataset.
If you are dealing with hidden characters (like non-breaking spaces from web imports), the standard "Replace Values" box might not work because you cannot type the hidden character. In this case, use the formula bar and enter the Unicode character code, such as Character.FromNumber(160), to target the invisible character precisely.
Combine Replacer.ReplaceText with Trim and Clean. Before replacing text, use Transform > Format > Trim to remove leading and trailing whitespace, and Transform > Format > Clean to remove non-printable characters. This ensures that your "Value to Find" matches the data exactly without being blocked by an invisible space.
Troubleshooting
If the function is not replacing the text as expected, first check the Data Type. Ensure the column is explicitly set to Text. If it is set to "Any", the replacer may behave inconsistently.
If you see an Expression.Error, check your M-code syntax in the formula bar. Ensure every opening quote has a closing quote and that the step name refers to the exact name of the previous step. If the previous step was renamed to "Filtered Rows", your formula must start with = Table.ReplaceValue(#"Filtered Rows", .... Note the hashtag and quotes used for step names that contain spaces.
If the replacement is happening in places you didn't intend (the "category" to "dogegory" problem), you must refine your search term. Try adding a space before or after the word in the "Value to Find" box (e.g., " cat " instead of "cat"). This limits the replacement to standalone words, though it may miss words at the very beginning or end of a sentence.
If the performance is lagging after many replace steps, consider using a conditional column or a custom M-function to handle all replacements in one pass. Each "Replace Value" step creates a new virtual table in memory; reducing the number of steps optimizes the query refresh time.
Frequently Asked Questions
Is Replacer.ReplaceText case-sensitive?
Yes, it is strictly case-sensitive. "TEXT" and "text" are treated as different values. You must match the case exactly or normalize the column case first.
Can I use this to replace numbers?
Not directly. The function is designed for text. To replace numbers, you must first convert the column to Text, perform the replacement, and then convert it back to a Numeric type.
How do I replace multiple different values at once?
The standard function only handles one pair of values per step. For multiple values, you can stack multiple Replace Value steps or use a mapping table with a custom function.
What is the difference between Replacer.ReplaceText and Replacer.ReplaceValue?
Replacer.ReplaceValue replaces the entire cell content only if it matches the search term exactly. Replacer.ReplaceText replaces any occurrence of the search term found anywhere within the cell.
Can I use wildcards with this function?
No, Replacer.ReplaceText does not support wildcards like "*" or "?". For pattern-based replacement, you would need to use Text.Select or integrate a regular expression via a custom R or Python script within Power Query.
How do I replace a line break?
You cannot type a line break into the Replace Values dialog. Instead, use the M-code formula bar and replace the text with Character.FromNumber(10) for line feeds or Character.FromNumber(13) for carriage returns.