Something like attached?
Formula in B2: Copy down
=IF(COUNTIFS($C$2:C2,C2,$D$2:D2,D2)>1,A2-INDEX($A$2:$A$11,SMALL(IF(($C$2:$C$11=C2)*($D$2:$D$11=D2),ROW($D$2:$D$11)-1),COUNTIFS($C$2:C2,C2,$D$2:D2,D2)-1)),0)
Confirm as array (CSE).
Note that you may need additional check if data spans multiple days.
Edit: Alternate using IFERROR (Confirm as array)
=IFERROR(A2-INDEX($A$2:$A$11,SMALL(IF(($C$2:$C$11=C2)*($D$2:$D$11=D2),ROW($D$2:$D$11)-1),COUNTIFS($C$2:C2,C2,$D$2:D2,D2)-1)),0)