List.Last is a Power Query M function that retrieves the last item in a list, or an optional default value if the list is empty. The function returns the last item or null if the list is empty and no default value is specified.
Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365
Syntax
List.Last(
list as list,
optional defaultValue as any,
) as any
Description
The List.Last function retrieves the final element in a list. If the list is empty, it will return null by default. Nonetheless, an optional second parameter allows users to set a default return value for empty lists.
Examples
The List.Last
function retrieves the last item from a list. Let’s dive into its applications with some real-world examples.
Extract Last Item from List
Consider a scenario where you have a list of students, arranged in the order they registered. To identify the student who signed up last, you can use:
// Output: "Charlie"
List.Last( { "Alice", "Bob", "Charlie" } )
If you’re working with a chronological list of dates, pinpointing the latest date is straightforward:
// Output: #date(2023, 1, 3)
List.Last( { #date(2023, 1, 1), #date(2023, 1, 2), #date(2023, 1, 3) } )
Handling Empty Lists
When you apply List.Last
to an empty list, it naturally returns a null value:
// Output: null
List.Last( {} )
Providing a Default Value
There might be instances where a null return isn’t ideal. In such cases, List.Last
allows you to set a default return value:
// Output: "The List is empty"
List.Last( {}, "The list is empty" )
In conclusion, the List.Last
function offers a straightforward way to access the last item in a list. It is more explicit than using item-selection and allows for a convenient way to return a default value.
Related articles
Learn more about List.Last in the following articles:
- Lists in Power Query M / List Functions (200+ Examples)
The complete guide to Lists in Power Query M. Learn from practical examples and master Power Query’s most powerful List functions. » Read more
Related functions
Other functions related to List.Last are:
- List.Alternate
- List.FindText
- List.First
- List.FirstN
- List.LastN
- List.Max
- List.MaxN
- List.Min
- List.MinN
- List.Range
- List.Repeat
- List.Select
