List.Last

List.Last

D

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

Introduction to List.Last Function

The List.Last function is used to return the last item in a list. It takes a list as an argument and returns the last item in the list. The syntax for the List.Last function is as follows:


List.Last(list as list) as any


The argument "list as list" specifies the list you want to return the last item from. The function returns the last item in the list as "any".

The M Code Behind List.Last

To understand the M code behind List.Last, we need to understand some basic concepts of M language. M is a functional language that is used to define custom functions. It is used in Power Query to manipulate data and perform various transformations.

The M code behind List.Last is as follows:


(list as list) =>

let

numberOfItems = List.Count(list),

result = List.Buffer(list){numberOfItems-1}

in

result


Let's break down this code to understand how it works.

The Arguments

The function takes only one argument, which is "list" as list. This specifies the list that we want to return the last item from.

The Variables

The code defines two variables:

- "numberOfItems": This variable calculates the number of items in the list using the List.Count function.

- "result": This variable uses the List.Buffer and List.{index} functions to return the last item in the list.

List.Buffer Function

The List.Buffer function is used to store a list in memory. This is important because it allows us to reference the list multiple times without recalculating it. In the case of List.Last, we use List.Buffer to store the list so that we can reference it again in the List.{index} function without recalculating it.

List.{index} Function

The List.{index} function is used to return a specific item from a list. In the case of List.Last, we use List.{numberOfItems-1} to return the last item in the list.

The reason we use "numberOfItems-1" instead of "numberOfItems" is because the List.{index} function is zero-indexed, meaning that the first item in the list is at index 0, the second item is at index 1, and so on. Therefore, the last item in the list is at index "numberOfItems-1".

The Result

The code returns the result variable as the output of the function.

The List.Last function is a useful function in Power Query that can be used to return the last item in a list. The M code behind the function uses the List.Buffer and List.{index} functions to calculate the last item in the list. Understanding the M code behind List.Last is important for anyone who wants to create custom functions in Power Query. By understanding the M code, you can modify the function to suit your own needs and create your own custom functions.

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: