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:
| EmployeeID | FullName | Department | Salary |
|---|---|---|---|
| E-101 | Priya Nair | Finance | 72000 |
| E-102 | Marcus Reed | Operations | 68000 |
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:
| OrderID | Customer | Amount | Status | Region |
|---|---|---|---|---|
| O-5001 | Hannah Brooks | 240 | Shipped | West |
| O-5002 | Rohan Gupta | 980 | Pending | South |
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:
| ContactID | Name | Phone | City |
|---|---|---|---|
| C-31 | Derek Olson | 555-2048 | Denver |
| C-32 | Ananya Iyer | 555-7741 | Seattle |
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:
| SKU | ProductName | Price |
|---|---|---|
| SKU-900 | Wireless Mouse | 18.5 |
| SKU-901 | USB-C Cable | 9.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 givesExpression.Error: There weren't enough elements in the enumeration to complete the operation.Use{9}?to getnullinstead. - Comparisons against the names are case-sensitive.
List.Contains(Table.ColumnNames(Source),"email")returnsfalsewhen the real header isEmail.
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: