Expression.Evaluate

Expression

Evaluates a Power Query M expression provided as text at runtime.

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

Syntax

Expression.Evaluate(document as text, optional environment as nullable record) as any

Parameters

NameTypeRequiredDescription
documenttextYesThe M expression text to evaluate.
environmentrecordNoA record of names and values available to the expression during evaluation.

Return Value

anyThe result of evaluating the M expression.

Remarks

Expression.Evaluate compiles and executes an M expression provided as a text string. This enables dynamic or meta-programming scenarios where the expression to run is not known at design time.

The environment parameter is critical — it defines which names (functions, values, types) are available inside the evaluated expression. Without an environment, the expression has access only to built-in constants and operators. To make standard library functions available, pass #shared as the environment:

``powerquery Expression.Evaluate("Text.Upper(""hello"")", #shared) ``

Or pass a custom environment with specific bindings:

``powerquery Expression.Evaluate("x + y", [x = 10, y = 20]) ``

### Important notes

- The evaluated expression runs in an isolated scope. It cannot access let bindings or step names from the surrounding query unless you explicitly pass them in the environment record. - #shared provides access to all standard library functions and any other queries in the workbook. - Use this sparingly — dynamic evaluation can make queries harder to debug and may prevent query folding.

Examples

Example 1: Evaluate a simple arithmetic expression

let
    Result = Expression.Evaluate("a * b + c", [a = 5, b = 4, c = 3])
in
    #table({"Expression", "Result"}, {{"a * b + c  (a=5, b=4, c=3)", Result}})
Result
Expression
Result
1a * b + c (a=5, b=4, c=3)23

Example 2: Evaluate with standard library functions

let
    Result = Expression.Evaluate("Text.Reverse(""Power Query"")", #shared)
in
    #table({"Input", "Reversed"}, {{"Power Query", Result}})
Result
Input
Reversed
1Power QueryyreuQ rewoP

Compatibility

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