MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Jan 7th, 2004, 12:06 PM   #1
lenny3k
 
Join Date: Jan 2003
Posts: 18
Default Count distinct function?...

Hi there.

Is there a way to count distinct values in a range of cells in a worksheet?

i'm looking for something like a COUNT DISTINCT function that would count all distinct values in an entire column. any ideas?

any help would be appreciated!

Len
lenny3k is offline   Reply With Quote
Old Jan 7th, 2004, 01:00 PM   #2
fairwinds
MrExcel MVP
 
fairwinds's Avatar
 
Join Date: May 2003
Posts: 8,462
Default Re: Count distinct function?...

Du you mean count unique?
(English is not my native tongue)


Then try:

=SUMPRODUCT((A1:A10<>"")/COUNTIF(A1:A10,A1:A10&""))
__________________
"Fair Winds and Following Seas"
fairwinds is offline   Reply With Quote
Old Jan 7th, 2004, 03:58 PM   #3
luckycharm
 
Join Date: Sep 2003
Posts: 88
Default Re: Count distinct function?...

Fairwinds,
Care to elaborate on the qriteria used in the countif arguments?
It does work, but not sure about the intuition.
Cheers.
luckycharm is offline   Reply With Quote
Old Jan 7th, 2004, 04:17 PM   #4
fairwinds
MrExcel MVP
 
fairwinds's Avatar
 
Join Date: May 2003
Posts: 8,462
Default Re: Count distinct function?...

Hi,
I learned this formula from Aladin but I cannot find a post to refer to where he explains it.

Anyway, The first part gives 1 for each cell that is not empty. Then divides it with how many instances there is of this value.
Summing that gives you the "unique count".

The &"" is to avoid 0 and #DIV/0 when a cell is empty.
__________________
"Fair Winds and Following Seas"
fairwinds is offline   Reply With Quote
Old Jan 7th, 2004, 04:50 PM   #5
luckycharm
 
Join Date: Sep 2003
Posts: 88
Default Re: Count distinct function?...

So (as usual) everything begins with Aladin.
Sould've guessed.

Hopefully he reads in on this one and elaborates, since I
cannot say I follow/agree with your explanation.
Anyhow, to me it looks as though the second part (countif)
returns number of occurences for the "non distinct".

Waiting on Aladin.
luckycharm is offline   Reply With Quote
Old Jan 12th, 2004, 02:12 AM   #6
Aladin Akyurek
MrExcel MVP
 
Aladin Akyurek's Avatar
 
Join Date: Feb 2002
Location: The Hague
Posts: 40,228
Default Re: Count distinct function?...

Quote:
Originally Posted by luckycharm
So (as usual) everything begins with Aladin.
Sould've guessed.

Hopefully he reads in on this one and elaborates, since I
cannot say I follow/agree with your explanation.
Anyhow, to me it looks as though the second part (countif)
returns number of occurences for the "non distinct".

Waiting on Aladin.
I did have two Invalid_Session in trying to respond to this one.

Well, the formula Fairwinds posted is due to Harlan Grove, although I did play the role of an evaluator regarding the earlier public versions Harlan posted at the worksheet.functions.

Harlan eventually succeeded to create a SumProduct version of David Hager's formula, which is:

{=SUM(1/COUNTIF(Range,Range))}

The kernel idea of this formula is:

1/Tokens(Type)

Tha is, if Bob is a distinct name (a type) and Bob occurs say 3 times, we can assign a weight to each of its occurrences (tokens), thus:

1/3, 1/3, 1/3

Summing the individual weights we get 1 back. That is: dividing 1 by the tokens, we get a distinct/unique/type count.

Two things to note about this formula:

If Range has any empty cell, we get #DIV/0! for COUNTIF(EmptyCell,EmptyCell) == 0.

If Range has any cell housing a formula (like =IF(X1,1,"") that returns a blank (sometimes referred to as null string), COUNTIF(CellWithFormulaBlank,CellWithFormulaBlank), we get a count of 1 for such a cell.

It's somewhat theoretical whether an empty cell should be considered a distinct type. The same holds for a formula-blank. Supposing that they are not distinct types, Hager's formula needs some modification before it can be applied to a Range housing empty cells or formula blanks:

{=SUM(IF(LEN(Range),1/COUNTIF(Range,Range))}

or

{=SUM(IF(Range<>"",1/COUNTIF(Range,Range))}

It's obvious that using the latter (with lesser function call) is better (for robustness).

My contribution (along with Juan) consists of just this modification.

How does this formula works using an example?

Let A2:A8 house the following sample:

{"Bob";"Bob";"Bob";"Jane";"Jane";EmptyCell;"Thomas"}

where EmptyCell stands for an empty cell (not for a distinct type).

The COUNTIF(A2:A8,A2:A8) bit gives:

{3;3;3;2;2;0;1}

The 1/COUNTIF(A2:A8,A2:A8) gives:

{0.333333333333333;0.333333333333333;0.333333333333333;0.5;0.5;#DIV/0!;1}

Note #DIV/0!. Clearly, there are 3 types/distinct/unique items if SUM could ignore the error values.

With

{=SUM(IF(A2:A8<>"",1/COUNTIF(A2:A8,A2:A8)))}

we have successively:

=SUM(IF({TRUE;TRUE;TRUE;TRUE;TRUE;FALSE;TRUE},{0.333333333333333;0.333333333333333;0.333333333333333;0.5;0.5;#DIV/0!;1}))

When IF does effect the filtering:

=SUM({0.333333333333333;0.333333333333333;0.333333333333333;0.5;0.5;FALSE;1})

Since SUM ignores logical values, we get: 3.

It is quite easy to turn Hager's original formula

{=SUM(1/COUNTIF(Range,Range))}

into a SumProduct formula:

=SUMPRODUCT(1/COUNTIF(Range,Range))

but it isn't regarding:

{=SUM(IF(Range<>"",1/COUNTIF(Range,Range)))}

Whenever IF is needed when computing with array objects, a control+shift+entered formula is almost always a necessity.

Harlan eventually arrived at:

=SUMPRODUCT((A2:A8<>"")/COUNTIF(A2:A8,A2:A8&""))

which is harder to understand, but becomes intelligible if one knows the following about CountIf (as touched upon at the beginning of this post)...

******** ******************** ************************************************************************>
Microsoft Excel - UitlegHagerGrove.xls___Running: xl2000 : OS = Windows Windows 2000
(F)ile (E)dit (V)iew (I)nsert (O)ptions (T)ools (D)ata (W)indow (H)elp (A)bout
=

A
B
C
D
1
****
2
*0**
3
*
1**
4
*0**
5
*1**
6
****
Sheet3*

[HtmlMaker 2.32] To see the formula in the cells just click on the cells hyperlink or click the Name box
PLEASE DO NOT QUOTE THIS TABLE IMAGE ON SAME PAGE! OTHEWISE, ERROR OF JavaScript OCCUR.


C2:

=COUNTIF(A2,A2)

C5:

=COUNTIF(A5,A5&"")

Using our original example...

The (A2/A8<>"") bit gives:

{TRUE;TRUE;TRUE;TRUE;TRUE;FALSE;TRUE}

The COUNTIF(A2:A8,A2:A8&"") bit gives [ see the foregoing about the behavior of CountIf regarding "" ]...

{3;3;3;2;2;1;1}

The (A2:A8<>"")/COUNTIF(A2:A8,A2:A8&"") bit gives:

{TRUE;TRUE;TRUE;TRUE;TRUE;FALSE;TRUE}/{3;3;3;2;2;1;1}

The pairwise division (with coercion of logical values into numbers) gives:

{0.333333333333333;0.333333333333333;0.333333333333333;0.5;0.5;0;1}

which gets summed, producing as result: 3.

Concatenating Range with "" recurs also in other forms in other formulas where filtering If can be circumvented like in:

http://www.mrexcel.com/board2/viewto...highlight=left
Aladin Akyurek is offline   Reply With Quote
Old Jan 12th, 2004, 12:08 PM   #7
luckycharm
 
Join Date: Sep 2003
Posts: 88
Default Re: Count distinct function?...

So this is what a concise dictionary looks like.
Reading is weeping
Sorry for your missed sessions.
Took me 3 to grasp

Cheers.
luckycharm is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 05:02 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes