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

DAX measure to calculate previous 4 week number considering year change

Hello

 

I am trying to calculate and show a range of week numbers in a Power BI card visualization but the problem is that with the year change (week 52 going forwards to week 1) i want to always show the previous week number and the previous 4 weeks number. For example, it is currently week number 50. I want to show the range week number 46-49 in the card visualization. In 2022, if it is week 2, then the range should show: 50-1.

 

So, here is what i have done for the previous week number:

 

 

PreviousWeekNumber = WEEKNUM(TODAY(),21) - 1

 

 

And to find the previous 4 weeks number:

 

 

3 Weeks Before Previous Week Number = 
return IF([PreviousWeekNumber] == 1, 49, [PreviousWeekNumber]-3)
IF[PreviousWeekNumber] == 2, 50, [PreviousWeekNumber]-3)
IF([PreviousWeekNumber] == 3, 51, [PreviousWeekNumber]-3)
IF([PreviousWeekNumber] == 4, 52, [PreviousWeekNumber]-3)

 

 

But the latter is not working. I am new to Power BI. Any help is much appreciated!!

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

You can rewrite your 3 Weeks expression with a SWITCH:

3 Weeks Before Previous Week Number =
VAR W = [PreviousWeekNumber]
RETURN
    SWITCH (
        W,
        1, 49,
        2, 50,
        3, 51,
        4, 52,
        W - 3
    )

 

Personally, I'd try using modular arithmetic:

3 Weeks Before Previous Week Number =
MOD ( WEEKNUM ( TODAY (), 21 ) - 5, 52 ) + 1

View solution in original post

1 REPLY 1
AlexisOlson
Super User
Super User

You can rewrite your 3 Weeks expression with a SWITCH:

3 Weeks Before Previous Week Number =
VAR W = [PreviousWeekNumber]
RETURN
    SWITCH (
        W,
        1, 49,
        2, 50,
        3, 51,
        4, 52,
        W - 3
    )

 

Personally, I'd try using modular arithmetic:

3 Weeks Before Previous Week Number =
MOD ( WEEKNUM ( TODAY (), 21 ) - 5, 52 ) + 1

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
Top Kudoed Authors