The Table.Schema function in Power Query is used to extract the metadata of a table, returning a new table that describes the column names, data types, and other structural properties of the original dataset. To use it, you simply wrap your table reference in the function: Table.Schema(YourTableName). This is primarily used by developers to build dynamic queries that adapt to changing source columns or to audit data types across large datasets without manually checking every column.
What You'll Need
- Microsoft Excel (with Power Query/Get & Transform) or Power BI Desktop.
- An existing data source loaded into the Power Query Editor (such as a CSV, SQL table, or Excel range).
- Basic familiarity with the Power Query Formula Bar (View > Formula Bar must be checked).
Step-by-Step
- Load Your Data: Open Power Query Editor. Import your data using the standard Get Data methods. Ensure your table has a name (e.g.,
SourceorFilteredRows) as seen in the Applied Steps pane. - Create a New Step: Click the
fxicon next to the formula bar. This creates a new custom step in your query sequence. - Enter the Function: In the formula bar, replace the default
= PreviousStepNamewith= Table.Schema(PreviousStepName). For example, if your last step was named"Changed Type", your formula should be= Table.Schema("Changed Type"). Press Enter. - Analyze the Schema Table: Power Query will now display a table where each row represents a column from your original data. You will see several metadata columns, including:
- Name: The name of the column in the original table.
- TypeName: The Power Query data type (e.g.,
Text.Type,Number.Type). - Kind: The general category of the type.
- IsNullable: A boolean value indicating if the column allows nulls.
- Filter for Specific Metadata: If you only need to know which columns are numbers, click the filter arrow on the
TypeNamecolumn and selectNumber.Type. - Convert Schema to List (Optional): If you need a list of column names for a dynamic function, right-click the
Namecolumn in the schema table and selectDrill Down. This converts the table into a List object.
Common Mistakes
One of the most frequent errors is attempting to use Table.Schema on a value that is not a table. If you have previously used a function that converted your table into a list or a record, Table.Schema will return an error because it specifically requires a table object as its input. Always verify the icon next to the step name in the Applied Steps pane; it must be the table icon.
Another common mistake is forgetting that Table.Schema creates a description of the data, not a transformation of the data itself. Users often expect the function to change the types of the original columns. In reality, it simply reports what those types are. To change types, you must use Table.TransformColumnTypes.
Users also frequently overlook the case sensitivity of the M language. Writing table.schema (lowercase 't') will result in a formula error. Power Query is strictly case-sensitive, so the function must be written exactly as Table.Schema.
Tips
To make your queries more resilient, use Table.Schema to create a dynamic list of columns to be removed. Instead of hard-coding column names in a Table.RemoveColumns step—which causes the query to fail if a column is renamed at the source—you can use Table.Schema to identify all columns that are not of a certain type and remove them dynamically.
When working with very large tables, Table.Schema is significantly more efficient than trying to manually inspect the data. It allows you to programmatically verify that a source file matches the expected template before the rest of your transformation steps run. You can add a conditional check (using an if statement) that stops the query or sends a warning if the schema does not match your requirements.
If you are building a function that needs to be applied to multiple tables, pass the table as a parameter to Table.Schema. This allows you to generate a data dictionary for an entire database by iterating through a list of table names and applying the schema function to each.
Troubleshooting
This happens when the input provided to the function is a List, Record, or Single Value. Ensure the step referenced in the parentheses is a valid table. If you are using a custom function, ensure the parameter being passed is not being accidentally converted to a list via a Table.Column reference.
If the TypeName column shows any for all columns, it means your source data has not been typed yet. Power Query often imports data as the any type by default. To get meaningful schema information, you must apply a Table.TransformColumnTypes step before calling Table.Schema.
If the schema table appears empty, verify that your source table actually contains columns. A table with zero columns is technically valid in Power Query but will result in an empty schema table. Check your source connection to ensure data is being retrieved correctly.
Frequently Asked Questions
Can Table.Schema be used to change data types?
No. Table.Schema is a read-only function. It reports the current metadata of a table but cannot modify the data types or the structure of the original table.
Does Table.Schema work with Folded queries in SQL?
Yes, but be aware that calling Table.Schema may trigger a request to the server to retrieve metadata. Depending on the data source, this might break query folding for subsequent steps because the result of Table.Schema is a new table that exists only in the Power Query engine.
What is the difference between Table.ColumnNames and Table.Schema?
Table.ColumnNames returns a simple list of strings containing the names of the columns. Table.Schema returns a full table containing the names, types, nullability, and other detailed properties of those columns.
Can I use Table.Schema to find columns with a specific name?
Yes. Since Table.Schema returns a table, you can use standard table filtering on the Name column to check if a specific column exists before attempting to perform an operation on it.
Is Table.Schema available in both Excel and Power BI?
Yes, it is a core part of the M formula language and is available in any environment that uses the Power Query engine, including Excel, Power BI Desktop, and Power BI Dataflows.