Downloads
Power Query MTable: columnsBeginner

Table.ColumnNames

Returns the column names of a table as a list.

Syntax
Table.ColumnNames(table as table) as list

Returns: 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

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

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.

Related functions

All Power Query M functions · View as Markdown · M basics