List.Reverse Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The List.Reverse function in Power Query flips the order of the items in a list, so the last item comes first and the first item comes last.

That’s handy when your data is stored oldest first and you want the newest item on top. It also helps with a few less obvious jobs, like flipping “Last, First” names or putting the latest month’s column first.

Follow along with the example file

List.Reverse Function Examples.xlsx

Download

List.Reverse Function Syntax

List.Reverse(list as list) as list
  • list – the list you want to reverse. It can be a list you type in curly brackets, like {1,2,3}, or a column from a table that your query has loaded (Example 3 shows how).

What it returns: A new list with the same items in reverse order. The original list isn’t changed.

The List.Reverse function is part of the core Power Query M library, so it works wherever Power Query does, including Excel for Microsoft 365, 2024, 2021, 2019, and 2016 (on Windows for 2019 and 2016), and Power BI.

When to Use the List.Reverse Function

Use this function when you need to:

  • Show a list with the newest item first, when your data is stored oldest first
  • Get the last few items of a list, with the most recent one at the top
  • Flip names stored as “Last, First” into “First Last”
  • Reverse the order of columns in a table, such as showing the latest month first

If you want to flip the rows of a whole table upside down, the Table.ReverseRows function does that in one step. List.Reverse works on one list at a time.

Example 1: Reverse a List of Numbers

Let’s start with the basics. Suppose you have a list of years from 2022 to 2026, and you want them with the latest year first.

In Excel, go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor that opens, type this formula in the formula bar:

= List.Reverse({2022..2026})

Result: 2026, 2025, 2024, 2023, 2022

List.Reverse returns the years with the latest first: 2026, 2025, 2024, 2023, 2022

Here, {2022..2026} creates a list of all the years from 2022 to 2026, in that order. List.Reverse then flips it, so 2026 comes first.

The Power Query Editor shows the result as a list, with one item per row.

Examples 1 and 2 type their list right into the query, so they don’t need a table on a worksheet. In the example file, you can open them from Data > Queries & Connections.

Example 2: Reversing Is Not the Same as Sorting

This one trips people up. Suppose you have four test scores in the order they were entered, 72, 95, 88, and 64, and you reverse the list.

Create a new blank query and use this formula:

= List.Reverse({72,95,88,64})

Result: 64, 88, 95, 72

Reversing the scores gives 64, 88, 95, 72, which is not sorted

Here, List.Reverse only flips the positions, so the last score (64) comes first and the first score (72) goes to the end. It doesn’t look at the values at all, which is why the result isn’t sorted.

If you want the highest score first, use the List.Sort function with Order.Descending instead: List.Sort({72,95,88,64},Order.Descending) gives 95, 88, 72, 64.

Example 3: Reverse a Column from a Table

Here’s a common scenario. Below is a support ticket log where new tickets are added at the bottom, and I want a list of the Ticket IDs with the newest ticket first.

Tickets table with Ticket IDs from TKT-301 to TKT-306, oldest first

The ticket log is an Excel Table named Tickets. The code below reads it by that name, so for your own data, change “Tickets” to the name of your table.

Create a new blank query, open the Advanced Editor (Home > Advanced Editor), and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Tickets"]}[Content],
NewestFirst = List.Reverse(Source[Ticket ID])
in
NewestFirst

Result: TKT-306, TKT-305, TKT-304, TKT-303, TKT-302, TKT-301

Ticket IDs with the newest ticket first: TKT-306 down to TKT-301

Here, Source[Ticket ID] gives the Ticket ID column as a list, from TKT-301 to TKT-306. List.Reverse flips it, so the last ticket logged, TKT-306, comes first.

This works because the rows are already in the order the tickets were logged. List.Reverse only follows the row order, so if your own rows are mixed up, sort them first (by a date column or by the ticket number) with the Table.Sort function.

Follow along with the example file

List.Reverse Function Examples.xlsx

Download

Example 4: Get the Last 3 Items, Newest First

Now let’s combine List.Reverse with another list function. Below is an order log with the newest order at the bottom, and I want the IDs of the last 3 orders, with the most recent one first.

Orders table with Order IDs from ORD-5011 to ORD-5016, newest at the bottom

The order log is an Excel Table named Orders (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Orders"]}[Content],
LastThree = List.FirstN(List.Reverse(Source[Order ID]),3)
in
LastThree

Result: ORD-5016, ORD-5015, ORD-5014

The last 3 orders, newest first: ORD-5016, ORD-5015, ORD-5014

Here, List.Reverse flips the Order ID column so the newest order is at the top. Then the List.FirstN function keeps the first 3 items of that reversed list.

The List.LastN function also gets the last 3 items, but it keeps them in their original order. List.LastN(Source[Order ID],3) gives ORD-5014, ORD-5015, ORD-5016, so the newest order ends up last.

Example 5: Flip “Last, First” Names to “First Last”

This next one comes up a lot with exported staff or customer lists. Below is a staff list where each name is written as Last, First, and I want to change the names in the Employee column to First Last.

Staff table with names written as Last, First

The staff list is an Excel Table named Staff (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="Staff"]}[Content],
#"Flipped Names" = Table.TransformColumns(Source,{{"Employee",each Text.Combine(List.Reverse(Text.Split(_,", "))," "),type text}})
in
#"Flipped Names"

Result: The Employee column with each name flipped, so “Kennedy, Raymond” becomes “Raymond Kennedy”

The Employee column with each name flipped to First Last

Here, the Table.TransformColumns function changes every value in the Employee column. The each keyword runs the formula once for every name, and the underscore (_) stands for the current name.

So for “Kennedy, Raymond”, the Text.Split function breaks the name at the comma and space into a list, {“Kennedy”,”Raymond”}.

List.Reverse flips that list to {“Raymond”,”Kennedy”}, and the Text.Combine function joins it back into one name with a space in between. The type text part at the end sets the Employee column’s data type to text.

This works because every name uses a comma followed by a space. If your names use a comma with no space after it, change “, ” in Text.Split to “,”.

Important: List.Reverse only works on lists, so List.Reverse("Raymond") gives the error “We cannot convert the value “Raymond” to type List.” To reverse the characters in a text value, use the Text.Reverse function instead.

Example 6: Reverse the Order of Columns

This last one uses List.Reverse on column names instead of data. Below is a monthly sales table with one column per month from Jan to Apr, and I want the latest month to come right after the Region column.

Monthly sales table with month columns from Jan to Apr

The sales data is an Excel Table named MonthlySales (use your own table’s name in the code). Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="MonthlySales"]}[Content],
#"Newest Month First" = Table.ReorderColumns(Source,{"Region"}&List.Reverse(List.Skip(Table.ColumnNames(Source))))
in
#"Newest Month First"

Result: The same table with the columns in the order Region, Apr, Mar, Feb, Jan

The same table with the columns in the order Region, Apr, Mar, Feb, Jan

Here, the Table.ColumnNames function returns the column names as a list, {“Region”,”Jan”,”Feb”,”Mar”,”Apr”}. The List.Skip function drops the first one, Region, and List.Reverse flips the months to {“Apr”,”Mar”,”Feb”,”Jan”}.

The & operator then puts “Region” back at the front, and the Table.ReorderColumns function arranges the columns in that new order. Since the code reads the column names from the table, it still works when you add a May column next month.

Other Excel Articles You May Also Like:

  • List.PositionOf – Use List.PositionOf in Power Query M to find where a value sits in a list.
  • List.Combine – Use List.Combine in Power Query M to join lists into one.
  • List.Distinct – Use List.Distinct in Power Query M to remove duplicates from a list.
  • List.Max – Use List.Max in Power Query M to find the largest value in a list.

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.