This formula uses COUNTIF with an expanding range to first check if the current row is the first occurrence of a given invoice number:
This expression only returns TRUE when this is the first occurrence of a given invoice number. If so, a SUMIF calculation is run:
Here, SUMIF is used used to generate a total sum by invoice number, using the amounts in column D.
If the count is not 1, the formula simply returns an empty string ("")