Forum Discussion
DAX - dynamic time range on trend chart issue
I understand that you want to create a dynamic time range on your trend chart based on the slicers. One possible solution is to use a separate calendar table for the slicer and create an inactive relationship with the main calendar table. Then you can use a measure to filter the last 12 months based on the selected date. You can find a detailed explanation and an example of this approach in this post.
Alternatively, you can use a measure to calculate the reference date based on the slicer selection and then use the DATESINPERIOD function to filter the last 12 months. For example, you can try something like this:
Reference Date =
VAR SelectedDate = MAX ( 'Calendar'[Date] )
VAR SelectedYear = YEAR ( SelectedDate )
VAR SelectedMonth = MONTH ( SelectedDate )
VAR SelectedQuarter = QUARTER ( SelectedDate )
VAR SelectedYearMonth = SelectedYear * 100 + SelectedMonth
VAR SelectedYearQuarter = SelectedYear * 10 + SelectedQuarter
RETURN
SWITCH (
TRUE (),
ISFILTERED ( 'Calendar'[Year] ) && ISFILTERED ( 'Calendar'[Quarter] ), EOMONTH ( DATE ( SelectedYearQuarter / 10, SelectedYearQuarter - SelectedYear * 10 * 3, 1 ), 0 ),
ISFILTERED ( 'Calendar'[Year] ) && ISFILTERED ( 'Calendar'[Month] ), EOMONTH ( DATE ( SelectedYearMonth / 100, SelectedYearMonth - SelectedYear * 100, 1 ), 0 ),
ISFILTERED ( 'Calendar'[Date] ), SelectedDate,
MAX ( 'Calendar'[Date] )
)
Last 12 Months Filter =
VAR ReferenceDate = [Reference Date]
VAR PreviousDates = DATESINPERIOD ( 'Calendar'[Date], ReferenceDate, -12, MONTH )
RETURN
IF ( 'Calendar'[Date] IN PreviousDates, 1, 0 )Then you can use the Last 12 Months Filter measure as a visual level filter and set it to 1.
I hope this helps you solve your issue. If you have any further questions, please let me know. 😊
Plz follow these links may solve your issue:
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.
In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.
Hi,
Thank you for your reply. Actually I want my trend chart shows last 12 months as default chart view but if any Date filter is selected then it should "reset" this 12 months period from the chart view and behave like a standard view that reacts on the slicer selection i.e if user selects "January" it should show January only and not January with previous 12 months...Hope this explanaition is clear now.