Table.SelectColumns

D

T

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

Understanding the Table.SelectColumns Function

The Table.SelectColumns function is used to select specific columns from a table of data. It takes two arguments: the first argument is the table to select columns from, and the second argument is a list of column names to select. The function returns a new table that contains only the selected columns.

Here is an example of how the Table.SelectColumns function is used:


let

Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

SelectColumns = Table.SelectColumns(Source, {"Column1", "Column2"})

in

SelectColumns


In this example, we are selecting only the “Column1” and “Column2” columns from the “Table1” table.

Understanding the M Code Behind Table.SelectColumns

The M code behind the Table.SelectColumns function is relatively simple. It uses the Table.SelectColumns function to select the desired columns and returns the resulting table. Here is the M code for the Table.SelectColumns function:


(table as table, columnsToSelect as list) =>

let

columns = List.Intersect(Table.ColumnNames(table), columnsToSelect),

selectColumns = Table.SelectColumns(table, columns)

in

selectColumns


The first argument, “table”, is the input table to select columns from. The second argument, “columnsToSelect”, is a list of column names to select.

The code uses the List.Intersect function to intersect the list of column names in “columnsToSelect” with the actual column names in the input table. This is done to ensure that only valid column names are selected.

The resulting list of valid column names is then passed to the Table.SelectColumns function to create a new table that contains only the selected columns.

Using Table.SelectColumns for Data Transformations

The Table.SelectColumns function can be used for a variety of data transformations. Here are some examples:

1. Selecting Specific Columns

As shown in the example earlier, the Table.SelectColumns function can be used to select specific columns from a table. This can be useful when you only need certain columns for further analysis or reporting.

2. Removing Unwanted Columns

Sometimes, a table may contain columns that are not needed for a particular analysis or report. The Table.SelectColumns function can be used to remove these unwanted columns and create a new table with only the necessary columns.

3. Renaming Columns

The Table.SelectColumns function can also be used to rename columns in a table. This can be done by selecting the desired column(s) and renaming them using the Table.RenameColumns function.

4. Reordering Columns

The Table.SelectColumns function can also be used to reorder columns in a table. This can be useful when you need to group columns or create a specific order for reporting purposes.

The Table.SelectColumns function is a powerful M function in Power Query that can be used for a variety of data transformations. Understanding the M code behind this function can help you use it more effectively for your data analysis and reporting needs. By using the Table.SelectColumns function, you can select specific columns, remove unwanted columns, rename columns, and reorder columns in a table.

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)