List.LastN

List

Returns the last N items from a list, or items at the end of the list that satisfy a condition.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

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

Parameters

NameTypeRequiredDescription
listlistYesThe source list.
countOrConditionanyYesA 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

anyA 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 1: Return the last 3 items

List.LastN({10, 20, 30, 40, 50}, 3)
Result
Result
1{30, 40, 50}

Example 2: Return items from the end while value is greater than 25

List.LastN({10, 20, 30, 40, 50}, each _ > 25)
Result
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
    LastWeek
Applied Steps

The final output — returns the last 7 dates from the 14-day list, representing the most recent week.

Result
11/8/2024
21/9/2024
31/10/2024
41/11/2024
51/12/2024
61/13/2024
71/14/2024

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks