Minimize
03

When developing DAX calculations in order to create meaningful business metrics you normally want to remove or refine an existing filter in the current filter context.  For example, when calculating the sales contribution of a country to the sales across all countries in the Adventure Works sample database one could use:

SUM(FactInternetSales[SalesAmount])/

CALCULATE(SUM(FactInternetSales[SalesAmount]),ALL(DimDate[CalendarYear]))

This would yield the following result




So far so good.  However, what if we want to pick only certain years and have the expression dynamically recalculate only for the selected years?  We could add a slicer with the year values. This won’t work, though: the ALL function will force the filter context to evaluate across all years – regardless of slicer selection:




As you can see, DAX computes the contribution across the total for all years in the in-memory DimDate table.  The result incorrectly adds up to only 54.82% as the denominator is still taking into account all years, not just the selected ones.  How can we make this dynamic, to use only the selected values on the slicer? 

The solution is to take advantage of the inherent flexibility offered by calculated columns. We can create another ‘Year’ column in our data model, and then add another context to the calculation that will use ALL years but only within the boundaries defined by the user defined filters.  The nice thing about this solution is that the output only takes into account the years in the slicer (in other words, the query context), instead of either taking ALL years or just the CURRENT year.

So how to do this?

1)      Add a calculated column called "QueryContextYear" to the in-memory FactResellerSales table, using this DAX expression :

    RELATED(DimDate[CalendarYear])

2)      Replace the slicers in the spreadsheet with this column:  Instead of using the CalendarYear from DimDate in the slicer, use the new QueryContextYear from FactResellerSales

That’s it!  Now the result is dynamic to your slicer selection: the denominator will be calculated only using the selected years.




The reason why works is that the ALL function in the DAX expression removes the filters only on the DimDate[CalendarYear] column, but not in FactResellerSales[QueryContextYear]; The output of a calculation in DAX
is always the result of stacking all the filtered tables.  So while FactResellerSales[QueryContextYear] is being filtered, DimDate[CalendarYear] is not; And the intersection of these filters determines the calculation output.

Posted in: Blog

Comments

There are currently no comments, be the first to post one.

Post Comment

Only registered users may post comments.

MSBiCentral Bloggers Minimize

Latest Entries Minimize
Using VBA to enhance interactivity of PowerPivot reports by JavierGuillen
Presenting for PowerPivot Analytics Expo (hosted by SSWUG on July 15) by JavierGuillen
PowerPivot Analytics Expo
Quartile, Percentile and Median in PowerPivot by JavierGuillen
Using MDX in a PowerPivot model
Median calculation in PowerPivot / DAX by JavierGuillen
How to calculate median over a PowerPivot measure
Calculating moving averages in DAX by JavierGuillen
After SSRS 2008 R2 Upgrade: Some Reports Hang by wsnyder
You just upgraded to SSRS 2008 R2 and some reports are hanging up, while others seem to work fine. How do you debug for this, and what is the fix?
SSRS SystemOverflowexception: Value was either too large or too small for an Int32 by wsnyder
You install or upgrade to SSRS 2008R2 Gold, and get the following error "SSRS SystemOverflowexception: Value was either too large or too small for an Int32". Here is what is going on....
PASS Virtual Chapter Presentation Slides on PowerPivot / DAX by JavierGuillen
Presentation slides for PASS VC webinar on PowerPivot and DAX
Business Intelligence Prototyping with PowerPivot by JavierGuillen
PowerPivot can be a really powerful tool for prototyping Business Intelligence projects.
Speaking for PASS Business Intelligence Virtual Chapter by JavierGuillen
Page 1 of 3First   Previous   [1]  2  3  Next   Last   

Copyright 2004-2012 MSBICentral.com Terms Of Use Privacy Statement