Table.FirstN
Table function · By Tahir Nas · Examples run in our simulator · Published · Updated
Keeps the first N rows, or the leading rows while a condition holds.
When to use it
Use it for top-N lists after a sort, or to keep leading rows while a condition holds (such as a header block). For the top N within each group, combine it with Table.Group.
Syntax
Table.FirstN(table as table, countOrCondition as any) as tableParameters
- countOrCondition
- A number of rows, or a condition: rows are taken while it is true.
Returns
The first rows of the table.
Example
Table.FirstN(Table.Sort(#table({"Region", "Product", "Qty"}, {{"North", "Kite", 2}, {"South", "Reel", 5}, {"North", "Reel", 1}, {"West", "Kite", 4}}), {{"Qty", Order.Descending}}), 2)Result
| Region | Product | Qty |
|---|---|---|
| South | Reel | 5 |
| West | Kite | 4 |
Example: keep rows until the first blank
With a condition instead of a count, rows are taken while the condition is true and the rest are dropped.
Table.FirstN(#table({"Line"}, {{"Item A"}, {"Item B"}, {""}, {"Notes"}}), each [Line] <> "")Result
| Line |
|---|
| Item A |
| Item B |
Both results are produced by the Power Query Exercises simulator, an independent implementation of a documented subset of M, and checked on every build.
Good to know
- Sort first to get a top N.
Common mistakes
- Taking the top N without sorting first.
- Expecting ties to be included: exactly N rows are returned.
The matching command in Excel
- Excel for Windows
- Home > Keep Rows > Keep Top Rows
- What you see
- You enter the number of rows to keep from the top of the table.
Paths are for the Power Query editor in Excel for Microsoft 365, checked against Microsoft's documentation on 8 November 2026: Filter a table by row position (Microsoft Learn).
Exercises that use Table.FirstN
- intermediate~8 minFree
Find the three largest orders (kite shop)
Calculate each order's value and keep the three biggest, largest first.
- advanced~14 minPro
Combine two header rows into one (franchise)
A sheet has years on the first row (printed once, over merged cells) and quarters on the second. Build headers like "2026 Q1".
Learn more
Related functions
Official documentation: Table.FirstN on Microsoft Learn.