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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Calculating Y-O-Y difference up until a certain date

Hi,

 

I have a bar chart that breaks down sales by year. I then overlayed a line on it that shows Y-O-Y % difference in sales. It works for 2019 and 2020, but 2021 is way off because there has only been 1 month and it's comparing 1 month of sales to the entire year of 2020's sales. I've been suggested to make my date table end at 2/2/21, but that does not work in my context because I need future dates in my date table. Is there a way to make it so that 2021 sales data is compared to the respective months of 2020 rather than the entire year in DAX?

 

 

My current measure (which doesn't fit my needs):

 

Sales YoY% =
VAR __PREV_YEAR = CALCULATE(SUM('Example'[Sales]), DATEADD('Dates'[Date], -1, YEAR))
RETURN
    DIVIDE(SUM('Example'[Sales]) - __PREV_YEAR, __PREV_YEAR)

YOY.PNG

1 ACCEPTED SOLUTION

Hi @Anonymous 

Please try this measure. I assume that the last date in your sales data is the last sales date rather than the end date of a year. (It means you only have sales data until a certain date you mentioned)

Sales YoY% 2 = 
VAR _lastSalesDate = MAX(Example[Date])
VAR _year = YEAR(_lastSalesDate)
VAR _month = MONTH(_lastSalesDate)
VAR _day = DAY(_lastSalesDate)
VAR __PREV_YEAR = CALCULATE(SUM('Example'[Sales]), DATESBETWEEN('Date'[Date],DATE(_year-1,1,1),DATE(_year-1,_month,_day)))
RETURN
    DIVIDE(SUM('Example'[Sales]) - __PREV_YEAR, __PREV_YEAR)

Kindly let me know if this helps.
Community Support Team _ Jing Zhang
If this post helps, please consider Accept it as the solution to help other members find it.

View solution in original post

5 REPLIES 5
VijayP
Super User
Super User

@Anonymous 

Now use the Slicer to see the comparison at which level you want! Currenly you Axis is Year hence it is showing Yeear over Year and now you need to change the max period avialble in the Latest year in the Slicer to see the comparison




Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
MY Blog || My YouTube Channel || Connect with me on Linkedin || My Latest Data Story - Ageing Analysis

Proud to be a Super User!


Anonymous
Not applicable

Thank you, I see now. Is there a way for the calculation to always be calculating from the max period available in this chart without the use of the slicer? I need the slicer to be selected for future dates for other charts in the dashboard.

Hi @Anonymous 

Please try this measure. I assume that the last date in your sales data is the last sales date rather than the end date of a year. (It means you only have sales data until a certain date you mentioned)

Sales YoY% 2 = 
VAR _lastSalesDate = MAX(Example[Date])
VAR _year = YEAR(_lastSalesDate)
VAR _month = MONTH(_lastSalesDate)
VAR _day = DAY(_lastSalesDate)
VAR __PREV_YEAR = CALCULATE(SUM('Example'[Sales]), DATESBETWEEN('Date'[Date],DATE(_year-1,1,1),DATE(_year-1,_month,_day)))
RETURN
    DIVIDE(SUM('Example'[Sales]) - __PREV_YEAR, __PREV_YEAR)

Kindly let me know if this helps.
Community Support Team _ Jing Zhang
If this post helps, please consider Accept it as the solution to help other members find it.

VijayP
Super User
Super User

@Anonymous 

In Place of DATEADD Function to Calculate the Previous Year Number use SAMEPERIODLASTYEAR Function to get What  you Want!




Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
MY Blog || My YouTube Channel || Connect with me on Linkedin || My Latest Data Story - Ageing Analysis

Proud to be a Super User!


Anonymous
Not applicable

Thanks for your response, I changed my measure to:
 
Sales YoY% =
VAR __PREV_YEAR = CALCULATE(SUM('Example'[Sales]), SAMEPERIODLASTYEAR('Dates'[Date]))
RETURN
    DIVIDE(SUM('Example'[Sales]) - __PREV_YEAR, __PREV_YEAR)
 
 
But it's still showing up as -97%. Did I do something wrong?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.