Power Query Table.ColumnCount: How to Count Columns
In this article
Table.ColumnCount returns the number of columns in a Power Query table. It is a simple M function that takes a table and gives you an integer, which is useful for dynamic transformations, validation, and building flexible queries. You do not need any special setup, just a table to reference.
What You'll Need
- Power Query Editor open in Excel or Power BI Desktop.
- A table loaded or created in your query (for example, from a data source or entered manually).
- Basic familiarity with the Power Query formula bar and the Advanced Editor.
Step-by-Step
- Open Power Query Editor. In Excel, go to Data > Get & Transform Data > Launch Power Query Editor. In Power BI Desktop, click Transform Data on the Home ribbon. Menu labels can differ slightly by version, device, or region.
- Load or create a table. Connect to your data source or use Enter Data to create a table. Ensure the query you want to inspect is selected in the Queries pane on the left.
- Add a custom column or step. To use Table.ColumnCount, you can add a new step in the Advanced Editor, or create a custom column that references the table. For a quick test, go to Home > Advanced Editor and add a line like
ColumnCount = Table.ColumnCount(YourTableName)before the finalinstatement, then change the output toColumnCount. - Reference the correct table name. Replace
YourTableNamewith the actual name of your table variable in the query. This is often the name of the previous step, such asSourceorChangedType. - Check the result. After clicking Done, the query will return a single number representing the column count. If you added it as a custom column, each row will show the same number.
- Use the result dynamically. You can use Table.ColumnCount to drive other functions, such as Table.SelectColumns or Table.RemoveColumns, by passing the count to a function that expects a number. For example,
Table.RemoveLastN(YourTable, Table.ColumnCount(YourTable))would remove all columns, though that is rarely useful. More practically, you can use it to validate that a table has the expected number of columns before proceeding. - Handle errors. If the table reference is invalid or the argument is not a table, Table.ColumnCount will return an error. Ensure the argument is a table value, not a record or list.
Common Mistakes
- Passing a non-table value. Table.ColumnCount expects a table. If you pass a list or record, you will get an error. Always verify the argument type.
- Using the wrong table name. In the Advanced Editor, step names are case-sensitive and must match exactly. A typo will cause a reference error.
- Forgetting to update the output. If you add a step but leave the final
instatement pointing to the old step, you won't see the result. - Assuming it counts rows. Table.ColumnCount counts columns, not rows. For row count, use Table.RowCount.
- Using it in a custom column without context. If you reference the table inside a custom column, you may need to use the table name from the previous step, not the current row context.
Tips
- Use Table.ColumnCount to create dynamic queries that adapt when the number of columns changes, such as when combining files with varying schemas.
- Combine with Table.ColumnNames to get both the count and the names for validation.
- Test the function in a small query first to understand its behavior before integrating into complex transformations.
- Remember that column count includes all columns, even those that are hidden or have errors.
- If you need to count columns after removing some, apply Table.ColumnCount after the removal step.
Troubleshooting
If Table.ColumnCount returns an error, check the following:
- Expression.Error: We cannot convert a value of type Record to type Table. This means the argument is not a table. Verify the step you are referencing produces a table.
- Expression.Error: The name 'YourTableName' wasn't recognized. Check the spelling and case of the table variable. In the Advanced Editor, step names are defined with
#"Step Name"if they contain spaces. - The query returns a table instead of a number. Ensure the final
instatement outputs the step that contains the Table.ColumnCount result, not the original table. - Unexpected column count. If the number is different from what you see in the data preview, check if any columns are hidden or if the table has been pivoted or unpivoted. Also, ensure you are counting the correct table (e.g., after a merge or append).
If the function still does not work, try simplifying the query to isolate the step. Create a new query with just the table and apply Table.ColumnCount to see if it works. If it does, the issue is likely in the preceding steps.
Frequently Asked Questions
What is the difference between Table.ColumnCount and Table.RowCount?
Table.ColumnCount returns the number of columns in a table, while Table.RowCount returns the number of rows. They are not interchangeable.
Can I use Table.ColumnCount on a table with zero columns?
Yes, it will return 0. A table with no columns is valid in Power Query, though uncommon.
Does Table.ColumnCount count hidden columns?
Yes, it counts all columns in the table, including those that are hidden in the preview. Hidden columns are still part of the table structure.
How do I get the column count as a scalar value for use in another function?
You can assign the result to a step and then reference that step. For example, ColCount = Table.ColumnCount(YourTable) then use ColCount in subsequent expressions.
Is Table.ColumnCount available in all versions of Power Query?
Yes, it is a standard M function available in Excel, Power BI Desktop, and other Power Query environments. The exact menu paths may vary slightly.
Can I use Table.ColumnCount to count columns after a specific operation?
Yes, apply it to the step that results from the operation. For example, if you remove columns, reference the step after removal to get the new count.