Not perfect because I'm out if time:
In cell E3 array-enter this formula:Array-entering means committing the formula to the sheet with Ctrl+Shift+Enter, not just Enter.=IF(ISNUMBER(A3),MAX(0,IF(MATCH("Done",A4:A$19,0)<MATCH(1,(ISNUMBER(A4:A$19)+ISBLANK(A4:A$19)),0),INDEX(B4:B$19,MATCH("Done",A4:A$19,0)),$D3)-B4),"-")
Copy down.
Notes:
If completion dates are present in both columns B and D for a given project, the column B one is used.
If no completion date is present at all for a given project 0 is displayed (not ideal, I know)
The formula depends on column A for:
1. Finding the Done row
2. Determining the extent of each project by using the presence of a number (or a blank cell) so this assumes all project numbers are just numbers. Because of this and to prevent an error for the bottom-most project duration, references to columns A and B need to include a blank cell below the table (not ideal), so in this case, although the table finishes at row 18, references in the formula include row 19. This also means that there should be no blank cells within the table in column A.
So lots of ifs and buts, however it seems to work here.