List.InsertRange

List.InsertRange

D

The M Code Behind the Power Query M function List.InsertRange

What is the List.InsertRange Function?

The List.InsertRange function in Power Query is used to insert a range of values into a list at a specified index. It takes three arguments:

– The list to insert the range into

– The index at which to insert the range

– The range of values to insert

Here is an example of how to use the List.InsertRange function in Power Query:


let

myList = {1, 2, 3, 4, 5},

myRange = {10, 11, 12},

insertIndex = 2,

outputList = List.InsertRange(myList, insertIndex, myRange)

in

outputList


In this example, we have a list called `myList` that contains the values 1 through 5. We also have a range of values called `myRange` that contains the values 10 through 12. We want to insert `myRange` into `myList` at index 2. The `List.InsertRange` function is used to accomplish this. The resulting list will be `{1, 2, 10, 11, 12, 3, 4, 5}`.

Understanding the M Code Behind List.InsertRange

The M code behind the List.InsertRange function is relatively simple. Here is the basic syntax of the function:


List.InsertRange(list as list, index as number, values as list) as list


The first argument (`list`) is the list that the range of values will be inserted into. The second argument (`index`) is the index at which the range of values will be inserted. The third argument (`values`) is the range of values to be inserted.

Let's break down the M code for the `List.InsertRange` function:


(List.InsertRange(list as list, index as number, values as list) as list) =>

let

start = List.FirstN(list, index),

end = List.Skip(list, index),

result = start & values & end

in

result


The first line of the code defines the function and its arguments. The second line uses the `let` keyword to define some variables that will be used in the function. The `start` variable uses the `List.FirstN` function to get the first `index` elements of the `list`. The `end` variable uses the `List.Skip` function to get all elements of the `list` after the `index`. The `result` variable concatenates `start`, `values`, and `end` using the `&` operator.

Tips for Using List.InsertRange

Here are some tips for using the List.InsertRange function in Power Query:

- Make sure that the index you specify is within the bounds of the list. Otherwise, you will get an error.

- You can insert a range of values into a list multiple times by calling the List.InsertRange function multiple times. Just make sure to adjust the index accordingly.

- If you want to insert a single value into a list, you can use the List.Insert function instead.

In this article, we have explored the M code behind the List.InsertRange function in Power Query. We have learned how to use the function to insert a range of values into a list at a specified index. We have also provided some tips for using the function effectively. With this knowledge, you should be able to use the List.InsertRange function to manipulate and transform your data in Power Query.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

Subject

Your Name (required)

Company/Organisation

Email (required)

Telephone

Training Course(s)

Your Message

Upload Example Document(s) (Zip multiple files)

Copyright, G Com Solutions Ltd, 2024.
Tower Bridge Business Centre, 46-48 East Smithfield, London E1W 1AW
0800 998 9248   |   9:00 a.m. till 5:30 p.m.

Connect With Me: