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:
| Entry | Meter | Units |
|---|---|---|
| Morning | Alpha | 140 |
| Midday | Beta | 205 |
| Evening | Gamma | 175 |
| Night | Delta | 90 |
Pass the table straight into Table.ReverseRows.
let
Source = Excel.CurrentWorkbook(){[Name="Readings"]}[Content],
Result = Table.ReverseRows(Source)
in
Result
The result produces:
| Entry | Meter | Units |
|---|---|---|
| Night | Delta | 90 |
| Evening | Gamma | 175 |
| Midday | Beta | 205 |
| Morning | Alpha | 140 |
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:
| Player | Points |
|---|---|
| Quinn | 48 |
| Rosa | 71 |
| Sami | 33 |
| Theo | 62 |
| Uma | 55 |
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:
| Player | Points |
|---|---|
| Rosa | 71 |
| Theo | 62 |
| Uma | 55 |
| Quinn | 48 |
| Sami | 33 |
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:
| OrderRef | Customer | Amount |
|---|---|---|
| ORD-A | Westfield | 320 |
| ORD-B | Northgate | 145 |
| ORD-C | Eastpoint | 260 |
| ORD-D | Southbank | 510 |
| ORD-E | Crossway | 95 |
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:
| OrderRef | Customer | Amount |
|---|---|---|
| ORD-E | Crossway | 95 |
| ORD-D | Southbank | 510 |
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.Sortdoes the same job. - Pair it with
Table.FirstNto 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.Buffercan keep the row order stable. - To filter rows by a condition instead of position, reach for
Table.SelectRowsrather 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: