# Trailing 4 quarters DAX calculation on an aggregated level

• ### Question

• Hi,

I am having difficulties with the calculation of the last 4 quarters in DAX. I am using a standard fact table and a separate time table (Time). The calculation works fine on the quarterly level but I can't find a way to display the correct results on a total year level.

My calculation is as follows:

Dividends TTM:=CALCULATE([Dividend],
DATESBETWEEN(Time[Date],
LASTDATE(time[date])))

Thanks a lot for your help!

Tuesday, January 14, 2014 11:17 PM

• I proposed using -360 days knowing that data is only stored on a monthly level and always on the last of the month, so -360 is "OK" to use here but yes, its more of a dirty solution

after reconsidering the calculation this is probably the most correct way to calculate it:

```Dividends TTM:=CALCULATE(
[Dividend],
DATESINPERIOD(Time[Date], MAX(Time[Date]), -4, QUARTER)
)
```

-gerhard

- www.pmOne.com -

Friday, January 17, 2014 11:23 AM
• Something like the following logic might work at both the year level and below

Dividends TTM:=CALCULATE([Dividend],
DATESBETWEEN(Time[Date],
LASTDATE(time[date])))

• Marked as answer by Wednesday, January 15, 2014 1:16 AM
Wednesday, January 15, 2014 12:42 AM
• "Quarter" is a parameter that specifies the time interval to use for the DATESINPERIOD function. More at: https://msdn.microsoft.com/en-us/library/ee634539.aspx?f=255&MSPPError=-2147217396.

Martin

http://powerbiexperts.com.au

• Marked as answer by Thursday, March 2, 2017 6:43 AM
Thursday, March 2, 2017 6:42 AM

### All replies

• Something like the following logic might work at both the year level and below

Dividends TTM:=CALCULATE([Dividend],
DATESBETWEEN(Time[Date],
LASTDATE(time[date])))

• Marked as answer by Wednesday, January 15, 2014 1:16 AM
Wednesday, January 15, 2014 12:42 AM
• Thanks a lot for your quick help! That has worked.
Wednesday, January 15, 2014 1:16 AM
• Hi Darren, On a closer look I still have problems. With your calculation I get now 5 quarters added up although the relevant dates are not within 4 quarters (31/3, 30/6, 30/9 and 31/12 each year):

Wednesday, January 15, 2014 2:36 PM
• So I think it's actually returning 4 Quarters and 1 day which you could fix with something like the following:

Dividends TTM:=CALCULATE([Dividend],
DATESBETWEEN(Time[Date],
LASTDATE(time[date])))

Wednesday, January 15, 2014 7:44 PM
• Thanks Darren! That has worked. In the meantime we also had another solution that was given by Gerhard Brueckl which also fixed the issue:

Dividends TTM:=CALCULATE([Dividend],

DATESBETWEEN(Time[Date],
LASTDATE(time[date])))

Is in your opionion this 4 quarter +1 day behaviour by design or a bug?

Thursday, January 16, 2014 1:48 PM
• Is in your opionion this 4 quarter +1 day behaviour by design or a bug?

It's definitely by design. The problem was with my logic, the function was returning exactly what it should.

It's just one of the pitfalls of suggesting expressions without having access to the data model to be able to verify the calculation.

The only potential issue that may exist with Gerhard's approach is that it's actually a rolling 360 days, so i you have any data in the first 5 days of the first quarter it will be excluded. I also don't know if you are worried about how the calculations will work at grains lower than quarter (ie. at the Month and Date levels) - as I think they may work differently

Friday, January 17, 2014 1:04 AM
• I proposed using -360 days knowing that data is only stored on a monthly level and always on the last of the month, so -360 is "OK" to use here but yes, its more of a dirty solution

after reconsidering the calculation this is probably the most correct way to calculate it:

```Dividends TTM:=CALCULATE(
[Dividend],
DATESINPERIOD(Time[Date], MAX(Time[Date]), -4, QUARTER)
)
```

-gerhard

- www.pmOne.com -

Friday, January 17, 2014 11:23 AM
• @Gerhard - Good work! I like your last expression the best. I think it should work in most circumstances and is better than both our first attempts. :)