Defining variables in DAX queries

This article shows how to define variables in DAX queries and introduces the differences in scope when there are multiple EVALUATE statements.

If you read DAX queries generated by Power BI, you will probably notice that the variables are often defined in the DEFINE section rather than after an EVALUATE statement. In doing so, all the EVALUATE statements of the same batch execution can access the variables previously defined.

For example, the following code defines a variable SelectedColors used as a filter in two different EVALUATE statements.

DEFINE
VAR SelectedColors = 
FILTER ( 
    ALL ( 'Product'[Color] ),
    'Product'[Color] IN { "Purple", "Azure" }
)

EVALUATE 
CALCULATETABLE ( 
    'Product',
    SelectedColors
)


EVALUATE 
CALCULATETABLE ( 
    VALUES ( 'Product'[Brand] ),
    SelectedColors
)

When the definition of a variable follows the EVALUATE statement its scope is only the table expression referenced by the EVALUATE statement. The following example defines the SelectedColors variable for the first EVALUATE statement only – the reference to that variable in the following EVALUATE statement fails (see line 18), because the variable is no longer accessible there.

EVALUATE 
VAR SelectedColors = 
FILTER ( 
    ALL ( 'Product'[Color] ),
    'Product'[Color] IN { "Purple", "Azure" }
)
RETURN 
CALCULATETABLE ( 
    'Product',
    SelectedColors
)


EVALUATE 
RETURN 
CALCULATETABLE ( 
    VALUES ( 'Product'[Brand] ),
    SelectedColors
)

You can define a variable in each EVALUATE statement, as you can see in the following example where the variable SelectColors is evaluated twice.

EVALUATE 
VAR SelectedColors = 
FILTER ( 
    ALL ( 'Product'[Color] ),
    'Product'[Color] IN { "Purple", "Azure" }
)
RETURN 
CALCULATETABLE ( 
    'Product',
    SelectedColors
)


EVALUATE 
VAR SelectedColors = 
FILTER ( 
    ALL ( 'Product'[Color] ),
    'Product'[Color] IN { "Purple", "Azure" }
)
RETURN 
CALCULATETABLE ( 
    VALUES ( 'Product'[Brand] ),
    SelectedColors
)

A variable defined in DEFINE can be overridden by a variable defined in EVALUATE and/or locally to any other DAX expression. In the following example, the first EVALUATE returns the purple or azure color products, whereas the second EVALUATE uses another definition of the SelectedColors variable including only the red and white color names.

DEFINE
VAR SelectedColors = 
FILTER ( 
    ALL ( 'Product'[Color] ),
    'Product'[Color] IN { "Purple", "Azure" }
)
EVALUATE 
CALCULATETABLE ( 
    'Product',
    SelectedColors
)


EVALUATE 
VAR SelectedColors = 
FILTER ( 
    ALL ( 'Product'[Color] ),
    'Product'[Color] IN { "Red", "White" }
)
RETURN
CALCULATETABLE ( 
    'Product',
    SelectedColors
)

Power BI declares many variables in the DEFINE section, so that it can reuse the same variables in different queries for different visuals. These variables often represent filters applied to the visuals in the report. Their use leads to a more efficient execution, lowering the roundtrips between engine and report and reusing common filters shared across several visuals.

If you query a model in DAX, you should consider using variables to simplify your code. If you use multiple EVALUATE statements to get multiple result sets you should also consider which variables can be used in multiple queries – defining them only once in the DEFINE section before the first EVALUATE statement.

It is worth reminding that the DEFINE statement can appear only once, before one or more EVALUATE statement(s). A single execution batch can have multiple EVALUATE statements, but only one DEFINE section before the first EVALUATE statement.

The ZIP file in the download section includes a sample PBIX file with the Products table and the source code of the queries shown in this article. Use DAX Studio to run the queries.

Download

Download Demo (ZIP)


Article written by

 Enclose code in comments with <PRE></PRE> to preserve indentation.