Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
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!
Solved! Go to 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.
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."
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.
Thanks a lot!
@Anonymous Use Sequential and then it is a cake walk: (1) Sequential - Microsoft Power BI Community
Check out the July 2025 Power BI update to learn about new features.
User | Count |
---|---|
25 | |
10 | |
7 | |
6 | |
6 |
User | Count |
---|---|
30 | |
11 | |
11 | |
10 | |
6 |