Downloads
Power Query MTable: columnsBeginner

Table.ColumnCount

Returns how many columns a table has.

Syntax
Table.ColumnCount(table as table) as number

Returns: A number.

What it does

Table.ColumnCount returns the number of columns in a table. It is a quick guard for an export that should always have the same width. A changed column count is an early sign that the layout has changed.

Syntax and arguments

Example

A bank export always has five columns. If the count is different, the query stops with a clear message instead of loading the wrong data.

let
    Source = Excel.CurrentWorkbook(){[Name = "Bank"]}[Content],
    Checked = if Table.ColumnCount(Source) = 5
        then Source
        else error Error.Record("Layout changed", "The bank export should have five columns.")
in
    Checked

The check passes the table through when the count is five. Any other count stops the query.

Example: a second, different use

A reviewer wants to know how many fields a table has before choosing which columns to keep. The count is returned as a number, so it can be used in a message or a condition.

let
    Source = Excel.CurrentWorkbook(){[Name = "GLExport"]}[Content],
    Fields = Table.ColumnCount(Source)
in
    Fields

Things to watch

In business terms

A fixed-width check protects month-end reports from exports that changed shape without warning. It takes a moment to write and prevents loading the wrong columns into a report.

Related functions

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