DAX formulas can quickly become very complicated and difficult to read especially when you start to combine many functions in one single formula.
Variables in a DAX is a way of breaking down parts of a formula into smaller defined parts.
Let’s take a look at this simple DAX formula below:
How easy is it to interpret that?
That entire formula can be re-written, making use of Variables to break things down in a more explainable way.
Then it can look like the image below:
After making use of variables, the different units of the formula have been broken down to make readability a lot easier.
Trust me, it also makes writing the formula easier as well.
Apart from the fact that variables improves readability, reduces complexity and makes it easier to author DAX formulas, it also improves performance of DAX formulas.
To use variables in DAX, you need to:
- Start your formula with the name of the Measure/Column/Table you are creating. Put an “=” after like you are writing a normal Beasty DAX
- Use the keyword VAR after the “=” sign and type in the name of the variable you are defining without spaces. Put and “=” sign after and write the formula for that breakdown.
- You can continue to define more variable using step 2 above.
- The last leg that will serve as the output should be preceded with the keyword RETURN.
Also note that, you can also define all the legs as variables, including the output leg.
Then to complete the formula, precede the variable you want to output with the RETURN keyword as shown below.
Need to learn more about variables? Check out the Microsoft Documentation