Variable.ValueOrDefault

Value

Returns the value of a variable identified by name, or an optional default value if the variable is not defined.

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

Syntax

Variable.ValueOrDefault(identifier as text, optional defaultValue as any) as any

Parameters

NameTypeRequiredDescription
identifiertextYesThe name of the variable to look up in the current evaluation environment.
defaultValueanyNoThe value to return if the variable identified by identifier is not defined.

Return Value

anyThe 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
    Result
Output
Result
10

Compatibility

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