Variable.ValueOrDefault
ValueReturns the value of a variable identified by name, or an optional default value if the variable is not defined.
Syntax
Variable.ValueOrDefault(identifier as text, optional defaultValue as any) as anyParameters
| Name | Type | Required | Description |
|---|---|---|---|
identifier | text | Yes | The name of the variable to look up in the current evaluation environment. |
defaultValue | any | No | The value to return if the variable identified by identifier is not defined. |
Return Value
any — The value of the specified variable, or the default value if the variable is not defined.
Remarks
Variable.ValueOrDefault retrieves the value of a variable by its text name from the current evaluation environment. Unlike Variable.Value, which raises an error when the variable is not found, this function returns the optional defaultValue instead. If defaultValue is not provided and the variable is not defined, null is returned.
This is an internal engine function used by the Power Query evaluation infrastructure to resolve variable references with fallback behavior. It supports scenarios where the engine needs to gracefully handle missing bindings during expression evaluation — for example, when evaluating expressions in environments where certain names may or may not be present.
In normal user-written M queries, this function is not needed. Standard patterns such as try ... otherwise ... or null-coalescing with ?? provide equivalent fallback behavior for user code. If you need dynamic name resolution with defaults, consider using Record.FieldOrDefault on a record, or Expression.Evaluate with a controlled environment.
Examples
Example 1: Conceptual usage — resolve a variable with a fallback
let
// Conceptually, if "x" is defined in the environment, return its value.
// If not defined, return the default value 0.
// Variable.ValueOrDefault("x", 0) would return 0 if x is not in scope.
Result = 0
in
ResultResult | |
|---|---|
| 1 | 0 |