Home » excel » excel – Forgotten how to achieve this: distinct counts from data based on a lookup value

# excel – Forgotten how to achieve this: distinct counts from data based on a lookup value

Questions:

I remember solving this with an array formula a while back, but I haven’t got the original workbook any more and the answer is completely escaping me.

Here’s sample data:

``````Salesman    Product
Bill        Apple
Bob         Orange
Bill        Banana
Bob         Apple
Bill        Apple
Bob         Orange
Bill        Apple
``````

What I’m wanting to do is make another table with the number of DISTINCT items Bob and Bill have sold. i.e:

``````Salesman Distinct Items
Bill     2
Bob      2
``````

I remember using the array formula trick with `IF` to build an array based on a lookup value, but for the life of me I can’t figure out how I then applied a distinct count to that subsequent array. It was something to do with `FREQUENCY`, but that only works with numbers and I haven’t even been able to replicate that when I’ve tried just numbers. Here I’ve played around with both `SUM(1/COUNTIF(RANGE,RANGE))` and `SUMPRODUCT(1/COUNTIF(RANGE,RANGE))`.

Specifically, with Bill in `D2` and Bob in `D3` I’ve arrived at this array formula (placed in `E2` and `E3`):

`{=SUM(IF(\$A\$2:\$A8=D2,1/COUNTIF(\$B\$2:\$B8,\$B\$2:\$B8)))}`

(D3 is obviously substituted for D2 in cell `E3`.)

It’s still way off, though. I’m using Evaluate Formula to trace it and I can see that the `COUNTIF` counts all the occurrences of each item, not just the ones belonging to the lookup.

Any pointers back in the right direction? It’s really frustrating given that I cracked it once in the past.

(I realise with Excel 2013 I can do it with `Distinct Count` in a PivotTable, but I’m needing to do it outside of one here for a couple of reasons.)

EDIT – FOUND IT – SEE ANSWER BELOW

Answering my own question so that it will show up as having been resolved.

I went searching and came across this:

http://www.mrexcel.com/forum/excel-questions/706444-function-formula-excel-count-unique-values-matching-criteria.html

Here is the formula:

`=SUM(IF(FREQUENCY(IF(\$A\$2:\$A\$8=D2,MATCH(\$B\$2:\$B\$8,\$B\$2:\$B\$8,0)),IF(\$A\$2:\$A\$8=D2,MATCH(\$B\$2:\$B\$8,\$B\$2:\$B\$8,0)))>0,1))`

I don’t know how I did it before, but I’ll take whatever I can get after facepalming all day on this.

``````=SUM(IF(ROW(\$C\$2:\$C\$6)-ROW(\$C\$2)+1=IFERROR(MATCH(\$C\$2:\$C\$6;IF(\$B\$2:\$B\$6=A2;\$C\$2:\$C\$6);0);0);1;0))