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 August 31st. Request your voucher.

Reply
mhortman
Regular Visitor

Calculate days from filter, overlapping date ranges

I have a table that has a task start date and and task end date, and the number of hours of work spent during that time.

I am trying to allow the user to select a Beginning date and end date, and have Power BI calculate the hours spent during the selected date range. (assuming linear work) grouping by some other column on the tasks.   I am having trouble figuring out the proper approach to take towards this.   I found an algorithm to calculate if the task range is within the selected date range, but cannot figure out how to get the value from the slicer into a column to calculate the number of days in the task that are within the range.   

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @mhortman,

 

According to your description, you want to get the datediff in the selected data range, right?

If this is a case, you can refer to below formulas to achieve your requirement.

 

Logic :
1. If selected start date larger than current start date, use selected start date to calculate.
2. If selected end date less than current end date, use selected end date to calculate.
3. If selected start date large then current end date or selected end date less than current start date, remove.

 

1. Use start date and end date to build a calendar table.

CALENDAR = CALENDAR(FIRSTDATE('Table'[StartDate]),LASTDATE('Table'[EndDate])) 

 

 

2. Write a measure to filter range and calculate the dynamic date.

Diff = 
var start_Date=FIRSTDATE(ALLSELECTED('CALENDAR'[Date]))
var end_Date=LASTDATE(ALLSELECTED('CALENDAR'[Date]))
var current_Start=MAX('Table'[StartDate])
var current_end=MAX('Table'[EndDate])
return
IF(current_end>start_Date&&current_Start<end_Date,DATEDIFF(MAX(start_Date,current_Start),MIN(end_Date,current_end),DAY),BLANK())

 

 

3. Add measure to display the calculated date range.

calculate_start = 
var start_Date=FIRSTDATE(ALLSELECTED('CALENDAR'[Date]))
var current_Start=MAX('Table'[StartDate])
return
IF([Diff]<>BLANK(),MAX(current_Start,start_Date))

calculate_end = 
var end_Date=LASTDATE(ALLSELECTED('CALENDAR'[Date]))
var current_end=MAX('Table'[EndDate])
return
IF([Diff]<>BLANK(),MIN(current_end,end_Date))

 

3.PNG4.PNG

 

If above not help, please share some detail contents.

 

Regards,

Xiaoxin Sheng

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @mhortman,

 

According to your description, you want to get the datediff in the selected data range, right?

If this is a case, you can refer to below formulas to achieve your requirement.

 

Logic :
1. If selected start date larger than current start date, use selected start date to calculate.
2. If selected end date less than current end date, use selected end date to calculate.
3. If selected start date large then current end date or selected end date less than current start date, remove.

 

1. Use start date and end date to build a calendar table.

CALENDAR = CALENDAR(FIRSTDATE('Table'[StartDate]),LASTDATE('Table'[EndDate])) 

 

 

2. Write a measure to filter range and calculate the dynamic date.

Diff = 
var start_Date=FIRSTDATE(ALLSELECTED('CALENDAR'[Date]))
var end_Date=LASTDATE(ALLSELECTED('CALENDAR'[Date]))
var current_Start=MAX('Table'[StartDate])
var current_end=MAX('Table'[EndDate])
return
IF(current_end>start_Date&&current_Start<end_Date,DATEDIFF(MAX(start_Date,current_Start),MIN(end_Date,current_end),DAY),BLANK())

 

 

3. Add measure to display the calculated date range.

calculate_start = 
var start_Date=FIRSTDATE(ALLSELECTED('CALENDAR'[Date]))
var current_Start=MAX('Table'[StartDate])
return
IF([Diff]<>BLANK(),MAX(current_Start,start_Date))

calculate_end = 
var end_Date=LASTDATE(ALLSELECTED('CALENDAR'[Date]))
var current_end=MAX('Table'[EndDate])
return
IF([Diff]<>BLANK(),MIN(current_end,end_Date))

 

3.PNG4.PNG

 

If above not help, please share some detail contents.

 

Regards,

Xiaoxin Sheng

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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