Downloads
Power Query MTable: rowsBeginner

Table.LastN

Keeps the last rows, either a set number or while a condition holds from the bottom.

Syntax
Table.LastN(table as table, countOrCondition as any) as table

Returns: A table with the last rows.

What it does

Table.LastN keeps the bottom rows of a table. In practice you sort the table by date in ascending order, so the newest rows are at the bottom, and then keep the last few. It gives you the most recent items, such as the latest payments or the last postings to an account.

Syntax and arguments

Example

A cash manager wants the five most recent customer payments in the sample database.

let
    Source = Sql.Database("learnsql.cpetoday.com,1433", "classicmodels"),
    Payments = Source{[Schema = "classicmodels", Item = "payments"]}[Data],
    Oldest = Table.Sort(Payments, {{"paymentDate", Order.Ascending}}),
    Latest = Table.LastN(Oldest, 5)
in
    Latest

The sort puts the oldest payment first and the newest last. The last five rows are the five most recent payments. Because the sort is ascending, "last" means "newest" here.

Example: a second, different use

A ledger is sorted by posting date. The last entries are the postings that came after the cut-off, so the reviewer takes the rows from the bottom while the posting date is on or after the cut-off date.

let
    Source = Excel.CurrentWorkbook(){[Name = "GL"]}[Content],
    Sorted = Table.Sort(Source, {{"PostingDate", Order.Ascending}}),
    AfterCutoff = Table.LastN(Sorted, each [PostingDate] >= #date(2026, 10, 1))
in
    AfterCutoff

Things to watch

In business terms

Recent activity lists, the latest bank lines for a cash check and the newest entries in a journal are all bottom-of-the-sorted-list questions.

Related functions

All Power Query M functions · View as Markdown · M basics