Table.ColumnNames

D

T

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

In this article, we will take an in-depth look at the M code behind the Table.ColumnNames function and explore how it works.

Understanding the Table.ColumnNames Function

The Table.ColumnNames function is a simple but powerful function that returns a list of column names for a given table. The function takes a single parameter, which is the name of the table that you want to retrieve the column names for. Here is the syntax for the Table.ColumnNames function:


Table.ColumnNames(table as table) as list


The function returns a list of column names for the specified table. Here is an example of how to use the Table.ColumnNames function in Power Query:


let

Source = Excel.Workbook(File.Contents("C:MyWorkbook.xlsx"), null, true),

Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],

#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),

ColumnNames = Table.ColumnNames(#"Promoted Headers")

in

ColumnNames


In this example, we are using the Table.ColumnNames function to retrieve a list of column names for a table that we have loaded from an Excel workbook. We are first loading the workbook using the Excel.Workbook function, and then selecting the Sheet1 worksheet using the Source{[Item=”Sheet1″,Kind=”Sheet”]}[Data] syntax. We are then promoting the headers of the table using the Table.PromoteHeaders function, and then using the Table.ColumnNames function to retrieve a list of column names for the resulting table.

The M Code Behind the Table.ColumnNames Function

The M code behind the Table.ColumnNames function is fairly simple and straightforward. Here is the M code for the Table.ColumnNames function:


(table as table) =>

let

ColumnNames = Table.ColumnNames(table)

in

ColumnNames


As you can see, the function takes a single parameter, which is the table that you want to retrieve the column names for. The function then uses the Table.ColumnNames function to retrieve a list of column names for the specified table. Finally, the function returns the list of column names as the result.

In this article, we have explored the M code behind the Table.ColumnNames function in Power Query. We have seen that the function is a simple but powerful function that can be used to retrieve a list of column names for a given table. We have also seen how to use the function in Power Query, and how to work with the M code behind the function.

If you are interested in learning more about Power Query and the M language, there are many great resources available online that can help you get started. With a little bit of practice, you can become a Power Query expert and transform your data in ways that you never thought possible.

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)