Power Query Table.ColumnNames: How to Use It
In this article
Power Query's Table.ColumnNames function returns a list of the column names in a table. You use it when you need to know the exact headers your query is working with—for example, to build dynamic transformations that don't break when columns are added, removed, or renamed. The function takes a single argument, a table, and returns a list of text values. It is available in Power Query for Excel, Power BI, and other Microsoft data tools that use the M language. No special setup is required beyond having a table to reference.
What You'll Need
- A Power Query query that already contains a table (for example, loaded from a file, database, or another query).
- Access to the Power Query Editor (in Excel: Data > Get Data > Launch Power Query Editor; in Power BI: Transform data).
- Basic familiarity with the formula bar and the Advanced Editor, since you'll be writing or editing M code.
Step-by-Step
- Open the Power Query Editor and select the query that contains the table you want to inspect. The query name appears in the Queries pane on the left.
- Check the table's name. In the Applied Steps pane, the last step usually returns a table. You can refer to it by the step name (for example,
SourceorChanged Type) or by the query name if the query is a table. If you're unsure, click the step and look at the formula bar—it shows the expression that produces the table. - Add a new step to call the function. You can do this in the formula bar or the Advanced Editor. For a quick test, click fx next to the formula bar to add a custom step, then type
= Table.ColumnNames(Source)(replaceSourcewith your table reference). Press Enter. The result is a list of column names. - Inspect the result. Power Query will show the list as a series of text values. You can click List Tools to see options like To Table if you want to convert the list into a table for further processing.
- Use the list in a transformation. Common uses include passing the list to functions like
Table.SelectColumnsorTable.RemoveColumnsto keep or drop columns dynamically. For example,Table.SelectColumns(Source, List.FirstN(Table.ColumnNames(Source), 3))keeps the first three columns regardless of their names. - Handle errors. If the argument is not a table, the function returns an error. Make sure the step you reference actually produces a table. You can wrap the call in
try ... otherwiseto catch errors, but that's usually unnecessary if you reference a known table step. - Close and load. Once your query works, close the Power Query Editor and load the results to your worksheet or data model. The column names list itself is not usually loaded; it's used inside the query logic.
Common Mistakes
- Referencing the wrong step. Using a step name that returns a list or a scalar instead of a table causes an error. Always confirm the step's output is a table.
- Forgetting that column names are case-sensitive. Power Query treats column names as case-sensitive in many operations.
Table.ColumnNamesreturns the exact names, so use them as-is when feeding into other functions. - Assuming the list is static. If the source schema changes, the list changes too. That's the point—but it means your query must handle new or missing columns gracefully.
- Using it on a table with duplicate column names. Power Query usually prevents duplicate names, but if they occur, the list will contain duplicates, which can confuse downstream functions.
- Expecting it to return column types. It returns only names. To get types, use
Table.Schemainstead.
Tips
- Combine
Table.ColumnNameswithList.Containsto check if a specific column exists before referencing it:List.Contains(Table.ColumnNames(Source), "Sales")returns true or false. - Use
Table.ColumnNamesto build a list of columns to unpivot dynamically. For example, you can select all columns except a fixed set by usingList.Difference(Table.ColumnNames(Source), {"ID", "Name"}). - When debugging, add a temporary step that outputs
Table.ColumnNames(Source)and view it in the editor. Delete the step when done. - Remember that column names in Power Query are text, so you can manipulate them with text functions (e.g.,
Text.Upper) before using them in transformations.
Troubleshooting
If Table.ColumnNames returns an error, check the following:
- "We cannot convert a value of type ... to type Table." The argument you passed is not a table. Verify the step name and ensure it produces a table. You can click the step and look at the preview—if it shows a list or a single value, that's the problem.
- The list is empty. This happens if the table has no columns, which is rare but possible with certain connectors. Check the source data.
- Column names appear with unexpected characters. Some sources return names with leading/trailing spaces or special characters. Use
Text.Trimon each name if needed, but be aware that renaming columns later may be necessary. - The function works in the editor but fails on refresh. This often means the underlying table structure changed (e.g., a column was removed). Update your query to handle schema changes, or use
try ... otherwiseto provide a fallback. - Performance issues with very wide tables.
Table.ColumnNamesis lightweight, but if you call it repeatedly in a large query, consider storing the result in a variable to avoid recomputation.
Frequently Asked Questions
What does Table.ColumnNames return?
It returns a list of text values, each being the name of a column in the specified table, in the order they appear.
Can I use Table.ColumnNames on a table with no rows?
Yes, it works on the table's schema, so even an empty table (with columns defined) returns the column names.
How do I get column names and types together?
Use Table.Schema, which returns a table with columns like Name, TypeName, and more. Table.ColumnNames only gives names.
Is Table.ColumnNames available in all Power Query versions?
Yes, it's a standard M function included in Power Query for Excel, Power BI, and other Microsoft data products. The exact behavior is consistent across versions.
Can I use Table.ColumnNames to rename columns dynamically?
Yes, you can pair it with Table.RenameColumns and a list of new names. For example, renames columns to A, B, C in order.
What happens if the table has duplicate column names?
Power Query typically disallows duplicate column names in a table, but if they exist, the list will include duplicates. Downstream functions may behave unexpectedly, so it's best to ensure unique names.