Forum Discussion
Trailing 12 month YOY Comparison
Example: To view data just for the day, for a week, for a month, for YTD, Full year. After that, I want to add Trailing 12 months. but somewhere code is not working.
I do have separate year filters to dynamically decide which year will be This year vs Last year.
Time Period=
Any help is appreciated!
Anonymous Ashish_Mathur Anonymous
7 Replies
- Ritaf1983
Super User
Hi Jidnyasa2904
Please provide a workable sample data and your expected result from that. It is hard to figure out what you want to achieve from the description alone.
- Jidnyasa2904
Helper I
Hi Ritaf1983 Anonymous,
Yes. The above Dax Code is to generate a Time Interval Selector for selecting different time ranges in Power BI.
Assuming I have 5 Grocery stores, filtered to Year-to-Date.Stores This Year Last Year 1 $ 20,000 $ 18,000 2 $ 15,020 $ 16,036 3 $ 19,000 $ 18,000 4 $ 17,560 $ 21,001 5 $ 17,000 $ 15,550 - I have a Year Selection filter, so the This Year column is a measure that looks at the max year similar to last year.
- I have the Dax code for Time Interval Selection ( Previous Day, Week to Date, Month to Date, Year to Date, Full Year, Trailing 12 months).
- I think the Trailing 12-month logic is not working in the above code because I have the Year Selection filter. For example, This year is 2024, so the Trailing 12 month cannot grab data from 2023. Similar to last year as well.
- AnonymousNot applicable
Hi All,
Firstly Ritaf1983 thank you for your solution!
And Jidnyasa2904 ,I think the meaning of your DAX code is to generate a Time Interval Selector for selecting different time ranges in Power BI. The function of this time interval selector is to help the user quickly switch between different time perspectives to view the data, right?In our attempt, we applied a similar approach to help you achieve this requirement, first we create a new table to provide filtering to the user, and then we filter based on the data in the table on the basis of our Date Table Make sure that this value of yours is dynamic Hopefully, this will help you with your question!
TimePeriodChoices = DATATABLE( "TimePeriod", STRING, { {"Year to Date"}, {"Month to Date"}, {"Week to Date"}, {"Trailing 12 Months"} } )Dynamic Total Sales = VAR SelectedPeriod = SELECTEDVALUE(TimePeriodChoices[TimePeriod], "Year to Date") VAR TodayDate = TODAY() VAR YearStart = DATE(YEAR(TodayDate), 1, 1) VAR MonthStart = DATE(YEAR(TodayDate), MONTH(TodayDate), 1) VAR WeekStart = TodayDate - WEEKDAY(TodayDate, 2) + 1 VAR Trailing12MonthsStart = EDATE(TodayDate, -12) RETURN SWITCH( SelectedPeriod, "Year to Date", CALCULATE(SUM('Sales Table'[Sales]), 'Date Table'[Date] >= YearStart && 'Date Table'[Date] <= TodayDate), "Month to Date", CALCULATE(SUM('Sales Table'[Sales]), 'Date Table'[Date] >= MonthStart && 'Date Table'[Date] <= TodayDate), "Week to Date", CALCULATE(SUM('Sales Table'[Sales]), 'Date Table'[Date] >= WeekStart && 'Date Table'[Date] <= TodayDate), "Trailing 12 Months", CALCULATE(SUM('Sales Table'[Sales]), 'Date Table'[Date] >= Trailing12MonthsStart && 'Date Table'[Date] <= TodayDate), BLANK() )If you have more recent questions, you can contact us at any time, we will be the first time to reply to you after receiving your message, looking forward to your reply!
Hope it helps!
Best regards,
Community Support Team_ Tom ShenIf this post helps then please consider Accept it as the solution to help the other members find it more quickly.
- Jidnyasa2904
Helper I
Anonymous I have made some changes to the file you have attached. But I am unable to upload it.
- AnonymousNot applicable
Hi Jidnyasa2904 ,
You can take your modified pbix file, store it in the cloud and send me the link so I can understand your needs better and give you an accurate solution!
How to provide sample data in the Power BI Forum - Microsoft Fabric Community
Looking forward to your reply!