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
jdwalker5
Helper II
Helper II

Need measure to calculate if last year's week number equals this year's week number

Hello,

 

Just learning how to use custom DAX code to create measures in Power BI.  I'm trying to create a simple table visual using measures that calcualte and compare this year's results to last year's where the week numbers match.  My data already has a week number column that corresponds with my company's fiscal year so I do not need to use a date column.  Creating the measures for this year were easy because the data is limited to the current week.  In other words, the data source doesn't contain any data for dates beyond the current week that we're in now.  So for 2020 data, I created a measure for max week and called it current week.  Now I need to create a measure to pull last year's data for that same week number so that I can compare results year over year.  I cannot use max for last year's data because it has all 53 weeks.

This is basically how I would like to present the data:

 

YoY ComparisionWeek 40 This YearWeek 40 Last Year
Products Sold10085

 

Again, my data has a week number column.  I just need to find a way to get last year's data for the same week as this year's current week.  Any help is very much appreciated!

1 ACCEPTED SOLUTION
Nathaniel_C
Super User
Super User

Hi @jdwalker5 ,
If this solves your issue, please mark it so others can find it more easily!

 

Thank you,

 

Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

4 REPLIES 4
jdwalker5
Helper II
Helper II

This worked perfectly!  Thanks for the solution.  I actually used this for a separate measure to pull the week prior to max.  What was odd is I had to use MAX (data[Week]) - 10 to accomplish that.  Logically I would expect to be able to use MAX(data[week]) -1 but it continued to show data from the max week.  My week column is data time whole number so I couldn't figure out why -10 worked but I'll take what I can get.  Thanks again for your help!

Hi @jdwalker5 ,
You are very welcome!

Thank you,

Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Nathaniel_C
Super User
Super User

Hi @jdwalker5 ,
If this solves your issue, please mark it so others can find it more easily!

 

Thank you,

 

Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Nathaniel_C
Super User
Super User

Hi @jdwalker5 ,

 

 

So does your data have a year column?


Here is a quick table to show proof of concept.
last1.PNG
The measure 

Last Year Product Same Week =
VAR PastYear =
    MAX ( Data[Year] ) - 1
VAR CurrentWeek =
    MAX ( Data[Week] )
VAR _calc =
    CALCULATE (
        MAX ( Data[ProductSold] ),
        Data[Year] = PastYear,
        Data[Week] = CurrentWeek
    )
RETURN
    _calc


The result
last.PNG


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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.

Top Solution Authors