Table.ReverseRows

D

T

The M Code Behind the Power Query M function Table.ReverseRows

What is Table.ReverseRows?

Table.ReverseRows is a function in Power Query that is used to reverse the order of rows in a table. This function takes a table as its input and returns a table with the same columns but with the rows in reverse order. The function can be useful when you want to view the data in a table in reverse order, or when you want to perform further transformations on the reversed data.

The M Code Behind Table.ReverseRows

The M code behind Table.ReverseRows is relatively simple. Here is what the code looks like:


(table as table) =>

let

Source = Table.FromRecords({}),

ColumnCount = Table.ColumnCount(table),

RowCount = Table.RowCount(table),

Rows = List.Reverse(table[Rows]),

NewTable = Table.FromColumns(Rows, Table.ColumnNames(table))

in

NewTable


Let’s break down this code line by line to understand what it does.


(table as table) =>


This line defines the input parameter for the function. In this case, the input parameter is a table and it is named “table”.


let


This line starts a new variable definition block. The code inside this block will create variables that can be used later in the function.


Source = Table.FromRecords({}),


This line creates an empty table called “Source”. This table will be used later to create the output table.


ColumnCount = Table.ColumnCount(table),


This line creates a variable called “ColumnCount” that stores the number of columns in the input table.


RowCount = Table.RowCount(table),


This line creates a variable called “RowCount” that stores the number of rows in the input table.


Rows = List.Reverse(table[Rows]),


This line creates a variable called “Rows” that stores the reversed rows from the input table. The List.Reverse function is used to reverse the rows.


NewTable = Table.FromColumns(Rows, Table.ColumnNames(table))


This line creates the output table called “NewTable”. The Table.FromColumns function is used to create the table from the reversed rows. The Table.ColumnNames function is used to ensure that the column names in the output table match the column names in the input table.


in


This line marks the end of the variable definition block.


NewTable


This line returns the output table.

Using Table.ReverseRows in Power Query

Now that we understand the M code behind Table.ReverseRows, let’s see how we can use it in a Power Query project. Here are the steps:

1. Open Microsoft Excel and create a new workbook.

2. Click the “Data” tab and select “From Other Sources” in the “Get & Transform Data” section.

3. Select “From Microsoft Query” and choose the data source you want to use.

4. Follow the prompts to select the data you want to use and import it into Power Query.

5. In Power Query, click the “Add Column” tab and select “Custom Column” in the “General” section.

6. Enter a name for the new column and enter the following formula:


= Table.ReverseRows([PreviousStep])


7. Click “OK” to create the new column. The table will now be reversed.

Table.ReverseRows is a useful function in Power Query that can be used to reverse the order of rows in a table. The M code behind this function is relatively simple, but it can be difficult to understand if you are not familiar with the language. By following the steps in this article, you can easily use the Table.ReverseRows function in your own Power Query projects.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)