• Home
  • About Us
  • Contact Us
  • Privacy Policy
  • Special Offers
Business Intelligence Info
  • Business Intelligence
    • BI News and Info
    • Big Data
    • Mobile and Cloud
    • Self-Service BI
  • CRM
    • CRM News and Info
    • InfusionSoft
    • Microsoft Dynamics CRM
    • NetSuite
    • OnContact
    • Salesforce
    • Workbooks
  • Data Mining
    • Pentaho
    • Sisense
    • Tableau
    • TIBCO Spotfire
  • Data Warehousing
    • DWH News and Info
    • IBM DB2
    • Microsoft SQL Server
    • Oracle
    • Teradata
  • Predictive Analytics
    • FICO
    • KNIME
    • Mathematica
    • Matlab
    • Minitab
    • RapidMiner
    • Revolution
    • SAP
    • SAS/SPSS
  • Humor

Use more variables in DAX to simplify your life

January 15, 2019   Self-Service BI

Today I ran across a pretty straightforward DAX challenge that is made so much simpler thanks to variables. So I decided to share it. Remember folks use variables for each and every measure (even if you don’t think you need them). It will prepare you for the toughest challenges :).

In this case we had a pretty straightforward table of survey results where we want to visualize the PCT of Yes answers by weight to a question for each day but also a running AVG of this PCT for the last 3 days.
The data is pretty straightforward:

First I write a measure that calculated the total weight for both Yes and No answers. We then divide the yes answer by the total to get the PCT of Yes answers compared to the total.

Measure =
VAR yesq =
    CALCULATE ( SUM ( Calc[Weight] ), Calc[Brand A] = “Yes” )
VAR noq =
    CALCULATE ( SUM ( Calc[Weight] ), Calc[Brand A] = “No” )
VAR total = yesq + noq
VAR pctYes =
    DIVIDE ( yesq, total )
RETURN
    pctYes

The benefits of using variables are:
– Performance, each value that is stored in the variable is calculated just once.
– Re-usability, I can reuse the values multiple times
– Ease of use, this code looks pretty straightforward (I like to think)

This now allows us to plot the PCT of yes in a chart:

Finally we are going to use more variables to calculate the avg of this PCT for the last 3 days:

Measure 2 =
VAR daterange =
    DATESINPERIOD ( Calc[Date], MAX ( Calc[Date] ), -3, DAY )
RETURN
    AVERAGEX ( daterange, [Measure] )

Here create a variable to determine the daterange. Using DATESINPERIOD we get the date from the current context (using MAX) to 3 days ago. Then we use the 3 dates in the variable to calculate the PCT for each of those before we finally take the AVG over the results using AVERAGEX.

That’s it, now we can add both the measures in a line chart and call it done.

As Darren points out in the comments below you shouldn’t really be averaging percentages (goes to show you shouldn’t always just do what is asked :)). Again that is also very easy to change our previous measure a bit to facilitate this. Here we use the variable to create a new filter and use the base measure to divide the sum of the yes’s for the 3 days divided by the sum of the total for the 3 days.

Measure 3 =
VAR daterange =
    DATESINPERIOD ( Calc[Date], MAX ( Calc[Date] ), -3, DAY )
RETURN
    CALCULATE ( [Measure], daterange )

That gives us a small variation but probably more correct:

So as you can see using variables you can split up problems into smaller simpler problems. This is especially true when you need to work with intermediate tables like we did with the Dates.

You can download the sample file here.

Let’s block ads! (Why?)

Kasper On BI

life, more, simplify, Variables
  • Recent Posts

    • The Easier Way For Banks To Handle Data Security While Working Remotely
    • 3 Ways Data Virtualization is Evolving to Meet Market Demands
    • Did you find everything you need today?
    • Missing Form Editor through command bar in Microsoft Dynamics 365
    • I’m So Excited
  • Categories

  • Archives

    • April 2021
    • March 2021
    • February 2021
    • January 2021
    • December 2020
    • November 2020
    • October 2020
    • September 2020
    • August 2020
    • July 2020
    • June 2020
    • May 2020
    • April 2020
    • March 2020
    • February 2020
    • January 2020
    • December 2019
    • November 2019
    • October 2019
    • September 2019
    • August 2019
    • July 2019
    • June 2019
    • May 2019
    • April 2019
    • March 2019
    • February 2019
    • January 2019
    • December 2018
    • November 2018
    • October 2018
    • September 2018
    • August 2018
    • July 2018
    • June 2018
    • May 2018
    • April 2018
    • March 2018
    • February 2018
    • January 2018
    • December 2017
    • November 2017
    • October 2017
    • September 2017
    • August 2017
    • July 2017
    • June 2017
    • May 2017
    • April 2017
    • March 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    • October 2016
    • September 2016
    • August 2016
    • July 2016
    • June 2016
    • May 2016
    • April 2016
    • March 2016
    • February 2016
    • January 2016
    • December 2015
    • November 2015
    • October 2015
    • September 2015
    • August 2015
    • July 2015
    • June 2015
    • May 2015
    • April 2015
    • March 2015
    • February 2015
    • January 2015
    • December 2014
    • November 2014
© 2021 Business Intelligence Info
Power BI Training | G Com Solutions Limited