List.Skip

Updated on

List.Skip is a Power Query M function that skips a specified number of items in a list or items until a specified condition is met. The function returns a modified list with the specified items skipped.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

List.Skip(
   list as list,
   optional countOrCondition as any,
) as list

Description

The List.Skip function creates a new list by removing elements from an existing one. By default, it removes the first element, but you can customize how many elements to skip or even specify a condition to determine which elements get left out. If you feed it an empty list, it will simply return another empty list.

In case you provide the optional countOrCondition argument, the function supports the following input:

  • Number Specified: If you provide a specific number, the function will skip that many items from the beginning of the list.
  • Condition Specified: If you give a condition, the function skips all elements that meet this condition. As soon as an element fails to meet the condition, the function stops considering any more items for exclusion.
  • Null Parameter: If this parameter is left null or unspecified, the function reverts to its default behavior, which is to skip just the first item in the list.

Examples

The List.Skip function can be used in several ways.

Basic Usage: Skipping the First Element

Let’s start simple. Imagine you have a list consisting of the numbers 1 through 5 and you want to create a new list that skips the first number. Here’s how to do it:

// Output: { 2, 3, 4, 5 }
List.Skip( { 1, 2, 3, 4, 5 } )

Just like that, the new list starts from 2, leaving out the first number 1.

Handling Empty Lists

But what if you have an empty list? Don’t worry, the function has you covered. When the list is empty, the function will simply return another empty list.

// Output: { }
List.Skip( { } )

Skipping N number of Items using a Count

Things start getting more interesting when you want to skip more than just one item from the list. For instance, if you’d like to remove the first three numbers from the list {1, 2, 3, 4, 5}, you can specify the count of items to skip like so:

// Output: { 4, 5 }
List.Skip( { 1, 2, 3, 4, 5 }, 3 )

And if you want to keep all the values intact, simply specify zero as the count.

// Output: { 1, 2, 3 }
List.Skip( { 1, 2, 3 }, 0 )

Adding Conditions: More Control Over Skipping

Now, let’s say you want to skip elements based on certain conditions, like their value. The countOrCondition argument can also take a condition, making it more useful.

For example, to create a list that only contains items less than or equal to 3, you would do:

// Output: { 2, 6, 1 }
List.Skip( { 5, 4, 2, 6, 1 }, each _ > 3 )

Alternatively, if you want to skip odd numbers, you could write:

// Output: { 4, 5, 6 }
List.Skip( { 1, 3, 4, 5, 6 }, each Number.IsOdd( _ ) )

If your list contains a mix of positive, negative, and zero values and you’d like to skip the initial positive values, you can use:

// Output: { -1, -2 }
 List.Skip( {4, 3, 2, -1, -2}, each _ >= 0 )

And guess what? List.Skip isn’t limited to just numbers. You can use this function with text values too. For instance, if you have a list of fruits and want to exclude the ones with names shorter than 6 characters, you can use the Text.Length function as your condition.

// Output: { "banana", "cherry", "date" }
List.Skip( {"apple", "banana", "cherry", "date"}, each Text.Length(_) < 6 )

To conclude, the List.Skip function allows you to manipulate lists in a variety of ways. Whether you’re skipping elements based on their position in the list or according to a specific condition, this function supports both.

Learn more about List.Skip in the following articles:

Other functions related to List.Skip are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/list-skip

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy