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.

X

## This Post Has 9 Comments

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

1. powerpivotpro says:

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

1. 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).

2. Stellan says:

Hopefully this is a good place to post this…

I am giving the TOTALYTD a try. Here is my scenario where I struggle. I have a DIMDATE table, a INPUT_FACT_INTERNAL and a INPUT_FACT_EXTERNAL tables and both have a field DOLLARS. I created a ‘measure’ called TOTALDOLLARS in the DIM_DATE table that sums up both DOLLARS from these two tables. I now want to use the following function to get the TOTALYTD: =TOTALYTD(SUM(dim_customer[TotalDollars]),dim_date[FullDate], ALL(dim_date)). But I get the following error message: Column ‘TotalDollars’ in table ‘dim_customer’ cannot be found or may not be used in this expression. But it is there, as I described I created the ‘measure’ and it is listed under the DIM_DATE table. I tried the TOTALYTD function with using just one DOLLAR field ‘=TOTALYTD(SUM(input_fact_INTERNAL[Dollars]),dim_date[FullDate], ALL(dim_date))’ and that works. Any help is appreciated. Thank you. Stellan

3. Smyrv says:

Excellent! This worked perfectly after struggling with DATESYTD for half a day… The ALL(DateTable) did the trick for my problem, wonder why Russo doesn’t mention it in an example he wrote online?

1. powerpivotpro says:

This is a very old post. I don’t think the ALL() is required anymore.

Have you marked your Calendar/Date table as a Date table? It’s a button on the Power Pivot ribbon. That may clear up your problem without the ALL(), give it a shot.

4. fuPz says:

Hi, I’m fairly new to PPVT – I’m using Cubefromulas to query my PPVT tabe

(e.g. =CUBEVALUE(“PowerPivot Data”,\$AC\$2,\$AC\$3,\$AC\$4,CUBEMEMBER(“PowerPivot Data”,”[LU_ReportingPeriod].[RP_Quarter_Year].&[“&E\$5&”]”),CUBEMEMBER(“PowerPivot Data”,”[Measures].[NOI (QTR)]”)) to pull specific values.

I run into a problem when I try to do that against a YTD calc I setup in PPVT – Here’s the formula:

=CALCULATE(Sum(FACT_Main[Building_Block_Value]), FACT_Main[Building_Block]=”PL_NOI_QTR”, DATESYTD(LU_Date_Master[Calendar_Date]), ALL(LU_Date_Master))

When I throw it in a pivot table and bring in the calendar data (from my master data table) it works fine, however when I try to a cube forumla

=CUBEVALUE(“PowerPivot Data”,\$AC\$2,\$AC\$3,\$AC\$4,CUBEMEMBER(“PowerPivot Data”,”[LU_Date_Master].[Calendar_Date].&[“&M\$5&”]”),CUBEMEMBER(“PowerPivot Data”,”[Measures].[TEST]”))

it returns N/A. the reference for the Calendar_Date points to a cell with a specific date.

Thanks

1. Rob Collie says:

Doesn’t matter, the post is out of date and you don’t need to use ALL() anymore. Thanks for letting us know however.