Forum Discussion
Sum everything within a date range (cumulatively)
- 1 year ago
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)
)
)
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,
- Walt10101 year ago
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.