Table.ColumnNames Function (Power Query M)

If you want to pull the list of column headers out of a table in Power Query, Table.ColumnNames is the function for that. It hands you every column name as a list of text, which you can then count, search, or pick from.

Syntax of Table.ColumnNames Function

Table.ColumnNames(table as table) as list

where

  • table (required, table). The table whose column names you want to read.

Returns: a list of text, one entry per column, in left-to-right column order.

In plain terms, you give it a table and it gives you back the header row as a list.

Example 1: Get the list of column names

You have an Employees table and you want its headers as a list.

Here is the starting data:

EmployeeIDFullNameDepartmentSalary
E-101Priya NairFinance72000
E-102Marcus ReedOperations68000

Pass the table straight to Table.ColumnNames:

let
Source = Excel.CurrentWorkbook(){[Name="Employees"]}[Content],
Names = Table.ColumnNames(Source)
in
Names

Result: {"EmployeeID", "FullName", "Department", "Salary"}

The output is a list, not a table. The names come back in the same left-to-right order as the columns.

Example 2: Count the columns

Wrap the result in List.Count to find out how many columns a table has.

Here is the starting data:

OrderIDCustomerAmountStatusRegion
O-5001Hannah Brooks240ShippedWest
O-5002Rohan Gupta980PendingSouth

Count the names that come back:

let
Source = Excel.CurrentWorkbook(){[Name="Orders"]}[Content],
ColumnCount = List.Count(Table.ColumnNames(Source))
in
ColumnCount

Result: 5

This is how you count columns in Power Query, since there is no Table.ColumnCount function to do it for you.

Example 3: Check whether a column exists

Combine it with List.Contains to test for a column before you reference it.

Here is the starting data:

ContactIDNamePhoneCity
C-31Derek Olson555-2048Denver
C-32Ananya Iyer555-7741Seattle

Ask whether an Email column is present:

let
Source = Excel.CurrentWorkbook(){[Name="Contacts"]}[Content],
HasEmail = List.Contains(Table.ColumnNames(Source),"Email")
in
HasEmail

Result: false

There is no Email column, so it returns false. This guards a step that would otherwise error on a missing column.

Example 4: Get the first column name by index

Since the output is a list, you can grab a single name with positional indexing.

Here is the starting data:

SKUProductNamePrice
SKU-900Wireless Mouse18.5
SKU-901USB-C Cable9.0

Take the item at position 0:

let
Source = Excel.CurrentWorkbook(){[Name="Products"]}[Content],
FirstName = Table.ColumnNames(Source){0}
in
FirstName

Result: SKU

Power Query lists are zero-based, so {0} is the first column and {1} is the second.

Things to keep in mind with Table.ColumnNames

  • The output is a list of text, not a table. To load it as a single column you have to convert it first, for example with Table.FromList.
  • It reads headers, not values. For the data in a column as a list, index the table directly with Source[ColumnName] or use Table.ToList.
  • Names come back in column order, left to right. Reorder the columns upstream and the list order changes with them.
  • Indexing past the end throws. Table.ColumnNames(Source){9} on a smaller table gives Expression.Error: There weren't enough elements in the enumeration to complete the operation. Use {9}? to get null instead.
  • Comparisons against the names are case-sensitive. List.Contains(Table.ColumnNames(Source),"email") returns false when the real header is Email.

Common questions about Table.ColumnNames

How do I get the column names of just some columns?

Table.ColumnNames always returns every column. To narrow it, filter the resulting list, or use Table.SelectColumns to keep a chosen set of columns and then read their names.

What is the difference between Table.ColumnNames and Record.FieldNames?

Table.ColumnNames reads the headers of a whole table, while Record.FieldNames reads the field names of a single record. Both return a list of text.

List of All Power Query Functions

Related Power Query Functions / Articles:

I am a huge fan of Microsoft Excel and love sharing my knowledge through articles and tutorials. I work as a business analyst and use Microsoft Excel extensively in my daily tasks. My aim is to help you unleash the full potential of Excel and become a data-slaying wizard yourself.