SqlExpression.SchemaFrom

D

T

The M Code Behind the Power Query M function SqlExpression.SchemaFrom

Understanding the SqlExpression.SchemaFrom Function

The SqlExpression.SchemaFrom function is used to retrieve metadata about a SQL database table. This metadata includes information about the columns in the table, such as the name, data type, and length. This information can be used to generate a schema for the table, which can then be used in Power Query to transform and analyze the data.

To use the SqlExpression.SchemaFrom function, you must provide the following arguments:

– ConnectionString: The connection string for the SQL database.

– Catalog: The name of the database catalog that contains the table.

– Schema: The name of the schema that contains the table.

– Table: The name of the table for which to retrieve metadata.

Once you have provided these arguments, the function will return a table that contains metadata about the columns in the specified table.

The M Code Behind SqlExpression.SchemaFrom

The M code behind the SqlExpression.SchemaFrom function is relatively simple. It involves creating a SQL statement that retrieves metadata about the columns in the specified table. Here is an example of the M code that generates a schema from a table called “Customers” in a SQL database:


let

Source = Sql.Database(ConnectionString, Catalog),

Schema = SqlExpression.SchemaFrom(Source, [Schema=Schema, Table=Table]),

Output = #table(Schema[Name], Schema[Type])

in

Output


Let’s break down each part of this code:

– Source: This line establishes a connection to the SQL database using the provided connection string and catalog name.

– Schema: This line calls the SqlExpression.SchemaFrom function and provides the schema and table names as arguments. This generates a table that contains metadata about the columns in the specified table.

– Output: This line creates a new table using the metadata generated by the SqlExpression.SchemaFrom function. The table has two columns: “Name” and “Type”. The “Name” column contains the names of the columns in the table, and the “Type” column contains their data types.

Using the SqlExpression.SchemaFrom Function in Practice

Now that we understand the M code behind the SqlExpression.SchemaFrom function, let’s explore how it can be used in practice. Here are some examples of how you might use this function to generate a schema for a SQL database table:

Example 1: Importing Data from a SQL Database

Suppose you have a SQL database table called “Sales” that you want to import into Power Query. You can use the SqlExpression.SchemaFrom function to generate a schema for the table, which you can then use to transform and analyze the data.

Here is an example of how you might use the SqlExpression.SchemaFrom function to import data from the “Sales” table:

1. Copy the M code for the SqlExpression.SchemaFrom function into a new query in Power Query.

2. Replace the values for the “ConnectionString”, “Catalog”, “Schema”, and “Table” arguments with the appropriate values for your SQL database.

3. Run the query to generate a schema for the “Sales” table.

4. Use the generated schema to import and transform the data from the “Sales” table.

Example 2: Validating Data from a SQL Database

Suppose you have a SQL database table called “Employees” that you want to validate before importing it into Power Query. You can use the SqlExpression.SchemaFrom function to generate a schema for the table, which you can then use to validate the data.

Here is an example of how you might use the SqlExpression.SchemaFrom function to validate data from the “Employees” table:

1. Copy the M code for the SqlExpression.SchemaFrom function into a new query in Power Query.

2. Replace the values for the “ConnectionString”, “Catalog”, “Schema”, and “Table” arguments with the appropriate values for your SQL database.

3. Run the query to generate a schema for the “Employees” table.

4. Use the generated schema to validate the data in the “Employees” table, ensuring that all columns have the correct data types and lengths.

The SqlExpression.SchemaFrom function is a powerful tool for generating a schema from a SQL database table. By understanding the M code behind this function and how it can be used in practice, you can take full advantage of Power Query to transform and analyze your data.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)