PowerPivotPro is Coming to Atlanta

March 20 - 22, 2018

Registration for 2018 Public Training is now open!


**Use the discount code “3ORMORE” when signing up 3 or more people.

MARCH 20 - 21

Foundations: Power Pivot & Power BI

Instructor: Austin Senseman

Super charge your analytics and reporting skills with Microsoft’s dynamic duo. Designed to handle huge volumes of data, these tools will transform the way you work! Two Days in our class and you are EMPOWERED!

  • Learn Microsoft’s secret weapon behind Power Pivot & Power BI: DAX
  • Taught by Austin Senseman – PowerPivotPro Partner
  • You don’t need to be an IT professional – most of our students come from an Excel background
Atlanta Public Training Classes
Atlanta Public Training Classes

MARCH 20 - 21

Level Up Series: Advanced DAX

Instructor: Ryan Sullivan

The Advanced DAX Course was such a hit in the first half of 2017 that we’ve expanded the course to 2 days!


  • This advanced DAX training class is taught completely in Power BI Desktop
  • Students are encouraged to take our Foundations course and have hands on experience with the DAX language
  • Taught by Ryan Sullivan – Principal Consultant
  • Class material drawn from usage of Advanced DAX applications while consulting with hundreds of international firms


Level Up Series: Power Query for Excel & Power BI

Instructor: Ryan Bergstrom

The second class in the series is our Level Up Series is Power Query for Excel & Power BI.

  • Students are encouraged to take our Foundations course and have hands on experience with Power Query in Excel or Power BI Desktop
  • Taught by Ryan Bergstrom – Principal Consultant and Trainer
  • We will cover common to unique business challenges made easy with Power Query’s data wrangling capabilities
  • Intermediate to Advanced Level Power Query best practices distilled into easy to understand patterns to apply to your most common business challenges
Atlanta Public Training Classes
PowerPivotPro Logo

Woops I'm sorry, did I change your Filter Context ?

By Kasper de Jonge, original post at PowerPivotblog.nl

I got an excellent question last week on the ask my question page that brought me new understanding of DAX. So finally a new interesting (I hope) blog post on DAX.

Let’s say I have a set of sales per week of a specific brand:

Now I want to have the average of sales per week (slicable by brand) and compare it to the average of the last 3 weekly totals. I prepare  the pivottable :

Now for the moving average of the last 3 weeks we are going to do some interesting DAX. First of all we want to use the current “Week No” as a base value, we need to check if our formula has one week in the current row context, otherwise we cannot get a moving average over a specific week. We check the number of values in the Sales[Week No] column using:

if(countrows(values(Sales[Week No])) = 1

Next we want to get the values of sales from this week and the previous two week to get a grouped average from. We can get a grouped average on Sales[Week No] of the Sales using the AVERAGEX function:

AVERAGEX(VALUES(Sales[Week No]), Sales[Sum of Sales])

Notice we use VALUES(Sales[Week No]) in function to group on the distinct values of Sales[Week No]. This function on its own will only return the average of the current Sales[Week No], we need to override the filter context to return the last current and previous two week. To override the current context with a broader set of values we can use the Calculate function.

This Calculate function will return a table of the current week plus the previous two:

CALCULATE(<grouped AVG function> , Sales[Week No] <= VALUES(Sales[Week No]) &&  Sales[Week No] > VALUES(Sales[Week No])-3)

If you combine the two we get the overall DAX function:

=if(countrows(values(Sales[Week No])) = 1,
		 AVERAGEX(VALUES(Sales[Week No]), Sales[Sum of Sales])
		 ,Sales[Week No] <= VALUES(Sales[Week No]) &&  Sales[Week No] > VALUES(Sales[Week No])-3
, blank())

This will give the following Pivottable:

The most interesting thing to notice here is that VALUES(Sales[Week No]) contains two different values in a single function. Let’s take a look at the DAX function again:

=if(countrows(values(Sales[Week No])) = 1,
		 AVERAGEX(VALUES(Sales[Week No]), Sales[Sum of Sales])
		 ,Sales[Week No] <= VALUES(Sales[Week No]) &&  Sales[Week No] > VALUES(Sales[Week No])-3
, blank())

The red function will return the actual row and filter context that we are in. To see what this row and filter context is all about check this link. The calculate function will change the context of the first argument, in this case we use AVERAGEX in the first argument to group on the distinct Sales[Week No] of the changed context. The blue function returns 3 Sales[Week No] to do the Average over.

It can look confusing but again shows the amazing capabilities of the CALCULATE and AVERAGEX functions. It took me some time to come up with CALCULATE in this particular scenario (thanks to my colleague Jeffrey who also made a in depth blog post about this subject. )

  Subscribe to PowerPivotPro!


This Post Has 9 Comments

  1. Thanks for this post which is really interesting. I wonder however how you manage the change of year. With this method you have issues on W1 or W52, don’t you? Is something similar possible using TimeIntelligence functions?

  2. Thanks Kasper! I’ve just arrived to the same result using CALCULATE(SUM….,)/Calculate(countrows(summarize(fact,dimdate[week])),….)
    Your calculation is much simlier and “straightforward”
    Michael Shparber

    1. Hi Michael,
      Could you please explain how you did it using your date dimension since I’m unable to get the calculation to work as per my comment below?



  3. Thanks for this post, awesome and simple explanation. But what if you filter down to brand and it does not have sales in one of the weeks? This formula would average the last 3 weeks regardless (which could include a week without sales values), and not the last 3 weeks where sales existed…correct?
    Ex: Brand 1
    Sales Wk1=80
    Sales Wk2=No value
    Sales Wk3=90
    Sales Wk4=100
    This formula of Avg last 3 wks would return 95 instead of 90, I’m very curious how you would adjust the formula to average the last 3 wks with values, that would return 90 in this example?

  4. Could you please explain how to apply this using a date dimension instead of Sales[Week No] from your fact table? When I try with the following it doesn’t work since there are multiple values for CalendarWeek.

    test:=IF(countrows(values(DimDate[CalendarWeek]))=1,calculate(averagex(Values(DimDate[CalendarWeek]),[RejectCount]),DimDate[CalendarWeek] VALUES(DimDate[CalendarWeek])-3),blank())


  5. Anyone know how to make this work with dynamic date arguments to get a rolling total?

  6. How to calculate Average of MTD value only displayed in Pivot table (if its 5th day of the month, I would like to show average of specific to those days)

  7. How would you include something so that it does not factor in blank rows. For example Week 1 =$20, Week 2 = $30, Week 3 = $35, Week 4 = Blank (closed and do not want to count in with moving average) Week 5 = $50

Leave a Comment or Question