Speeding up distinct count formula

powerpackinduo

Board Regular
Joined
Jul 28, 2005
Messages
128
I have the following formula that counts distinct alphanumeric records (Sheet2 column A) based on a store (store is column B).
The formula works correctly but because there are so many records it takes forever to process. Is there a more efficient/faster way to do this?

Code:
=SUMPRODUCT((Sheet2!$A2:$A40000<>"")/COUNTIF(Sheet2!$A2:$A40000,Sheet1!$A2:$A40000&""),--(Sheet2!$B2:$B40000=Sheet1!B2))
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
FWIW the formula you're using is presently flawed I'm afraid - should you have

Code:
X Y
X Z

and you search based on field 2 being Y your SUMPRODUCT would return 0.5 for X rather than 1 - to be accurate you would need to add a MATCH to your function, ie:

Code:
=SUMPRODUCT(--(MATCH(Sheet2!$A$2:$A$40000&":"&Sheet2!$B$2:$B40000,Sheet2!$A$2:$A$40000&":"&Sheet2!$B$2:$B$40000,0)=(ROW(Sheet2!$A$2:$A$40000)-ROW(Sheet2!$A$2)+1)),--(Sheet1!$B$2:$B$40000=Sheet1!B2))

which is obviously even more hideous and inefficient (you could also use a Frequency Array)

A quicker way to calculate this would be to use "helpers" on Sheet2, ie

Code:
Sheet2!C1: =$A2&":"&$B2
Sheet2!D1: =1-ISNUMBER(MATCH($C2,$C$1:$C1,0))
copied down

At which point your unique calculation becomes:

Code:
Sheet1!cell: 
=SUMIF(Sheet2!$B:$B,Sheet1!B2,Sheet2!$D:$D)

If the data on Sheet2 were sorted appropriately you could no doubt improve performance further (avoiding use of exact match in MATCH function for ex.)

Others may have better ideas for you.
 
Upvote 0
If a non-formula approach is OK, what about using a pivot table, or a query table, or (via VBA) a recordset?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top