Selecting a row in excel based on specific values

2020-02-15 06:14发布

问题:

Data roughly in the format

A    B    C
ID1  ID2  0.5
ID1  ID3  0.7
ID2  ID3  0.9

I want to create a correlation matrix (column C being the correlation between the IDs in A and B). It can definitely be done with a pivot table, though I have to use sum which could be risky if a duplicate existed since an error might not be apparent. Output format would be:

    ID1 ID2 ID3
ID1  1  .5  .7
ID2 .5   1  .9
ID3 .7  .9   1

(the '1' is easily done with an =IF(B$2=$A3,1,0) and replacing 0 with the formula to find the correlation)

I basically want a match (col a= ID1 && col b = ID2). I suspect it could be done by concatenation, but I am not sure that is a great solution? Match/Vlookup etc only return the first match [in that column], which is no good to me. Ode to a 'where' clause I guess?

My searches did not reveal any usuable help, I have already calculated the correlation and am putting it into excel from SQL. So yeah, any ideas would be super, a pivot table being a last resort.

Thanks.

回答1:

Assuming your source data range is on Sheet1, from A1 to C3 and your results range is on Sheet2, from A1 to D4.

You can put this formula on B2:

=SUMPRODUCT((Sheet1!$A$1:$A$3=Sheet2!B$1)*(Sheet1!$B$1:$B$3=Sheet2!$A2)*Sheet1!$C$1:$C$3)

and then, drag and drop this formula on the whole range.



回答2:

Why don't you create a third column that combines the values from columns A and B using =A1&B1 and then do vlookup on that value:

A    B    C       D
ID1  ID2  ID1ID2  0.5
ID1  ID3  ID1ID3  0.7
ID2  ID3  ID2ID3  0.9