Forum Discussion
DAX - dynamic time range on trend chart issue
Thank you for the clarification. If you want your measure to dynamically adjust based on the slicer selection, showing the last 12 months by default but displaying the exact selected date range when slicers are applied, you can modify the DAX measure as follows:
Customized Total =
IF(
ISFILTERED('Date'[Date]),
CALCULATE(
SUM(Headcount[Employees]),
ALL('Date'),
'Date'[Date] >= MIN('Date'[Date]) && 'Date'[Date] <= MAX('Date'[Date])
),
CALCULATE(
SUM(Headcount[Employees]),
FILTER(
ALL('Date'),
'Date'[Date] <= MAX('Date'[Date]) && 'Date'[Date] > DATE(YEAR(MAX('Date'[Date])) - 1, MONTH(MAX('Date'[Date])), DAY(MAX('Date'[Date])))
)
)
)
In this modified measure:
- If a date filter is applied (ISFILTERED('Date'[Date])), it calculates the sum of employees based on the selected date range using MIN and MAX functions.
- If no date filter is applied, it calculates the sum of employees for the last 12 months, similar to before.
This measure should now display the default last 12 months of data when no slicers are applied, and it will adjust dynamically to show the exact selected date range when slicers are applied to the 'Date' column.
If I answered your question, please mark my post as solution, Appreciate your Kudos.
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
Thank you for your reply. unfortunately, it is still not working properly. I created a test model and replicated conditions and used a measure formula you shared.
When no 'Date' filter is applied it shows last 12 months and this is OK.
but when I want select a date which is ie before last 12 month's period it does not show any selected data...
Here is my data model looks like
I uploaded my pbix model for the review under following link
https://drive.google.com/file/d/1d1hnE4FxwguwN6YkPdsFSU6H_gBVBcxq/view?usp=drive_link
Regards
KB