List.RemoveLastN

List

Removes the last N items from a list, or items from the end while a condition is true.

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

Syntax

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

Parameters

NameTypeRequiredDescription
listlistYesThe source list.
countOrConditionanyYesA number specifying how many items to remove from the end, or a condition function.

Return Value

listA new list with the last N items removed.

Remarks

List.RemoveLastN removes items from the end of a list and returns the remaining items. It is the tail counterpart to List.RemoveFirstN.

When passed a number N, it removes the last N items. If N is larger than the list length, an empty list is returned. When passed a condition function, it scans from the end of the list backward, removing items while the condition returns true. Removal stops at the first item (from the end) that fails the condition — items before that point are kept even if they would satisfy the condition.

The original list is not modified; a new list is returned. A common use case is stripping trailing "footer" or summary rows from a list before processing. For removing from the beginning, use List.RemoveFirstN. For removing a block in the middle, use List.RemoveRange.

Examples

Example 1: Remove the last 2 items

List.RemoveLastN({1, 2, 3, 4, 5}, 2)
Result
Result
1{1, 2, 3}

Example 2: Remove trailing items while value exceeds 30

List.RemoveLastN({10, 20, 30, 40, 50}, each _ > 30)
Result
Result
1{10, 20, 30}

Example 3: Remove footer rows from a list

let
    Rows = {"Data1", "Data2", "Data3", "---", "Footer", "Total"},
    DataOnly = List.RemoveLastN(Rows, 3)
in
    DataOnly
Applied Steps

The final output — the list with the 3 trailing footer rows removed, containing only the data entries.

Result
1{Data1, Data2, Data3}

Compatibility

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