Beasty DAX

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:

Complicated DAX Formula

How easy is it to interpret that?

Beauty DAX

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:

Simplified DAX with Variables

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.

Using Variables

To use variables in DAX, you need to:

  1. 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
  2. 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.
  3. You can continue to define more variable using step 2 above.
  4. The last leg that will serve as the output should be preceded with the keyword RETURN.

How to use VAR in DAX

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.

How to write VAR in DAX

Need to learn more about variables? Check out the Microsoft Documentation

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*