List.LastN
ListReturns the last N items from a list, or items at the end of the list that satisfy a condition.
Syntax
List.LastN(list as list, countOrCondition as any) as anyParameters
| Name | Type | Required | Description |
|---|---|---|---|
list | list | Yes | The source list. |
countOrCondition | any | Yes | A number specifying how many items to return from the end, or a function that returns true for items to include from the end of the list. |
Return Value
any — A list of the last N items, or a list of items from the end while the condition holds true.
Remarks
List.LastN is the tail counterpart to List.FirstN. It always returns a list, never a scalar, and accepts either a number or a condition function as its second argument.
When passed a number N, it returns the last N items of the list in their original order. If N exceeds the list length, the entire list is returned. If N is 0, an empty list is returned.
When passed a condition function, the function scans from the end of the list backward, collecting items as long as the condition returns true. Collection stops at the first item (from the end) that fails the condition — items before that point are excluded, even if they would satisfy the condition. The returned items appear in their original list order (not reversed). Use this mode for collecting a trailing run, such as the most recent dates above a threshold. For non-contiguous filtering, use List.Select.
Examples
Example 2: Return items from the end while value is greater than 25
List.LastN({10, 20, 30, 40, 50}, each _ > 25)Result | |
|---|---|
| 1 | {30, 40, 50} |
Example 3: Get last week of dates from a date list
let
Dates = List.Dates(#date(2024, 1, 1), 14, #duration(1, 0, 0, 0)),
LastWeek = List.LastN(Dates, 7)
in
LastWeekThe final output — returns the last 7 dates from the 14-day list, representing the most recent week.
Result | |
|---|---|
| 1 | 1/8/2024 |
| 2 | 1/9/2024 |
| 3 | 1/10/2024 |
| 4 | 1/11/2024 |
| 5 | 1/12/2024 |
| 6 | 1/13/2024 |
| 7 | 1/14/2024 |