Expression.Evaluate
ExpressionEvaluates a Power Query M expression provided as text at runtime.
Syntax
Expression.Evaluate(document as text, optional environment as nullable record) as anyParameters
| Name | Type | Required | Description |
|---|---|---|---|
document | text | Yes | The M expression text to evaluate. |
environment | record | No | A record of names and values available to the expression during evaluation. |
Return Value
any — The 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}})Expression | Result | |
|---|---|---|
| 1 | a * 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}})Input | Reversed | |
|---|---|---|
| 1 | Power Query | yreuQ rewoP |