If you want to flip a list so the last item comes first and the first item comes last, the List.Reverse function is what you reach for.
It takes one list and hands back the same items in the opposite order.
Syntax of List.Reverse Function
List.Reverse(list as list) as list
where
list(required, list). The list whose items you want to put in reverse order.
Returns: a new list with the same items in reverse order. An empty list returns an empty list.
In plain terms, you give it a list and it returns that list back to front, without changing the original.
Example 1: Reverse a list of numbers
Flip a short list of numbers so it runs from the last value to the first.
let
Source = List.Reverse({12,45,7,88,23})
in
Source
Result: {23, 88, 7, 45, 12}
The order is flipped end to end, but the values themselves stay exactly as they were.
Example 2: Reverse a list of text values
The same thing works on text. Here is a list of weekdays in their normal order.
let
Source = List.Reverse({"Monday","Tuesday","Wednesday","Thursday","Friday"})
in
Source
Result: {"Friday", "Thursday", "Wednesday", "Tuesday", "Monday"}
Friday was last, so it now sits first. List.Reverse only cares about position, not what kind of value it’s dealing with.
Example 3: Reverse a column pulled from a table
Say you have a MonthlySales table and you want the unit numbers listed newest month first. Pulling a single column into a list is the same move you would make to find the maximum value in a column in Power Query.
Here is the starting data:
| Month | Units |
|---|---|
| Jan | 180 |
| Feb | 210 |
| Mar | 150 |
| Apr | 260 |
| May | 300 |
| Jun | 240 |
Pull the Units column into a list and reverse it:
let
Source = Excel.CurrentWorkbook(){[Name="MonthlySales"]}[Content],
Reversed = List.Reverse(Source[Units])
in
Reversed
Source[Units] grabs the column as a list, then List.Reverse flips it.
Result: {240, 300, 260, 150, 210, 180}
June’s figure (240) leads now and January’s (180) trails, the reverse of the table order.
Example 4: Get the last N items with List.FirstN
List.Reverse is handy for grabbing the last few items of a list. Reverse it, then take from the top.
Here is a DailyVisitors table:
| Day | Visitors |
|---|---|
| Day1 | 410 |
| Day2 | 525 |
| Day3 | 480 |
| Day4 | 610 |
| Day5 | 700 |
| Day6 | 655 |
| Day7 | 720 |
Reverse the Visitors column, then List.FirstN keeps the first three of the flipped list:
let
Source = Excel.CurrentWorkbook(){[Name="DailyVisitors"]}[Content],
LastThree = List.FirstN(List.Reverse(Source[Visitors]),3)
in
LastThree
Result: {720, 655, 700}
Those are the last three days in reverse, the newest day first.
Things to keep in mind with List.Reverse
- It reverses order only, it does not sort. The output follows the original positions backward. To order by value instead, use
List.Sort. - It works on any list, whatever the items are. Numbers, text, dates, even records or nested lists all reverse the same way, because only position matters.
- To reverse the rows of a table, use
Table.ReverseRowsinstead.List.Reverseis for lists. Feeding it a table throwsExpression.Error: We cannot convert a value of type Table to type List. - The source list stays put, so the step is easy to back out.
List.Reversereturns a new list. If you change your mind, here is how to undo in Power Query.
Common questions about List.Reverse
What is the difference between List.Reverse and Table.ReverseRows?
List.Reverse flips a list of values, while Table.ReverseRows flips the row order of a whole table and keeps every column intact. Use the table one when you want the last row on top without breaking the table apart.
How do I get the last N items of a list?
Wrap the list in List.Reverse, then pass it to List.FirstN with the count you want. List.FirstN(List.Reverse(myList),3) gives the last three items, newest first.
List of All Power Query Functions
Other Power Query Related Articles:
Related Power Query Functions / Articles: