Table.RemoveLastN
TableRemoves the last N rows of a table, or rows while a trailing condition is true.
Syntax
Table.RemoveLastN(table as table, countOrCondition as any) as tableParameters
| Name | Type | Required | Description |
|---|---|---|---|
table | table | Yes | The table to remove rows from. |
countOrCondition | any | Yes | A number specifying how many rows to remove from the end, or a condition function. |
Return Value
table — The table with the last N rows (or trailing conditional rows) removed.
Remarks
Table.RemoveLastN removes rows from the bottom of the table. When countOrCondition is a number, it removes exactly that many trailing rows. When it is a function (predicate), it removes rows from the end while the condition returns true, working backwards from the last row — stopping at the first row (from the end) that fails the condition and retaining everything above it.
Table.RemoveLastN is the counterpart of Table.RemoveFirstN. The condition-based form is useful for stripping trailing metadata or footer rows from flat-file imports where the footer length may vary. Like Table.RemoveFirstN, this function does not fold to data sources.
Be aware that when using a condition function, the function is evaluated from the bottom of the table upward. This means the condition sees rows in reverse order, and only a contiguous sequence of trailing rows that satisfy the predicate will be removed — not all rows globally matching the condition.
Examples
Example 1: Remove the last 3 orders from the Sales table
let
Sales = #table(
{"OrderID", "CustomerName", "Product", "Region"},
{{1,"Alice","Widget A","East"},{2,"Bob","Gadget B","West"},
{3,"Charlie","Widget C","East"},{4,"Alice","Gadget D","North"},
{5,"Diana","Widget A","West"},{6,"Bob","Thingamajig E","East"}}
)
in
Table.RemoveLastN(Sales, 3)OrderID | CustomerName | Product | Region | |
|---|---|---|---|---|
| 1 | 1 | Alice | Widget A | East |
| 2 | 2 | Bob | Gadget B | West |
| 3 | 3 | Charlie | Widget C | East |
Example 2: Strip trailing summary rows from a report export
let
Raw = #table(
{"OrderID","CustomerName","Product","Amount"},
{{1,"Alice","Widget A",100},{2,"Bob","Gadget B",200},{3,"Charlie","Widget C",150},
{null,null,"Total",450},{null,null,"Grand Total",450}}
)
in
Table.RemoveLastN(Raw, each [Product] = "Total" or [Product] = "Grand Total")OrderID | CustomerName | Product | Amount | |
|---|---|---|---|---|
| 1 | 1 | Alice | Widget A | 100 |
| 2 | 2 | Bob | Gadget B | 200 |
| 3 | 3 | Charlie | Widget C | 150 |
Example 3: Remove trailing employees hired in 2024
let
Employees = #table(
type table [EmployeeID = text, FullName = text, HireDate = date],
{{"E002","BOB JONES",#date(2019,7,1)},{"E005","Eve Martinez",#date(2018,11,5)},
{"E003","Charlie Brown",#date(2022,1,10)},{"E006","frank lee",#date(2024,2,14)}}
)
in
Table.RemoveLastN(Employees, each Date.Year([HireDate]) = 2024)EmployeeID | FullName | HireDate | |
|---|---|---|---|
| 1 | E002 | BOB JONES | 7/1/2019 |
| 2 | E005 | Eve Martinez | 11/5/2018 |
| 3 | E003 | Charlie Brown | 1/10/2022 |