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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Get average sales of last 14 days - DAX Query

Hello,
I am new to DAX. To get the sales of last 14 days, I wrote the below query

Sales in Last 14 Days =
var CurrentDay = DAY(TODAY())
var CurrentYear = YEAR(TODAY())
var MaxDayNum = CALCULATE(MAX('Calendar'[Day of Year]), ALL('Calendar'))
Return
SUMX(FILTER(ALL('Calendar'),
  IF(CurrentDay = 1, 'Calendar'[Day of Year] = MaxDayNum && 'Calendar'[Year] = CurrentYear-1,
'Calendar'[Day of Year] = CurrentDay - 14 && 'Calendar'[Year] = CurrentYear)), [Total Sales]
)

My question is - If the CurrentDay is any value between 2 to 14, Do I need to write conditions for each of them? Like 

IF(CurrentDay = 2, 'Calendar'[Day of Year] = 1 && 'Calendar'[Year] = CurrentYear-1,
'Calendar'[Day of Year] = CurrentDay - 14 && 'Calendar'[Year] = CurrentYear) ||
IF(CurrentDay = 3, 'Calendar'[Day of Year] = 2 && 'Calendar'[Year] = CurrentYear-1,
'Calendar'[Day of Year] = CurrentDay - 14 && 'Calendar'[Year] = CurrentYear) ||
IF(CurrentDay = 4, 'Calendar'[Day of Year] = 3 && 'Calendar'[Year] = CurrentYear-1,
'Calendar'[Day of Year] = CurrentDay - 14 && 'Calendar'[Year] = CurrentYear) ||
IF(CurrentDay = 5, 'Calendar'[Day of Year] = 4 && 'Calendar'[Year] = CurrentYear-1,
'Calendar'[Day of Year] = CurrentDay - 14 && 'Calendar'[Year] = CurrentYear) ||
IF(CurrentDay = 6, 'Calendar'[Day of Year] = 5 && 'Calendar'[Year] = CurrentYear-1,
'Calendar'[Day of Year] = CurrentDay - 14 && 'Calendar'[Year] = CurrentYear) ||
IF(CurrentDay = 7, 'Calendar'[Day of Year] = 6 && 'Calendar'[Year] = CurrentYear-1,
'Calendar'[Day of Year] = CurrentDay - 14 && 'Calendar'[Year] = CurrentYear) ||
IF(CurrentDay = 8, 'Calendar'[Day of Year] = 7 && 'Calendar'[Year] = CurrentYear-1,
'Calendar'[Day of Year] = CurrentDay - 14 && 'Calendar'[Year] = CurrentYear) ||
IF(CurrentDay = 9, 'Calendar'[Day of Year] = 8 && 'Calendar'[Year] = CurrentYear-1,
'Calendar'[Day of Year] = CurrentDay - 14 && 'Calendar'[Year] = CurrentYear) ||
IF(CurrentDay = 10, 'Calendar'[Day of Year] = 9 && 'Calendar'[Year] = CurrentYear-1,
'Calendar'[Day of Year] = CurrentDay - 14 && 'Calendar'[Year] = CurrentYear)......


1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Anonymous 
Are you trying to calculate the total sales for 14 days from today? If so please use

Contracts created in Last 14 Days =
VAR EndDate =
    TODAY ()
VAR StartDate = EndDay - 14
RETURN
    CALCULATE (
        [Total Sales],
        'Calendar'[Date] <= EndDate,
        'Calendar'[Date] >= StartDate
    )

View solution in original post

1 REPLY 1
tamerj1
Super User
Super User

Hi @Anonymous 
Are you trying to calculate the total sales for 14 days from today? If so please use

Contracts created in Last 14 Days =
VAR EndDate =
    TODAY ()
VAR StartDate = EndDay - 14
RETURN
    CALCULATE (
        [Total Sales],
        'Calendar'[Date] <= EndDate,
        'Calendar'[Date] >= StartDate
    )

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.