Smart Systems for Home

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

Power Query List.ReplaceRange Function Guide

· 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 List.ReplaceRange function in Power Query (M language) is used to replace a specific contiguous segment of a list with another list. Unlike a simple value replacement, this function allows you to target a range based on a starting index and a count of items, effectively "cutting out" a piece of a list and "pasting in" a new set of values in its place. This is essential for cleaning structured lists where specific positions are known to be incorrect or need updating based on a set of replacement values.

What You'll Need

  • Microsoft Excel or Power BI Desktop with Power Query installed.
  • A data source that can be represented as a List (e.g., a column from a table converted to a list, or a manually created list).
  • Access to the Advanced Editor or a Custom Column formula box to enter the M code.

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 Your Target List: Ensure you have a list to work with. If your data is currently in a table, you can right-click a column and select Drill Down to convert that column into a List object. Power Query lists are zero-indexed, meaning the first item is at position 0.
  3. Determine the Replacement Parameters: Before writing the function, identify three critical values:
    • The Source List: The existing list you want to modify.
    • The Start Index: The position of the first item you want to remove (remember that the first item is 0).
    • The Count: How many consecutive items starting from the index should be removed.
    • The Replacement List: The new list of items to be inserted into the gap.
  4. Enter the Function Syntax: Navigate to the formula bar or the Advanced Editor. The basic syntax is: List.ReplaceRange(list as list, offset as number, count as number, replacement as list).
  5. Apply a Practical Example: Suppose you have a list {"Red", "Blue", "Green", "Yellow"} and you want to replace "Blue" and "Green" with "Orange" and "Purple".
    • The source list is {"Red", "Blue", "Green", "Yellow"}.
    • The offset is 1 (because "Blue" is the second item).
    • The count is 2 (because you are replacing two items: Blue and Green).
    • The replacement list is {"Orange", "Purple"}.
    • The formula would be: List.ReplaceRange({"Red", "Blue", "Green", "Yellow"}, 1, 2, {"Orange", "Purple"}).
  6. Execute and Verify: Press Enter. The result should be {"Red", "Orange", "Purple", "Yellow"}. If you are using this in a custom column, ensure the column type is set to List or expand the list to see the results in a table format.

Common Mistakes

One of the most frequent errors is forgetting that Power Query lists use zero-based indexing. Users coming from Excel formulas often start counting at 1. If you intend to replace the first item in a list, the offset must be 0. If you use 1, you will accidentally skip the first item and start the replacement from the second item.

Another common mistake is providing a single value instead of a list for the replacement argument. The function specifically requires a list as the fourth parameter. If you try to use List.ReplaceRange(myList, 1, 1, "NewValue"), Power Query will throw a type mismatch error because "NewValue" is a text string, not a list. To fix this, wrap the replacement value in curly brackets: {"NewValue"}.

Users also frequently confuse List.ReplaceRange with List.ReplaceValue. List.ReplaceValue searches for a specific value anywhere in the list and replaces it. List.ReplaceRange does not care what the values are; it only cares where they are located. If you use List.ReplaceRange and the data at those positions has changed, you will overwrite the wrong data.

Tips

To make your List.ReplaceRange logic dynamic, avoid hard-coding the offset and count. Instead, use List.PositionOf to find the starting index of a specific value. For example, if you want to replace a segment that starts with the word "Error", you can use List.PositionOf(myList, "Error") as the offset argument.

When replacing a range with a list of a different length, remember that List.ReplaceRange will effectively resize your list. If you remove 2 items but insert 5, your final list will be 3 items longer than the original. This is a powerful way to inject additional data points into a specific sequence without having to split and recombine the list manually using List.FirstN and List.LastN.

If you need to remove items without adding anything back, you can provide an empty list {} as the replacement argument. This transforms the function into a range-deletion tool, which is often cleaner than using complex filtering logic for specific index ranges.

Troubleshooting

If the function returns an error, first check the Data Types. Ensure the first and fourth arguments are lists (denoted by curly brackets {}) and the second and third arguments are numbers. If you see a "Expression.Error: We cannot convert the value... to type List" message, it is almost certainly because the replacement value is not wrapped in curly brackets.

If the function runs but replaces the wrong items, verify your Offset calculation. Print the list to a table and add an Index Column (from the Add Column tab) to visually confirm the zero-based index of the items you wish to target. If the offset is negative, the function will result in an error as indices must be non-negative.

If the list length is not what you expected after the operation, double-check the Count parameter. The count represents how many items are removed from the original list, not how many items are added from the replacement list. If you want to replace 3 items with 1 item, the count must be 3, and the replacement list must contain 1 item.

Frequently Asked Questions

Can I use List.ReplaceRange to replace only one item?

Yes. Set the count to 1 and provide a replacement list containing a single item, such as {"NewValue"}. This is a precise way to change a value at a known position regardless of what the current value is.

What happens if the count exceeds the remaining items in the list?

If the sum of the offset and the count is greater than the total number of items in the list, Power Query will simply replace everything from the offset to the end of the list. It will not throw an error, but it will truncate the original list at the end of the replacement.

Does this function modify the original list in place?

No. Like all M functions, List.ReplaceRange is functional and immutable. It returns a new list with the modifications applied. You must assign this result to a step name (e.g., ModifiedList = List.ReplaceRange(...)) to use it in subsequent steps.

Can I use this function inside a table column?

Yes, but only if the column contains lists. If you want to apply this to a text string, you should use Text.ReplaceRange instead. If you are trying to apply it to a table, you must first convert the target column to a list or use Table.TransformColumns to apply the list function to each row's list value.

Is there a performance impact when using this on very large lists?

For most standard datasets, the impact is negligible. However, because it creates a new list in memory, performing this operation thousands of times within a loop or a complex custom function on millions of rows can increase memory usage. In those cases, consider if a conditional column or a merge operation is more efficient.