From the sample data, I understand that for any Site which has a value of -10
somewhere in column B, then the calculation of (B / C)
needs to be ((B + 10) / C)
. (Although cell D11
doesn't follow this, I'm assuming it's just a typo :) )
The formula below would work in this case:
D2 = (SUMPRODUCT(($A$2:$A$22=$A2)*($B$2:$B$22=-10))*10 + B2) / C2
This formula works as follows:
SUMPRODUCT(($A$2:$A$22=$A2)*($B$2:$B$22=-10))
--> This will return the total number of rows which contain the value of A2
in column A and a -10
in column B :
($A$2:$A$22=$A2)
looks at all values in the cells between A2
& A22
, and finds which ones match A2
. That is, it finds which rows that have the same Site number as the row where the formula is entered
($B$2:$B$22=-10)
looks at all values in the cells between B2
& B22
, and finds which ones equal -10
. That is, it finds all rows that have a Distance value of -10
.
- The
SUMPRODUCT
finds the overlap of these. So, it counts how many rows have both the right Site number and a distance value of -10
.
This value is than multiplied by 10. If there were no -10 values paired with the site, then it is 0 * 10 = 0
. If there was one pair, it will be 1 * 10 = 10
.
That result is then added to B2
, and then divided by C2
.
Note: this formula assumes that there will only be a maximum of one -10
value for a particular site. If there is more than one, then the SUMPRODUCT
will return the total number of matches, and the calculation will be wrong (but the formula can be updated to correct this).
Also note that the formula refers to the ranges $A$2:$A$22
and $B$2:$B$22
. These ranges must match the first and last numerical entry in your data (and not include any text - this will break the SUMPRODUCT
formula). The simplest way to handle this (if you don't want to have to update the formula when adding values at the end of data set) is to set up a defined range name and refer to that in the formula.
To apply the formula, just paste it into D2
, and copy/drag the formula down into the cells below.
As a conclusion, I can recommend that you create a "dummy" / "helper" column to store the (SUMPRODUCT * 10) + B2
results, and use this as the input for the division calculation. This will help to visualize and check the data that is being used for the calculation. It can be hidden until you want to check the values or change the formula in case the requirements change in the future.