Forum Discussion
Sum everything within a date range (cumulatively)
Noob here.
I have a table of dates, indicating dates on which employees took a sickness leave day. I need to calculate the sum of all the sickness leave days taken over the past 2 years, at any date. So I think I need a cumulative total, but with a filter on the aggregation function.
What I've tried is the following:
CALCULATE(COUNT(sicknesstable[sicknessdate],FILTER(sicknesstable,DATEDIFF(sicknesstable[sicknessdate],today, DAY)<720)
but when I put this into a table visual, with the column of dates followed by this value, it doesnt work. I understand that the date range isn't 'moving along', but I dont know how to fix it. Any help would be appreciated!
Walt1010 , Try using
CumulativeSicknessLeave =
VAR CurrentDate = MAX(sicknesstable[sicknessdate])
RETURN
CALCULATE(
COUNT(sicknesstable[sicknessdate]),
FILTER(
ALL(sicknesstable),
sicknesstable[sicknessdate] <= CurrentDate &&
sicknesstable[sicknessdate] >= DATEADD(CurrentDate, -2, YEAR)
)
)
3 Replies
- DataNinja777
Super User
Hi Walt1010 ,
To calculate the rolling sum of sickness leave days over the past two years dynamically for any given date, you need a measure that filters the sickness leave dates within a 720-day window relative to each row’s date in the table visual. Your current approach does not dynamically adjust the date range as the table progresses. The correct way to achieve this is by using a CALCULATE function that filters the sickness leave table based on a MAX date reference from a calendar table.
Here’s the correct DAX formula:
Sickness Leave 2Y Rolling Total = VAR CurrentDate = MAX('Calendar'[Date]) RETURN CALCULATE( COUNT(sicknesstable[sicknessdate]), sicknesstable[sicknessdate] >= CurrentDate - 720 && sicknesstable[sicknessdate] <= CurrentDate )This formula defines CurrentDate as the maximum date within the current row context, ensuring that for each row in the table visual, the calculation dynamically considers the past 720 days. The CALCULATE function then filters the sickness leave table to count only those entries where the sicknessdate falls within the rolling two-year window. When used in a table visual alongside Calendar[Date], this measure will update per row, maintaining the cumulative logic as the dates progress. To ensure smooth functionality, make sure you have a separate Calendar table driving the date logic in your visual. If performance becomes an issue due to a large dataset, consider pre-aggregating sickness leave counts at the monthly level before applying the rolling sum logic.
Best regards,
- Walt1010
Helper V
Thanks fr your answer. I've tried it, exactly as you have suggested. What happens when I try to display it in a visual with fields as follows:
Name, Calendar[Date], Sickness Leave 2 Y Rolling Total
The results I get are:
The first row displayed is the first abscence date for the person, but with a 2 for the Sickness Leave 2 Y Rolling Total. The same number repeated despite the actual absence dates.
I think something is not working as regards the row context, so the MAX date used is not the date obtained from the Calendar. Any odeas would be appreciated.
- bhanu_gautam
Super User
Walt1010 , Try using
CumulativeSicknessLeave =
VAR CurrentDate = MAX(sicknesstable[sicknessdate])
RETURN
CALCULATE(
COUNT(sicknesstable[sicknessdate]),
FILTER(
ALL(sicknesstable),
sicknesstable[sicknessdate] <= CurrentDate &&
sicknesstable[sicknessdate] >= DATEADD(CurrentDate, -2, YEAR)
)
)