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
PKCRonny
New Member

Calculate Unavailability hours in a shift and exclude overlapping entries

Hi Community,

I have a calculation that works out actual hours in a shift minus any unavailabilty, but require it to exclude any that overlap and not be calculated twice. There are 2 tables; 1 to show all employees and the shift they are rostered for each day.  The 2nd table is a list of all unavailability recorded.  Each unavailabilty has been split onto individual days.

 

EmpDateShift StartShift EndShift Hours
124/06/202408:0014:006
125/06/2025--0

 

EmpDateUnavailabilty TypeUnavailabilty StartUnavailabilty End
124/06/2024Annual Leave08:0014:00
124/06/2024Sickness00:0023:59
125/06/2024Sickness00:0023:59


Added a column to the shift table to work out unavailable hours, it calculates -12 for the 24th giving an actual hours total of -6

3 REPLIES 3
Anonymous
Not applicable

Hi @PKCRonny 

 

Ensure there's a relationship between your two tables based on to accurately correlate shifts with unavailability periods.

 

 Use DAX to calculate the overlapping hours between the shift hours and the unavailability periods.

 

This involves creating a new measure that compares the start and end times of shifts and unavailability periods to determine the overlap duration.

 

Adjust your calculation for actual hours to subtract only the unique unavailability hours from the total shift hours.

 

For example:

 

Actual Hours = 
SUMX(
    ShiftsTable,
    [Shift Hours] - 
    MAXX(
        FILTER(
            UnavailabilityTable,
            UnavailabilityTable[EmpDate] = ShiftsTable[EmpDate]
        ),
        [Unavailability Duration]
    )
)

 

Regards,

Nono Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

hi, thanks for that.

Wondered if I could add another scenario, as I did not include some of the harder entries by the staff.
There is occasions where both unavailability have to be included, so would be looking to see if any of the availability overlapped.   Adding different entries to the original table for new scenario.

EmpDateUnavailabilty TypeUnavailabilty StartUnavailabilty End
124/06/2024Meeting10:0011:00
124/06/2024Private Appointment13:0015:00


for this scenario, both unavailability would be included.

PKCRonny
New Member

Unavailable Hrs (column) =
var availTimeStart = CONVERT(factAvailability[startTime],DATETIME)
var availTimeEnd = CONVERT(factAvailability[endTime],DATETIME)

var shStart1 = factAvailability[startShift1]
var shEnd1 = factAvailability[endShift1]
var shStart2 = factAvailability[startShift2]
var shEnd2 = factAvailability[endShift2]

var chkShift1 = IF(factAvailability[availabilityType]="BUSY" && availTimeStart <= shEnd1 && shStart1 <= availTimeEnd, 1, 0)
var chkShift2 = IF(factAvailability[availabilityType]="BUSY" && availTimeStart <= shEnd2 && shStart2 <= availTimeEnd, 1, 0)

var _availMins1 = IF(chkShift1=1,  DATEDIFF( IF(shStart1<=availTimeStart, availTimeStart, shStart1)
, IF(shEnd1<=availTimeEnd, IF(FORMAT(shEnd1,"HH:mm")="00:00", shEnd1+1, shEnd1), availTimeEnd), MINUTE), 0)
var _availMins2 = IF(chkShift2=1,  DATEDIFF( IF(shStart2<=availTimeStart, availTimeStart, shStart2)
, IF(shEnd2<=availTimeEnd, IF(FORMAT(shEnd2,"HH:mm")="00:00", shEnd2+1, shEnd2), availTimeEnd), MINUTE), 0)
var returnValue = (_availMins1 + _availMins2) / 60

RETURN returnValue

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.