PowerPivot time intelligent functions: why use ALL() and how to work around it

Posted by: Kasper de Jonge

Last week I got a most excellent question from Sasha at my question page. Using my timeintelligent function screencast Sasha created a workbook using  YTD  where he used one table with facts and dates in it. The problem he had was when created his time intelligent function he wasn’t able to use data from other columns than the date columns. In this blog post i´ll try to describe how the time intelligent functions work, what pitfalls are and how to solve them.

To be able to use a time intelligent function in DAX you use the Calculate function to group measures by a filter. With a time intelligent function you want to filter your values over a period of time (like YTD or previous month).  Most of the time you want the use these functions inside a pivottable where you use dates on the x or y-axis, the values inside the pivottable would show values per the current period context. This would logically result in the following DAX formula:

Dates YTD = CALCULATE(sum(Tablix1[nroforders]),DATESYTD(Tablix1[Date]))

This writes out to: you want the sum of Tablix1[nroforders] from the first Tablix1[Date] value of the year to the Tablix1[Date] belonging to the Tablix1[nroforders] in pivottable context. The YTD of the Tablix1[nroforders] from march 2009 would mean we need to take the sum of Tablix1[nroforders] from all rows from the start of year to march 2009, in the image below you can see a sample of the values that will be summed:

But when we add the formula to the measures and use it in a pivottable we see something strange:

As you can see the nroforders and the YTD formula result in the same values … this is not what we expected. The reason is because the time intelligent function requires an additional parameter, in a blog post at PowerPivotpro.com the PowerPivot product team gave the following answer to my question to why the result is not as expected:

When using Time intelligence functions like LASTDATE or DATESMTD, it is necessary to add another argument which is ALL (TimeTable). Without this only the selected dates are considered, and you can’t find the last month unless you are in the last month.

As you can see in the screenshot this is indeed what happens, the function only uses only the current date context in the sum.

Ok so we need to use ALL to get the results we want, this will result in the following syntax:

DatesYTD w All =CALCULATE(sum(Tablix1[nroforders]),DATESYTD(Tablix1[Date]),all(Tablix1))

This is indeed the result we expected, but having to use the ALL() function has a huge downside.  The ALL() function according to BOL:

Returns all the rows in a table, or all the values in a column, ignoring any filters that might have been applied. This function is useful for clearing filters and creating calculations on all the rows in a table.

This means when you use a filter or slicer this is being ignored by the time intelligent function that uses ALL(). As you can see below the results of the YTD are the same as unsliced:

There are two methods we can use to work around this problem:

  1. When you know what slicers and filters you want to work with you can use an ALL() alternative: ALLEXCEPT(). With ALLEXCEPT you can pass through “a list of columns for which context filters must be preserved”.
    In our case we would like to be able to slice on country. This would look like:
    DatesYTD w AllExcept =CALCULATE(sum(Tablix1[nroforders]),DATESYTD(Tablix1[Date]),allexcept(Tablix1,Tablix1[country]))
    I would say use this option if you already know what you want to slice on and don’t have much time to solve it properly.
  2. The proper and most flexible is option number 2. To be able to slice / filter on all the columns you can think of you should create a separate time table. This isn’t very user friendly and your end users will have a hard time grasping this. A few options i can think of to create this time table:
    1. Import the fact table with distinct on date columns (i hope your table isn’t too big ..)
    2. use excel to copy the date rows, remove duplicates and create a linked table (new dates won’t be added)
    3. Supply your users with a default time table in SharePoint/SSRS, they can import this by using the data feed option, use your DWH datetime table as source.

you should create a relationship on the datetime column between the fact table and the the imported time table (make sure your datetime columns have identical granularity, like year, month, day, otherwise the join wont find results). With this relationship in place you now are able to use ALL over the datetime table. When you use ALL() over the separate time table it no longer ignores filters / slicers over your  fact table.
The function would look like:
DatesYTD w All TT =CALCULATE(sum(Tablix1[nroforders]),DATESYTD(DateTable[Date]),all(DateTable))
You now are able to filter or slice all the columns from the fact table you want. In the RTM version of PowerPivot the ALL() in the DAX function is no longer required, the separate time table unfortunately still is.

The final workbook now looks like:

My conclusion is that Time intelligent functions still are a very powerful feature but i am really disappointed on its user-friendliness, while these workarounds are easy for IT/BI personnel to grasp and implement, end users will have a harder time implementing this. Maybe MS can implement a “add date time table” button in PowerPivot to automatically create a date time table to our PowerPivot tables to make it a little easier for end users to implement time intelligent functions.

3 Responses to PowerPivot time intelligent functions: why use ALL() and how to work around it

  1. Mike G. says:

    Great information..thanks!
    I am having trouble with two seemingly simple tasks.

    First Issue: I want a YTD measure and can’t seem to get it to work without using the All() filter.
    I’ve gathered from this site that in order to exclude the All() filter, a related “dates/calander” table must be used in the following function. (also a best practice from Rob’s book)

    calculate(SalesData[SaleAmount],DATESYTD(MyCalendar[Date]))
    *note that SalesData[SaleAmount] is a created measure that sums SalesData[SaleAmount]

    I’ve ensure that the SalesData and MyCalendar tables are related and each date column is a date type.

    This formula simply produces a sum of the sale amount and not a YTD. I’ve followed the formulas as I’ve seen them in Rob’s book and on this site..but no luck..what am I missing here?

    Second Issue: When I don’t use a dedicated dates table as follows:
    calculate(SalesData[SaleAmount],DATESYTD(SalesData[SaleDate]),All(SalesData))
    the YTD calculation works.

    However, if I use a product type/description in the pivot table row, the YTD are calculating based on the monthly period, not the prodcut type. In other words, the YTD is for all products during the time period and not the product type listed. I’m guessing that the All() filter is the reason but can’t get around that given my First Issue noted at the start of this post.

    Any guidance will restore my excitement with power pivot…I’ve been a bit discouraged given that what seems like a simple task has been so dificult.

    Thanks and Happy New Year.
    Mike G

    • powerpivotpro says:

      Hi Mike. Any chance you can send me the workbook? I’ll email you privately.

      • MikeG says:

        Hey Rob..thanks for the reply. Your suggestion in your email was right on target. The problem is solved and I’m excited about what power pivot has to offer!! Thanks

        Note to others – make sure that your date filters/slicers in the workbook pivot are from the calendar/dates table; don’t use date columns from any other table with values that will be manipulated (sales, etc).

Leave a Reply