Table.ColumnCount
Returns how many columns a table has.
Table.ColumnCount(table as table) as numberReturns: 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
- table: the table to count. Required.
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
- This counts columns, not rows. Use Table.RowCount for rows.
- A count can stay the same while a column is renamed or swapped for another. Pair this check with Table.ColumnNames for a stronger test.
- The result is a number, so it cannot be used as a table step. Put it inside a condition or in its own query.
- Query folding: the count reads the table's layout, so it does not change which rows come back.
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.