VAR DAX Statement


The VAR keyword introduces variables in an expression.
The syntax after VAR defines a variable, which can be consumed in following VAR statements or within the mandatory RETURN statement following the declaration of one or more variables.
If VAR is used in the DEFINE section of an EVALUATE statement, it can be used in any expression of the statement and the RETURN keyword cannot be used.

VAR <name> = <expression>
[VAR <name2> = <expression2> [...]]
RETURN <result_expression>

The expression is evaluated only once and assigned to the name in the scope of the expression where the VAR/RETURN expression is used.
The expression2 has access to name.
A variable nameN is visible in all the expressionN+1 of following variables defined within the same VAR/RETURN statement.
If an expression contains the definition of other variables in nested VAR/RETURN statements, the nested variables are not visible outside of the scope of expression. Indeed, the result_expression has access to all the variables (name, name2, …) defined in the VAR statement(s) before RETURN in the same statement, but it does not have access to variable defined in nested VAR/RETURN statements applied to assigned to variables.

The variable name cannot be a table name or a reserved keyword in DAX or MDX. When a new table is added to a model, it could break all the existing DAX expressions using the same name for a variable. To lower the chances of such an event, you should carefully name variables or use a prefix reserved for variables. DAX code generators commonly use a prefix like ‘_’ or ‘__’ to limit the likelihood of such an issue.

The actual evaluation of the variable is subject to possible optimizations in the query plan, which could remove unused variables or unused conditional branches as described in When are variables evaluated in DAX? article.

Last update: Jul 16, 2024   » Contribute   » Show contributors

Contributors: Alberto Ferrari, Marco Russo, Kenneth Barber, Pär Adeen

Microsoft documentation: https://docs.microsoft.com/en-us/dax/var-dax

2018-2024 © SQLBI. All rights are reserved. Information coming from Microsoft documentation is property of Microsoft Corp. » Contact us   » Privacy Policy & Cookies

Context Transition

This function performs a Context Transition if called in a Row Context. Click to read more.

Row Context

This expression is executed in a Row Context. Click to read more.

Iterator

Not recommended

The use of this function is not recommended. See Remarks and Related functions for alternatives.

Not recommended

The use of this parameter is not recommended.

Deprecated

This function is deprecated. Jump to the Alternatives section to see the function to use.

Volatile

A volatile function may return a different result every time you call it, even if you provide the same arguments. Click to read more.

Deprecated

This parameter is deprecated and its use is not recommended.

DirectQuery compatibility

Limitations are placed on DAX expressions allowed in measures and calculated columns.
The state below shows the DirectQuery compatibility of the DAX function.

Contribute

Want to improve the content of VAR? Did you find any issue?
Please, report it us! All submissions will be evaluated for possible updates of the content.


This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.