Table.Column

D

T

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

Understanding the Table.Column Function

The `Table.Column` function is a powerful M function in Power Query that allows users to extract a specific column from a table. The function takes two arguments: the table to extract the column from, and the name of the column to extract. Here is the syntax for the `Table.Column` function:


Table.Column(Table as table, ColumnName as text) as list


The `Table` argument is the table to extract the column from, while the `ColumnName` argument is the name of the column to extract. The function returns a list of values from the specified column.

The M Code Behind Table.Column

The `Table.Column` function is implemented in M code, which is the language behind Power Query. The M code for the `Table.Column` function is relatively simple and can be broken down into three main steps:

1. Find the index of the column to extract

2. Extract the values from the specified column

3. Return the list of values

Here is the M code for the `Table.Column` function:


(Table as table, ColumnName as text) =>

let

ColumnIndex = Table.ColumnIndex(Table, ColumnName),

Values = Table.Column(Table, ColumnIndex),

Result = Values

in

Result


The code begins by defining the two arguments for the function: the `Table` argument, which is the table to extract the column from, and the `ColumnName` argument, which is the name of the column to extract.

In the first step of the code, the `Table.ColumnIndex` function is used to find the index of the specified column in the table. This function takes two arguments: the table to search, and the name of the column to find. The function returns the index of the column in the table.

In the second step of the code, the `Table.Column` function is used to extract the values from the specified column. This function takes two arguments: the table to extract the column from, and the index of the column to extract. The function returns a list of values from the specified column.

Finally, in the third step of the code, the list of values is returned as the result of the function.

Using the Table.Column Function

Using the `Table.Column` function in Power Query is relatively straightforward. Here are the steps to extract a column from a table using the `Table.Column` function:

1. Load the table into Power Query

2. Open the Advanced Editor

3. Create a new function that uses the `Table.Column` function to extract the desired column

4. Invoke the function to extract the column

Here is an example of how to use the `Table.Column` function to extract a column from a table:

Suppose we have the following table in Power Query:

| Name | Age |

|————|—–|

| John Smith | 25 |

| Jane Doe | 30 |

| Bob Johnson| 40 |

To extract the `Age` column from the table, we would follow these steps:

1. Load the table into Power Query

2. Open the Advanced Editor

3. Create a new function with the following code:


Table.Column(

Table = Source,

ColumnName = "Age"

)


4. Invoke the function to extract the `Age` column

The resulting output would be a list of the values in the `Age` column:


{25, 30, 40}


The `Table.Column` function is a powerful M function in Power Query that allows users to extract a specific column from a table. The M code behind the function is relatively simple and can be broken down into three main steps. Understanding the M code behind the function can help users create custom data transformations and perform advanced data analysis in Power Query.

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)