List.RemoveFirstN

D

T

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

Understanding the List.RemoveFirstN Function

The List.RemoveFirstN function is a built-in function in Power Query that removes the first n elements from a list. The syntax for the List.RemoveFirstN function is:


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


Here, 'list' is the list from which the first n elements need to be removed, and 'count' is the number of elements to be removed. The function returns the remaining elements as a list.

For example, consider the following list:


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


If we want to remove the first 2 elements from this list, we can use the List.RemoveFirstN function as follows:


List.RemoveFirstN(list, 2)


This will return the following list:


{3, 4, 5}


The M Code Behind the List.RemoveFirstN Function

The M code behind the List.RemoveFirstN function is what makes the function work. The code for the List.RemoveFirstN function is:


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


This code takes two arguments - 'list' and 'count'. The list argument is the list from which the first n elements need to be removed, and the count argument is the number of elements to be removed.

The code then uses the List.Skip function to skip the first n elements of the list. The List.Skip function takes two arguments - the list and the number of elements to skip. In this case, the number of elements to skip is the 'count' argument passed to the List.RemoveFirstN function.

The List.Skip function returns a new list with the skipped elements removed, which is then returned as the output of the List.RemoveFirstN function.

Using the List.RemoveFirstN Function in Power Query

To use the List.RemoveFirstN function in Power Query, we need to have a list from which we want to remove the first n elements. We can create a list using the following M code:


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


We can then use the List.RemoveFirstN function to remove the first n elements from the list. For example, to remove the first 2 elements from the list, we can use the following M code:


List.RemoveFirstN(list, 2)


This will return the following list:


{3, 4, 5}


We can also use the List.RemoveFirstN function in Power Query queries to remove the first n elements from a list in a table. For example, consider the following table:

| Column1 |

|---------|

| 1 |

| 2 |

| 3 |

| 4 |

| 5 |

To remove the first 2 elements from the Column1 column in this table, we can use the following M code:


= Table.TransformColumns(Source,{{“Column1”, each List.RemoveFirstN(_, 2), type list}})


This will return the following table:

| Column1 |

|---------|

| 3 |

| 4 |

| 5 |

The List.RemoveFirstN function is an important function in Power Query that is used to remove the first n elements from a list. Understanding the M code behind this function is important for data analysts who work with Power Query. By using this function in Power Query queries, data analysts can extract only the necessary information from a list and process it more efficiently.

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)