Table.RemoveLastN

Table

Removes the last N rows of a table, or rows while a trailing condition is true.

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

Syntax

Table.RemoveLastN(table as table, countOrCondition as any) as table

Parameters

NameTypeRequiredDescription
tabletableYesThe table to remove rows from.
countOrConditionanyYesA number specifying how many rows to remove from the end, or a condition function.

Return Value

tableThe 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)
Result
OrderID
CustomerName
Product
Region
11AliceWidget AEast
22BobGadget BWest
33CharlieWidget CEast

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")
Result
OrderID
CustomerName
Product
Amount
11AliceWidget A100
22BobGadget B200
33CharlieWidget C150

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)
Result
EmployeeID
FullName
HireDate
1E002BOB JONES7/1/2019
2E005Eve Martinez11/5/2018
3E003Charlie Brown1/10/2022

Compatibility

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