Forum Discussion
Calculate Year over Year comparison dynamically based on the [year] slicer
Hello All,
I have a question regarding dynamically calculate the year over year comparison.
Suppose I have a table that look like this:
| Sales: | ||||
| 2019 | 2020 | 2021 | 2022 | |
| NY | 400K | 385K | 800K | 750K |
| NJ | 200K | 300K | 250K | 400K |
| CT | 150K | 400K | 280K | 300K |
and there is a slice where I can select what is the current year.
if I would like to construct a table that dynamically displays my current year selection and automatically populates the sales for the previsouly year and the sales increase (decrease), for example:
[Year] selected 2021
| Current Year | Previous Year | Increase (Decrease) | |
| NY | 800K | 385K | 415K |
| NJ | 250K | 300K | (50K) |
| CT | 280K | 400K | (120K) |
How can I achieve this?
Thank you all for your help!
Hi Anonymous ,
There's a lot of ways to do this, here's one way:
1) To use time intelligence measures in DAX you need a date table. I created a basic one and marked as the date table using this code:
TableDT =VAR MinYear = YEAR ( MIN ( Table1[Date] ) )VAR MaxYear = YEAR ( MAX ( Table1[Date] ) )RETURNADDCOLUMNS (FILTER (CALENDARAUTO( ),AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )),"Month Name", FORMAT ( [Date], "mmmm" ),"Month Number", MONTH ( [Date] ))2) Next I created three measures:2a)Current Year = SUM(Table1[Value])2b)Last Year = CALCULATE([Current Year],SAMEPERIODLASTYEAR(Table1[Date]))2c)Chg Year = [Current Year] - [Last Year]That gives me these results:Matrix visual with year slicerSimple Date Table for exampleHere's the PBIX file if you want to review in your desktop application.If you found this helpful, please mark as a solution so others can find it please! Always glad to help! Tom 😀
6 Replies
- tom480
Resolver I
Hi Anonymous ,
There's a lot of ways to do this, here's one way:
1) To use time intelligence measures in DAX you need a date table. I created a basic one and marked as the date table using this code:
TableDT =VAR MinYear = YEAR ( MIN ( Table1[Date] ) )VAR MaxYear = YEAR ( MAX ( Table1[Date] ) )RETURNADDCOLUMNS (FILTER (CALENDARAUTO( ),AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )),"Month Name", FORMAT ( [Date], "mmmm" ),"Month Number", MONTH ( [Date] ))2) Next I created three measures:2a)Current Year = SUM(Table1[Value])2b)Last Year = CALCULATE([Current Year],SAMEPERIODLASTYEAR(Table1[Date]))2c)Chg Year = [Current Year] - [Last Year]That gives me these results:Matrix visual with year slicerSimple Date Table for exampleHere's the PBIX file if you want to review in your desktop application.If you found this helpful, please mark as a solution so others can find it please! Always glad to help! Tom 😀- DEI-JasonPNew Member
Three years later, but thank you so much! ESPECIALLY for sharing that PBIX file!
- AnonymousNot applicable
hi tom480 ,
Thank you so much for the response. I am fairly new to PBI community and did not understand everything you did to make the magic happen, but I enjoy learning pbi and am looking forward to learning pbi from you in the future.
Thank again.
- tom480
Resolver I
hi Anonymous - if you need help with anything, always here to help you! I enjoy solving puzzles. Have a great weekend! Tom 😀
- Ashish_Mathur
Super User
- NickLiuFrequent Visitor
here you go! You may also creat 2 cards to show Current Year vs. Previous Year, and put 2 cards on top of the column headers to make it more user-friendly. full step-by-step instructions: