Calculate Value For Dates Between

user3730569 picture user3730569 · Jun 11, 2014 · Viewed 8k times · Source

A few references:

  1. Microsoft's documentation on DATESBETWEEN.
  2. Somewhat similar question, though the answer and derivatives of the formula don't return the correct results.
  3. Microsoft's documentation on TODAY

Per the above Microsoft documentation, I'm trying to get a calculation for the last three months based on today's current date in SSAS Tabular model. First, I have no idea how to use SSAS and my company doesn't provide any learning material, so I've been reading through the MSDN documentation, which may not be the place to start, so if this is wrong, I'd appreciate being told so. For instance, with C# or Ruby, I can test code in a console to see if I get the result that I want, and I don't see how I can do that in SSAS Data Tools' DAX language - this is a GUI which gives users very little power over what they can do (it took me four hours to figure out how to access a dimension's properties). I am definitely a code monkey.

I tried using the below formula (and derivatives of it) because this is what it looks like Microsoft is doing in their example:

3MonthValue:=CALCULATE(SUM([MeasureOne])/SUM([MeasureTwo]),DATESBETWEEN(DateDimension[Date],DATEADD(DateDimension[Date],-3,MONTH),TODAY()))

The result, nothing. Of course, if I run similar SQL logic, I get the right results. I also used the provided SO example, though I suspect that's not exactly what I'm trying to achieve, and only obtained blanks as answers. Given that I need to calculate a formula between a certain time frame, which in SQL would be the WHERE clause, how do I translate this into DAX? In other words, what is DAX's WHERE and if CALCULATE isn't right, what's the correct approach?

Answer

user401093 picture user401093 · Jun 26, 2014

When you say it isn't working, how do you mean? The formula you are using refers to your date dimension's key as the starting date for your DATESBETWEEN function - this means if you are expecting the measure to populate a value, you'll need to be using a particular date in your pivot to establish context.

If you are trying to view the measure at design time, in the editor, there is no context so the measure wont populate.

Moreover, if in a pivot you're looking at a time context that includes more than one date, that also will not work. So say you are looking at a month, or a quarter. Both of these encompass what amount to multiple DateDimension[Date]'s - so again context cannot be established.

so to recap - measures which look at date ranges like DATESBETWEEN using a starting time context that is set to your dimensions time key will only show up in a pivot when the pivots data is filtered to a single date.

You can test this using the same function, but hard set the starting date by replacing DateDimension[Date] with a static date (or possibly TODAY()). The measure should show up in design time because the formula has all the information it needs to complete the calculation.