Table.ColumnNames
Returns the column names of a table as a list.
Table.ColumnNames(table as table) as listReturns: A list of text values, one for each column name.
What it does
Table.ColumnNames reads the headings of a table and gives them back as a list. It is most useful as a check. Compare the list with the headings you expect, and stop the query with a clear message if a heading has changed. This catches a bank or system that quietly renamed or removed a column.
Syntax and arguments
- table: the table to read. Required.
Example
A monthly bank export must have five headings. If one is missing, the query stops with a message that names the missing column.
let
Source = Excel.CurrentWorkbook(){[Name = "Bank"]}[Content],
Expected = {"Date", "Description", "Debit", "Credit", "Balance"},
Missing = List.Difference(Expected, Table.ColumnNames(Source)),
Checked = if List.IsEmpty(Missing)
then Source
else error Error.Record("Layout changed", "The bank export is missing: " & Text.Combine(Missing, ", "))
in
Checked
If every expected heading is present, the difference list is empty and the table passes through. Otherwise the query stops and the message lists what is missing.
Example: a second, different use
Select all the amount columns in a file, when their names all start with the same text.
let
Source = Excel.CurrentWorkbook(){[Name = "Trial"]}[Content],
AmountColumns = List.Select(Table.ColumnNames(Source), each Text.StartsWith(_, "Amt ")),
Amounts = Table.SelectColumns(Source, AmountColumns)
in
Amounts
Things to watch
- The names are read at the point of the step. A rename or removal earlier in the query changes the list.
- The list checks names only, not types. Use Table.Schema when the type matters.
- The check depends on exact spelling and capitals. A trailing space in a heading makes a name look different.
- This step reads the layout, not the data, so it is a cheap way to protect a query from a changed export.
In business terms
Layout changes in bank, payroll and ERP exports are a common cause of broken monthly reports. A header check turns a silent error into a clear message that the preparer can act on.