It all goes in your total calculation. Instead of, say
${subtotal1} + ${subtotal2} + ...
replace it with
if(${subtotal1}='',0,${subtotal1}) + if(${subtotal2}='',0,${subtotal2}) + ...
That way any irrelevant/unset/nil subtotals will add 0 to the total (as opposed to introducing NaN)