Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
ianhyj
Regular Visitor

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:    
 2019202020212022
NY400K385K800K750K
NJ200K300K250K400K
CT150K400K280K300K

 

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 YearPrevious YearIncrease (Decrease)
NY800K385K415K
NJ250K300K(50K)
CT280K400K(120K)

 

How can I achieve this? 

 

Thank you all for your help!

1 ACCEPTED SOLUTION
tom480
Resolver I
Resolver I

Hi @ianhyj ,

 

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] ) )
RETURN
ADDCOLUMNS (
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 slicerMatrix visual with year slicer
 
Simple Date Table for exampleSimple Date Table for example
 
Here'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 😀

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
ianhyj
Regular Visitor

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.

hi @ianhyj - if you need help with anything, always here to help you! I enjoy solving puzzles. Have a great weekend!  Tom 😀

tom480
Resolver I
Resolver I

Hi @ianhyj ,

 

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] ) )
RETURN
ADDCOLUMNS (
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 slicerMatrix visual with year slicer
 
Simple Date Table for exampleSimple Date Table for example
 
Here'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 😀

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.