SqlExpression.ToExpression

ExpressionInternal

Converts a SQL query to M code using the provided environment of available identifiers. Intended for internal use only.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

SqlExpression.ToExpression(sql as text, environment as record) as text

Parameters

NameTypeRequiredDescription
sqltextYesThe SQL query text to convert to M code.
environmentrecordYesA record of available identifiers that define the context for the conversion.

Return Value

textA text string containing the M code equivalent of the provided SQL query.

Remarks

SqlExpression.ToExpression is an internal-only function as stated in the official Microsoft documentation. It converts a SQL query text into equivalent M code, using the environment record to resolve table and column identifiers.

This function is part of the Power Query engine's query folding infrastructure. It is used internally by SQL-based connectors to translate SQL expressions back into M code, typically as part of the bidirectional translation needed for query folding optimization.

### Context

In the Power Query query folding pipeline:

1. M expressions are analyzed and translated to SQL for efficient server-side execution. 2. SqlExpression.ToExpression provides the reverse direction — converting SQL back to M when needed.

For standard M query development, this function has no practical use. Custom connector developers should use Table.View handlers to implement query folding rather than calling this function directly.

Examples

Example 1: Conceptual usage

let
    // SqlExpression.ToExpression converts SQL to M code internally.
    // For example, conceptually:
    // SqlExpression.ToExpression("SELECT * FROM Customers WHERE City = 'Seattle'", env)
    // would produce equivalent M filtering code.
    //
    // In practice, use Table.View handlers for query folding.
    Result = "See Table.View for query folding patterns"
in
    Result

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks