Table.ReverseRows Function (Power Query M)

When you need the last row of a table to become the first, Table.ReverseRows flips the entire row order in one step. It is the quickest way to read a table from bottom to top without sorting by a column.

Syntax of Table.ReverseRows Function

Table.ReverseRows(table as table) as table

The where clause below explains each part of the syntax.

  • table (required, table): The table whose rows you want to flip.

Returns: a table with rows in reverse order

The output keeps every column and value as is. Only the row order changes, so the last row moves to the top and the first row drops to the bottom.

Example 1: Reverse the row order of a table

Say you have a list of meter readings logged from morning to night, and you want the most recent entry on top.

Here is the starting data:

EntryMeterUnits
MorningAlpha140
MiddayBeta205
EveningGamma175
NightDelta90

Pass the table straight into Table.ReverseRows.

let
Source = Excel.CurrentWorkbook(){[Name="Readings"]}[Content],
Result = Table.ReverseRows(Source)
in
Result

The result produces:

EntryMeterUnits
NightDelta90
EveningGamma175
MiddayBeta205
MorningAlpha140

The Night row was last in the source, so it lands first. Every value stays the same, and only the row sequence is flipped.

Example 2: Get descending order by sorting then reversing

Table.ReverseRows does not sort by value, but you can pair it with Table.Sort to get a clean descending list. Sort ascending first, then flip.

Here is the starting data:

PlayerPoints
Quinn48
Rosa71
Sami33
Theo62
Uma55

Sort Points in ascending order, then reverse the result.

let
Source = Excel.CurrentWorkbook(){[Name="Scores"]}[Content],
Ascending = Table.Sort(Source,{{"Points",Order.Ascending}}),
Result = Table.ReverseRows(Ascending)
in
Result

The result produces:

PlayerPoints
Rosa71
Theo62
Uma55
Quinn48
Sami33

The ascending sort runs first, putting Sami (33) on top. Reversing then puts the highest score, Rosa (71), at the top instead.

Example 3: Grab the last 2 rows of a table

When new records get appended to the bottom of a table, the newest rows sit last. Reverse the table and take the first few rows to pull the latest entries.

Here is the starting data:

OrderRefCustomerAmount
ORD-AWestfield320
ORD-BNorthgate145
ORD-CEastpoint260
ORD-DSouthbank510
ORD-ECrossway95

Reverse the rows, then use Table.FirstN to keep the top two.

let
Source = Excel.CurrentWorkbook(){[Name="Orders"]}[Content],
Reversed = Table.ReverseRows(Source),
Result = Table.FirstN(Reversed,2)
in
Result

The result produces:

OrderRefCustomerAmount
ORD-ECrossway95
ORD-DSouthbank510

After reversing, the last two source orders (ORD-E and ORD-D) sit at the top. Table.FirstN then keeps only those two, giving you the last 2 appended rows.

Things to keep in mind with Table.ReverseRows

  • It reverses the current row order. It does not sort, so the result depends entirely on the order the rows already have.
  • If you need a defined order, sort the table first with Table.Sort, then reverse if you want the opposite direction. For a standalone list, List.Sort does the same job.
  • Pair it with Table.FirstN to grab the last N rows. Reverse, then take the first N.
  • It takes a single argument, the table itself. There are no optional parameters.
  • It runs locally and breaks query folding against databases, so the source is pulled before the reverse happens. On large database tables this can be slower than a folded sort, and buffering the table first with Table.Buffer can keep the row order stable.
  • To filter rows by a condition instead of position, reach for Table.SelectRows rather than reversing.

Common questions about Table.ReverseRows

Is Table.ReverseRows the same as sorting a column in descending order?

No. Table.ReverseRows only flips the order the rows currently have. Table.Sort with Order.Descending orders rows by a column value. To get a true descending sort using reverse, sort ascending first and then reverse, as shown in Example 2.

How do I get the last N rows of a table?

Reverse the table so the bottom rows move to the top, then apply Table.FirstN with the count you want. Example 3 uses this pattern to return the last 2 rows.

List of All Power Query Functions

Related Power Query Functions / Articles:

I am a huge fan of Microsoft Excel and love sharing my knowledge through articles and tutorials. I work as a business analyst and use Microsoft Excel extensively in my daily tasks. My aim is to help you unleash the full potential of Excel and become a data-slaying wizard yourself.