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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Uzi2019
Super User
Super User

Dynamic Comparison of Date range

Hello Expert,
I want to create a measure that compare dynamic date range . For example:

Scenario 1: if user selects randomly 14th - 16th sept. So it should calculate data from 7th - 9th sept of last week. like this in given image.

Uzi2019_0-1676369269171.png

Scenario 2: If user select 1st - 30th sept , It should calculate previous month 1st - 31th Aug.


Please help me to find dynamic solution to this problem.

Thanks you in advance. 

Don't forget to give thumbs up and accept this as a solution if it helped you!!!
1 ACCEPTED SOLUTION

@Uzi2019 Try this DAX:
First create a measure to get the no. of days between date range:

No.ofDay = DATEDIFF(MIN(Date),MAX(Date),DAY)+1

Then create  a measure which will get the sales value of prev selected days:

Prev Days Sales = CALCULATE([Sales], DATEADD(Date[Date], - [No.ofDay], DAY)

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard

View solution in original post

4 REPLIES 4
MAwwad
Solution Sage
Solution Sage

 

For Scenario 1, you can use the following measure in DAX:

 

 
Measure = CALCULATE(SUM(Table[Value]), DATESBETWEEN(Table[Date], SELECTEDVALUE(Table1[Start Date]) - 7, SELECTEDVALUE(Table1[End Date]) - 7 ) )

This will calculate the sum of "Value" for the selected date range (14th-16th Sept) by using DATESBETWEEN function with a date range starting 7 days before the selected start date and ending 7 days before the selected end date.

For Scenario 2, you can use the following measure in DAX:

 

 
Measure = CALCULATE(SUM(Table[Value]), DATESBETWEEN(Table[Date], EOMONTH(SELECTEDVALUE(Table1[Start Date]),-1)+1, EOMONTH(SELECTEDVALUE(Table1[Start Date]),-1)+DAY(EOMONTH(SELECTEDVALUE(Table1[Start Date]),-1)) ) )
 

This will calculate the sum of "Value" for the selected date range (1st-30th Sept) by using DATESBETWEEN function with a date range starting from the first day of the previous month and ending on the last day of the previous month.

Note: In both scenarios, replace "Table" with the name of your table and "Value" with the name of your column that you want to aggregate.

This solution would not work for me.
I need one measure which dynamically calculate same previous period as per date range selected by user.
Date range can be 7 days , 3 days, 21 days or 1 month etc in same date range filter. So previous period measure should calculate as per date selection made by user.

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

@Uzi2019 Try this DAX:
First create a measure to get the no. of days between date range:

No.ofDay = DATEDIFF(MIN(Date),MAX(Date),DAY)+1

Then create  a measure which will get the sales value of prev selected days:

Prev Days Sales = CALCULATE([Sales], DATEADD(Date[Date], - [No.ofDay], DAY)

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard

Hey @Tahreem24 
Thanks alot. It actually kinda work for me.

Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.

Top Solution Authors