Start with a simpler version of the formula, enter this into E2 of your sample file, then fill down.You will see that this gives the difference of each row in the same way as your pivot table.
To condense all of this to a single formula, the criteria B2 needs to be expanded to the full range, using B:B is not a good idea, it will be slow to calculate and empty rows can mess things up, which is why I set the formula to the rows holding the data.
When SUMIFS (or any similar function) has more than one item in a single criteria (an array) then the array results need SUMPRODUCT (or SUM, the correct one to use depends on the criteria) to bind them together. This would give the overall total from the positive and negative results.
As you only want the total of the negative results, TEXT is used to apply a custom number format which removes positive values from the array before they are totalled by sumproduct. The string at the end, "\0;-0;0" defines the format. Google can explain excel custom number formats better than I can
Bookmarks