Power Query List.InsertRange: Step-by-Step Guide
In this article
List.InsertRange adds a list of new values into an existing list at a position you choose. The function takes three arguments: the original list, the zero-based index where the new values should start, and the list of values to insert. It returns a new list and leaves the original unchanged. You can use it in the Power Query Editor or in a custom column formula.
What You'll Need
- Power BI Desktop, Excel with Power Query, or Analysis Services with Power Query — any version that includes the Power Query Editor.
- A query with at least one list value, or the ability to create one. Lists appear when you expand a column of lists, split by delimiter into a list, or use functions like List.Numbers.
- Basic familiarity with the Power Query Editor: the formula bar, the Applied Steps pane, and the Advanced Editor.
Step-by-Step
- Open the Power Query Editor. In Excel, go to Data > Get Data > Launch Power Query Editor. In Power BI Desktop, click Transform Data on the Home ribbon. Menu labels can differ slightly by version, device, or region.
- Create or select a list. A list is a single-column set of values enclosed in curly braces, such as {1, 2, 3}. You can create one by typing a list directly into a blank query, by using List.Numbers(1, 5), or by referencing a column that contains lists. If you are working with a table, you can convert a column to a list with the drill-down button or by using the formula bar.
- Open the Advanced Editor. On the Home tab, click Advanced Editor. This shows the M code for your query. If you prefer not to use the Advanced Editor, you can add a step through the formula bar by clicking the fx button next to the formula bar.
- Write the List.InsertRange formula. The syntax is List.InsertRange(list, index, values). For example, to insert {4, 5} into {1, 2, 3} at position 3 (the end), use: List.InsertRange({1, 2, 3}, 3, {4, 5}). The index is zero-based, so 0 inserts at the beginning, 1 inserts after the first item, and so on. The third argument must be a list, even if you are inserting a single value: use {99} rather than 99.
- Add the step to your query. In the Advanced Editor, add a new line before the final in statement, for example: InsertedList = List.InsertRange(Source, 2, {"New", "Values"}), and then change the final in to return InsertedList. If you are using the formula bar, click fx, type the formula, and press Enter. Power Query will create a new step.
- Check the result. The step preview should show the original items with the new values inserted at the index you specified. If the index is larger than the number of items in the original list, List.InsertRange appends the new values at the end. If the index is negative, the function returns an error.
- Use the result in your query. You can expand the list back into a table, use it in a custom column, or pass it to another function. For example, to turn the list into a table, use Table.FromList(InsertedList, Splitter.SplitByNothing()).
Common Mistakes
- Using a one-based index. Power Query lists are zero-based. Inserting at index 1 puts the new values after the first item, not before it. To insert at the very beginning, use index 0.
- Passing a single value instead of a list. The third argument must be a list. List.InsertRange({1,2}, 1, 3) fails because 3 is not a list. Use {3} instead.
- Assuming the original list changes. List.InsertRange returns a new list. If you do not assign the result to a step or use it in a subsequent expression, the insertion has no effect.
- Using a negative index. Negative indexes are not supported and produce an error. If you need to insert relative to the end, calculate the index using List.Count.
- Forgetting that the index can exceed the list length. If the index is greater than the number of items, the new values are appended at the end. This is not an error, but it may not be what you intended.
Tips
- Use List.Count to calculate a dynamic index. For example, to append at the end, use List.Count(originalList).
- To insert multiple separate lists, nest List.InsertRange calls or combine the lists first with List.Combine.
- If you are inserting into a list of records, make sure the new records have the same field names and data types as the existing ones to avoid errors when expanding.
- Use the formula bar to test small expressions quickly. You can create a blank query, type =List.InsertRange({1,2,3}, 1, {9}), and see the result immediately.
- Remember that list indexes are zero-based throughout Power Query, including functions like List.First, List.Last, and List.Range.
Troubleshooting
- Error: "We cannot convert a value of type Number to type List." You passed a single value as the third argument. Wrap it in curly braces: {value}.
- Error: "Expression.Error: The index is out of range." This can happen if you use a negative index or if the index is not a number. Check that the index is a non-negative integer.
- The new values do not appear. Make sure you assigned the result to a step and that the final in statement returns that step. In the Advanced Editor, the last line should be the name of the step that contains the List.InsertRange call.
- The list looks different than expected. Remember that the index is zero-based. If you inserted at index 1, the new values appear after the first item. To insert before the first item, use index 0.
- The query is slow with large lists. List.InsertRange creates a new list, which can be expensive for very large lists. If performance matters, consider building the list in a different way, such as using Table.InsertRows on a table instead.
Frequently Asked Questions
What is the difference between List.InsertRange and List.Combine?
List.Combine joins two lists end-to-end. List.InsertRange places a list at a specific position inside another list. Use List.InsertRange when you need control over where the new values go.
Can I insert a single value instead of a list?
No. The third argument must be a list. To insert a single value, wrap it in curly braces: {value}.
What happens if the index is larger than the list length?
The new values are appended at the end of the list. No error is raised.
Is the index zero-based or one-based?
Zero-based. Index 0 inserts at the beginning, index 1 inserts after the first item, and so on.
Can I use List.InsertRange with a list of records?
Yes, as long as the new records have the same structure. If the records have different fields, expanding the list later may produce nulls or errors.
Does List.InsertRange modify the original list?
No. It returns a new list. The original list remains unchanged.