Text.ReplaceRange replaces a chunk of text at a specific position with new text. The syntax is Text.ReplaceRange(text as nullable text, offset as number, count as number, newText as text) as nullable text. It starts at the zero-based offset, removes count characters, and inserts newText in their place. This is useful for fixing a known substring at a known position, such as changing the third character of a product code or swapping a segment in a fixed-width ID. You do not need any add-ins or special permissions, just Power Query in Excel, Power BI Desktop, or Dataflows.
What You'll Need
- Power Query Editor open in Excel (Data > Get Data > Launch Power Query Editor) or Power BI Desktop (Home > Transform data). Labels can differ slightly by version and region.
- A query with a text column you want to modify, or a blank query to test the function.
- Basic familiarity with adding a custom column or editing the formula bar.
Step-by-Step
- 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 editor. If you are testing, create a blank query with Home > New Source > Blank Query.
- Select the text column. Click the column header that contains the text you want to change. If you are testing in a blank query, you can type a value directly into the formula bar later.
- Add a custom column. Go to Add Column > Custom Column. Give it a name such as "Fixed Code". In the formula box, start typing
Text.ReplaceRange(and Power Query will show the argument list. - Enter the arguments. The first argument is the text value, usually the column name in square brackets, for example
[ProductCode]. The second is the zero-based offset, the third is the number of characters to remove, and the fourth is the replacement text in double quotes. For example:Text.ReplaceRange([ProductCode], 2, 3, "XYZ"). This keeps the first two characters, removes the next three, and inserts XYZ. - Check the preview. The custom column dialog shows a preview of the result. If it shows an error, verify that the offset and count are numbers and that the replacement text is in quotes.
- Click OK. The new column appears in the query. You can rename it by double-clicking the header.
- Handle nulls if needed. If the source column can be null, Text.ReplaceRange returns null. Wrap it with
if [ProductCode] = null then null else Text.ReplaceRange(...)to avoid unexpected nulls. - Apply and load. Go to Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to send the result to your worksheet or data model.
If you prefer to edit the formula directly, you can also use the formula bar on an existing step. For example, replace a step that uses Text.Replace with a Text.ReplaceRange call. The function is case-sensitive for the text you insert, but the function name itself is not case-sensitive in Power Query M.
Common Mistakes
- Using a one-based offset. Power Query offsets start at zero. To replace the first character, use offset 0, not 1.
- Confusing count with end position. The third argument is the number of characters to remove, not the index where removal stops. If you want to remove characters 3 through 5, offset is 2 and count is 3.
- Forgetting quotes around the replacement text. The fourth argument must be a text literal in double quotes, or a text expression. A bare word will cause an error.
- Assuming it searches for text. Text.ReplaceRange works by position, not by matching content. If you need to find and replace a substring anywhere in the text, use Text.Replace instead.
- Ignoring nulls. Null input returns null output. If your column has blanks, handle them explicitly.
- Offsets beyond the text length. If the offset is greater than the length of the text, the function returns the original text unchanged. If count extends past the end, it removes only the characters that exist and appends newText.
Tips
- Use
Text.Length([Column])to check the length before choosing an offset, especially for variable-length codes. - Combine with
Text.PositionOfto find a delimiter and then replace a segment relative to it, rather than hard-coding an offset. - Test on a small sample query first. Duplicate the query (right-click > Duplicate) so you can compare before and after.
- If you need to replace multiple fixed positions, chain the function calls or use List.Accumulate for a cleaner formula.
- Remember that Power Query is case-sensitive in text comparisons by default, but Text.ReplaceRange does not compare text, so case only matters for the replacement string you provide.
Troubleshooting
If the custom column shows an error, click the error cell to see the message. A common message is "We cannot convert the value null to type Text" or "Token Comma expected". The first usually means a null in the source; add a null check. The second usually means a missing quote or parenthesis in the formula. If the result looks unchanged, check whether your offset is larger than the text length; in that case the function returns the original text. If the wrong characters are removed, recount from zero and confirm the count is the number of characters to delete, not the end index. If the function is not recognized, confirm you are in the Power Query Editor and not in a DAX formula bar; Text.ReplaceRange is an M function, not a DAX function. If the query is slow on large tables, consider whether Text.Replace or a conditional column would be simpler; Text.ReplaceRange is not a performance bottleneck by itself, but row-by-row custom columns can add overhead.
If that did not work, try recreating the step from scratch in a new custom column with a literal text value instead of a column reference, for example Text.ReplaceRange("ABCDE", 1, 2, "ZZ"). If that returns "AZZDE", the function is working and the issue is in your column reference or data type. Also check that the column is type text; if it is type any, use Text.From first.
Frequently Asked Questions
What is the difference between Text.ReplaceRange and Text.Replace?
Text.ReplaceRange replaces characters at a fixed position and length. Text.Replace searches for a substring anywhere in the text and replaces all or some occurrences. Use ReplaceRange for fixed-width data and Replace for pattern or content matching.
Is the offset zero-based or one-based?
It is zero-based. Offset 0 is the first character. This is different from some other languages and from Excel's MID function, which is one-based.
What happens if count is zero?
If count is 0, no characters are removed and newText is inserted at the offset. This effectively inserts text without deleting anything.
Can I use Text.ReplaceRange on a column with null values?
Yes, but the result will be null for null inputs. Wrap the call in a conditional if you want a different fallback, such as an empty string.
Does Text.ReplaceRange work in Power BI measures?
No. It is a Power Query M function used during data transformation. In DAX measures, use SUBSTITUTE or REPLACE instead.
What if the offset is negative?
A negative offset causes an error. Ensure the offset is zero or a positive number within the text length.