d2
=IF(C2="","",SUM(B$2:B2)-SUM(D$1:D1))
try this and copy towards down