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 September 15. Request your voucher.

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
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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