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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

calculate sales for the same number of days prior to the selected date range

Hi Everyone,

I have a fact table which contains Sales and Dates, But there is no Date and Sales for Weekends.
I created a Custom Date Table using following DAX

 

 

Calendar_Dates = 
VAR DateRange = CALENDARAUTO()

RETURN 
ADDCOLUMNS(
    DateRange,
    "Year",YEAR([Date]),
    "Month",FORMAT([Date],"mmmm"),
    "Day",FORMAT([Date],"yyyy-mm-dd"),
    "Year Month", FORMAT([Date],"yyyy-mmmm"),
    "YearMonthSort",YEAR([Date])*100 + MONTH([Date]),
    "ShortName",FORMAT([Date],"ddd"),
    "IsWeekDay", NOT WEEKDAY( [Date] ) IN {1,7}
)

 

 

 there is one-Many relation ship between my Fact and Calendar table.

Problem Statement:
I need to calculate sales for the same number of days prior to the selected date range.
For Example if user selected 27 May 2024 then it must calculate sales for 24 May 2024.
If user selected 20 May 2024 till 24 May 2024 then it must calculate Sales for 13 May 2024 till 17 May 2024.

to Achive this I already created some measures

 

 

SelectedRangeStart = MIN(Calendar_Dates[Date])

 

 

 

SelectedRangeEnd = MAX(Calendar_Dates[Date])

 

 

 

NumberOfDaysSelected = 
CALCULATE(
    COUNTROWS('Calendar_Dates'),
    DATESBETWEEN('Calendar_Dates'[Date], [SelectedRangeStart], [SelectedRangeEnd]),
    NOT(WEEKDAY('Calendar_Dates'[Date], 2) IN {6, 7})  -- Exclude weekends
)

 

SalesPreviousPeriod = 
VAR StartDate = [SelectedRangeStart]
VAR DaysCount = [NumberOfDaysSelected]
VAR PreviousDates = 
    CALCULATETABLE(
        TOPN(DaysCount, 
            FILTER(
                'Calendar_Dates', 
                'Calendar_Dates'[Date] < StartDate &&
                NOT(WEEKDAY('Calendar_Dates'[Date], 2) IN {6, 7})
            ), 
            'Calendar_Dates'[Date], DESC
        )
    )
RETURN
    CALCULATE(
        [_Net],
        PreviousDates
    )

 

 

_Net = SUM('Top Customer Information'[Net])


But this measure is returning empty result.
Kindly suggest

Thanks,

3 REPLIES 3
nandic
Super User
Super User

@Anonymous could you try this calculation:

SalesPreviousPeriod =
VAR StartDate = [SelectedRangeStart]
VAR DaysCount = [NumberOfDaysSelected]
VAR PreviousDate = StartDate - DaysCount
RETURN
    CALCULATE(
        [_Net],
        DATESBETWEEN(Calendar_Dates[Date],PreviousDate,StartDate)
    )

Cheers,
Nemanja Andic
Anonymous
Not applicable

Hi @nandic, Thank you for your response.
I implemented your suggested solution but it is giving incorrect result in there are weekends before.

Rana_Rumeel_0-1716901305098.png

You can download .pbix file from Here... 

Anonymous
Not applicable

Any suggestion please...

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.