List.RemoveLastN
ListRemoves the last N items from a list, or items from the end while a condition is true.
Syntax
List.RemoveLastN(list as list, countOrCondition as any) as listParameters
| Name | Type | Required | Description |
|---|---|---|---|
list | list | Yes | The source list. |
countOrCondition | any | Yes | A number specifying how many items to remove from the end, or a condition function. |
Return Value
list — A 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 2: Remove trailing items while value exceeds 30
List.RemoveLastN({10, 20, 30, 40, 50}, each _ > 30)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
DataOnlyThe final output — the list with the 3 trailing footer rows removed, containing only the data entries.
Result | |
|---|---|
| 1 | {Data1, Data2, Data3} |