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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

Top Solution Authors
Top Kudoed Authors