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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

DAX Last 4 Weeks from Week Number not Date Calculation

Total Sales Value Last 4 Weeks = 
VAR CurrentWeek = SELECTEDVALUE( Dates[Week_Number] )
VAR CurrentYear = SELECTEDVALUE( Dates[Year] )
VAR MaxWeekNumber = CALCULATE( MAX ( Dates[Week_Number] ), ALL( Dates ))
VAR LastWeekSales = SUMX(
                        FILTER( ALL( Dates ),
                            IF( CurrentWeek = 1,
                                Dates[Week_Number] = MaxWeekNumber && Dates[Year] = CurrentYear - 1,
                                Dates[Week_Number] = CurrentWeek - 1 && Dates[Year] = CurrentYear )),
                            [Total Sales Value] )
VAR Last2WeekSales = SUMX(
                        FILTER( ALL( Dates ),
                            IF( CurrentWeek = 1,
                                Dates[Week_Number] = MaxWeekNumber && Dates[Year] = CurrentYear - 1,
                                Dates[Week_Number] = CurrentWeek - 2 && Dates[Year] = CurrentYear )),
                            [Total Sales Value] )
VAR Last3WeekSales = SUMX(
                        FILTER( ALL( Dates ),
                            IF( CurrentWeek = 1,
                                Dates[Week_Number] = MaxWeekNumber && Dates[Year] = CurrentYear - 1,
                                Dates[Week_Number] = CurrentWeek - 3 && Dates[Year] = CurrentYear )),
                            [Total Sales Value] )

RETURN
[Total Sales Value] + LastWeekSales + Last2WeekSales + Last3WeekSales 

 

Hi All

 

I am using the above DAX to calculate the last 4 weeks Sales off Week and Year fields in my date table
Any pointers on how this can be simplified, doesn't feel like this can possibly be the most efficient way!

I want to also write this for the last 12 weeks, so hoping for a more straightforward way to write it!

Any help appreciated!

1 ACCEPTED SOLUTION

@Anonymous If you have a Sequential week column then you do this:

Total Sales Value Last 4 Weeks = 
VAR __CurrentWeek = MAX( Dates[Sequential] )
VAR __CurrentMinus4 = __CurrentWeek - 4
RETURN
SUMX(FILTER(ALL(Dates), [Sequential] <= __CurrentWeek && [Sequential] >= __CurrentMinus4),[Total Sales Value])

Seems self explanatory.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

How would that incorporate into my scenario?

@Anonymous Create a Sequential column using the provided DAX. Then the last 4 week is always just -4 and last twelve are -12. You don't have to worry about years because the week number is sequential across years.

 

"Suppose you have a list of years and weeks in those years (or months). Often, it would be nice to have a sequential number for this kind of data in order to make certain math calculations easier for comparisons between discrete units of time, etc."



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

I'm not with you

 

I am trying calculate the total of the last 4 weeks and the total of the last 12 weeks, unsure how sequential helps here?

@Anonymous If you have a Sequential week column then you do this:

Total Sales Value Last 4 Weeks = 
VAR __CurrentWeek = MAX( Dates[Sequential] )
VAR __CurrentMinus4 = __CurrentWeek - 4
RETURN
SUMX(FILTER(ALL(Dates), [Sequential] <= __CurrentWeek && [Sequential] >= __CurrentMinus4),[Total Sales Value])

Seems self explanatory.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Thanks a lot!

Greg_Deckler
Community Champion
Community Champion

@Anonymous Use Sequential and then it is a cake walk: (1) Sequential - Microsoft Power BI Community



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.