Power QueryExercises

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 table

Parameters

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

RegionProductQty
SouthReel5
WestKite4

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

Common mistakes

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

Learn more

Related functions

Official documentation: Table.FirstN on Microsoft Learn.