List.LastN

This function retrieves the final element of a given list. In case the list is empty, an exception is raised. An optional parameter, countOrCondition, can be provided to enable the collection of multi...

Syntax

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

About

This function retrieves the final element of a given list. In case the list is empty, an exception is raised. An optional parameter, countOrCondition, can be provided to enable the collection of multiple elements or the filtering of elements. countOrCondition can be defined in three different ways: 1. If a number is provided, the function will return up to that number of elements. 2. When a condition is specified, all elements that initially meet the condition will be returned from the end of the list. Once an element does not meet the condition, the function will stop considering additional elements. 3. If the parameter is set to null, the last element in the list will be returned.

Explanation

Example 1: Identify the final value in the given list {3, 4, 5, -1, 7, 8, 2}. Method of operation: Power Query MList.LastN({3, 4, 5, -1, 7, 8, 2}, 1) Result: {2} Example 2: Locate the last values in the list {3, 4, 5, -1, 7, 8, 2} that are above 0. Method of operation: Power Query MList.LastN({3, 4, 5, -1, 7, 8, 2}, each _ > 0) Result: {7, 8, 2}