+ Reply to Thread
Results 1 to 11 of 11

How to Count for each category

Hybrid View

afriedman How to Count for each category 06-21-2011, 12:43 PM
Colin Legg Re: How to Count for each... 06-21-2011, 12:48 PM
afriedman Re: How to Count for each... 06-21-2011, 12:52 PM
Colin Legg Re: How to Count for each... 06-21-2011, 12:56 PM
afriedman Re: How to Count for each... 06-21-2011, 01:04 PM
Colin Legg Re: How to Count for each... 06-21-2011, 01:20 PM
afriedman Re: How to Count for each... 06-21-2011, 02:02 PM
afriedman Re: How to Count for each... 06-21-2011, 03:37 PM
Colin Legg Re: How to Count for each... 06-21-2011, 03:47 PM
afriedman Re: How to Count for each... 06-21-2011, 05:29 PM
Colin Legg Re: How to Count for each... 06-21-2011, 05:33 PM
  1. #1
    Registered User
    Join Date
    12-09-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    68

    How to Count for each category

    This should be relatively simple, but I think I am over thinking it.

    In the attached spreadsheet, I have words broken out into 3 categories (person, place, thing).

    Next to each word, there is also a number. What I am looking to do is:

    IF the number is between 1-5 and IF its within the Category People, THEN count how many there are
    also
    IF the number is between 6-10 and IF its within the Category People, THEN count how many there are...
    Etc, for each category.

    Like I said, this should be simple, but I am having a brain fart.

    Thanks for the help!
    Aaron
    Attached Files Attached Files

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: How to Count for each category

    Please see attached. I did it using a pivot table and by grouping the numbers in the column fields.
    Attached Files Attached Files
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Registered User
    Join Date
    12-09-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: How to Count for each category

    Colin,
    Yes that certainly would do it as well, but I was hoping to use a formula rather than a pivot table to accomplish this as the formulas will "automate" more than a pivot table would. Even that one step of refreshing it is too much bother for me

    Any other suggestions?
    Thanks!
    Aaron

  4. #4
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: How to Count for each category

    Hi Aaron,

    The formula solution is second best to a pivot table, but sure - you can do this in XL 2003 (you attached an XLS file) using SUMPRODUCT.

    For ease, I put the lower/upper limit for each category in a spare cell.
    =SUMPRODUCT(--($B$5:$B$12=$F5),--($C$5:$C$12>=G$1),--($C$5:$C$12<=G$2))

    If you can use solely an XLSX file then use SUMIFS instead of SUMPRODUCT - it calculates more efficiently.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    12-09-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: How to Count for each category

    Perfect! Thanks. This works like a charm.

    A few question on this though:
    1. what is the "--" for. I have seen that but never completely understood its purpose.

    2. Can you think of a way to incorporate those numbers at the top into the actual formula

    3. Why do you think the formula is second best to pivot tables? They honestly seem to work more effectively. Do formulas take up more space than pivot tables? If that is true then I would have to agree, but this way makes it WAAY more simple.

  6. #6
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: How to Count for each category

    (1)

    The - is a unary minus operator. SUMPRODUCT processes numbers well, but it can't handle logical types such as TRUE or FALSE.

    A test such as ($B$5:$B$12=$F5) returns an array of logical values, such as {TRUE;TRUE;TRUE;FALSE;FALSE;FALSE;FALSE;TRUE}. SUMPRODUCT can't compute this array so we coerce the logical types into number types by performing an arithmetic operation on it.

    For each element in the array:
    -TRUE becomes -1, so --TRUE becomes 1
    -FALSE becomes 0, so --FALSE becomes 0

    So for the whole array:

    If
    ($B$5:$B$12=$F5) gives {TRUE;TRUE;TRUE;FALSE;FALSE;FALSE;FALSE;TRUE}

    then
    --($B$5:$B$12=$F5) gives
    {1;1;1;0;0;0;0;1}


    (2)
    Yes, of course, but you lose flexibility. The formula would be like:
    =SUMPRODUCT(--($B$5:$B$12=$F5),--($C$5:$C$12>=1),--($C$5:$C$12<=5))
    You'll need to adjust accordingly for each number grouping.

    (3)
    SUMPRODUCT is an expensive formula when it is used like this. Too many formulas like this and your workbook calculation will become slow.

    Additionally, a pivot table will automatically generate a distinct list of categories for you: with the formula solution, if a new category appears then you'll have to manually add it to your summary table, whereas a pivot table will do it automatically. Note that the pivot table can be set up to reference a dynamic named range/table so it will automatically account for changes in the source data size. The same sort of thing is true for the number groupings - if a new number grouping (say 15-20 or whatever) is necessary then you'll have to recognise that and manually add in new formulas; the pivot table would do that automatically.

    Finally, people will generally understand the pivot table more easily than the SUMPRODUCT formula. The SUMPRODUCT formula is complicated!
    Last edited by Colin Legg; 06-21-2011 at 01:26 PM.

  7. #7
    Registered User
    Join Date
    12-09-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: How to Count for each category

    SO let me throw one more layer of complexity at you.

    Say I have 3 columns with numbers. And I want to get that same Total between 1-5, 6-10 etc but agragated for categories Number's 1, Number's 2 Number's 3.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    12-09-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: How to Count for each category

    Because when I put it into a Pivot with the 3 columns, it doesn't work properly, and the formula wont read the 3 arrays....

  9. #9
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: How to Count for each category

    Hi Aaron,

    If I understand correctly, the formula you want in I4 would be:
    =SUMPRODUCT(($B$4:$B$11=$H4)*($C$4:$E$11>=1)*($C$4:$E$11<=5))

  10. #10
    Registered User
    Join Date
    12-09-2009
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    68

    Re: How to Count for each category

    Yes, perfect! but I noticed you removed the (--)?

  11. #11
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: How to Count for each category

    Hi Aaron,
    but I noticed you removed the (--)?
    That's right. In this case the arrays have different dimensions so we have to multiply them together (a requirement of SUMPRODUCT). Multiplication itself coerces the logicals to numbers, so there's no need to coerce them to numbers with -- too.

    Bob Phillips has a good SUMPRODUCT page which you might find interesting:
    http://www.xldynamic.com/source/xld.SUMPRODUCT.html

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1