List.Skip

D

T

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

What is List.Skip?

The List.Skip function is used to skip a specified number of elements from the beginning of a list and return the remaining elements. It takes two arguments: the first argument is the list, and the second argument is the number of elements to skip. The syntax for the List.Skip function is:


List.Skip(list as list, count as number) as list


Here, "list" is the list to be skipped, and "count" is the number of elements to skip.

How to Use List.Skip?

To use the List.Skip function in Power Query, you need to follow these steps:

1. Open Microsoft Excel or Microsoft Power BI.

2. Click on the "Data" tab.

3. Click on "From Table/Range" to import your data.

4. Click on "Transform Data" to open the Power Query Editor.

5. In the "Power Query Editor," select the column you want to skip elements from.

6. Click on the "Add Column" tab.

7. Click on "Custom Column" to open the "Add Custom Column" dialog box.

8. In the "Add Custom Column" dialog box, enter the formula for the List.Skip function.

9. Click "OK" to add the new column.

Here is an example of how to use the List.Skip function to skip the first two elements of a list:


let

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

skippedList = List.Skip(myList, 2)

in

skippedList


In this example, the List.Skip function skips the first two elements of the list [1, 2, 3, 4, 5] and returns the remaining elements [3, 4, 5].

The M Code Behind List.Skip

The M code behind the List.Skip function is relatively simple. Here is the M code for the List.Skip function:


(List as list, Count as number) as list =>

let

Size = List.Count(List),

EndIndex = Count + 1,

ItemsToSkip = if EndIndex >= Size then {} else List.Range(List, EndIndex, Size – EndIndex)

in

ItemsToSkip


The List.Skip function takes two arguments, "List" and "Count." The first line of the M code defines the function arguments and the function's return type.


(List as list, Count as number) as list =>


The second line of the M code defines two variables, "Size" and "EndIndex." The "Size" variable calculates the size of the list, and the "EndIndex" variable calculates the index of the element to start skipping.


let

Size = List.Count(List),

EndIndex = Count + 1,


The third line of the M code defines the "ItemsToSkip" variable. This variable uses the List.Range function to return a list of all elements starting from the "EndIndex" to the end of the list.


ItemsToSkip = if EndIndex >= Size then {} else List.Range(List, EndIndex, Size – EndIndex)


The last line of the M code returns the "ItemsToSkip" variable.


in

ItemsToSkip


In conclusion, the List.Skip function is a powerful tool in the Power Query language that allows you to skip a specified number of elements from the beginning of a list and return the remaining elements. The M code behind the List.Skip function is relatively simple, and understanding it can help you use the function more effectively. By following the steps outlined in this article, you should be able to use the List.Skip function in your Power Query projects with ease.

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)