Table.LastN
Keeps the last rows, either a set number or while a condition holds from the bottom.
Table.LastN(table as table, countOrCondition as any) as tableReturns: 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
- table: the table to read from. Required.
- countOrCondition: a number of rows to keep, or a function. With a function, rows are kept from the bottom while the function returns true. Required.
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
- Always sort first. The bottom row of an unsorted table is not necessarily the latest.
- Dates must be typed as dates. Text dates sort in text order, so "10/01/2026" can come before "9/15/2026".
- Ties at the cut-off can leave out equal values, as with Table.FirstN.
- Query folding: check View Native Query on the step. Do not assume it folds the same way as Table.FirstN.
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.