You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How do I count the number of rows that meet multiple conditions?

What I need is a formula to tell me how many people named "James" were born before 1970. So for example if my first table contains two columns such as this:


James 1964

Jonathan 1970

James 1969

Frank 1984

Bill 1990

James 1982


The formula needs to return the number 2 since there are two people named James in this table that were born before 1970.


The search values of "James" and "1970" in my case are values in another table, two columns wide, like this:


James 1970

Bill 1964


so in this table I want a third column to be calculated by adding up the number of rows in the first table that meet these conditions.


Can this be done? I've studied the functions available in Numbers including HLOOKUP and MATCH. I can't seem to find a way to make this work. Maybe this could be done if there was a function that could return multiple rows, but as far as I can see functions can return only single scalar values.

MacBook Air, OS X Mountain Lion (10.8.1)

Posted on Mar 18, 2014 4:57 PM

Reply
Question marked as Top-ranking reply

Posted on Mar 18, 2014 7:58 PM

MM2,


COUNTIFS will do that for you.


User uploaded file

Jerry

14 replies

Mar 19, 2014 10:42 PM in response to MiniMe2

Hi 'Me2,


You wrote:


"I had also been experimenting with the concatenation operator but was having some difficulty. I didn't see any examples in the manual with concatenation the way you illustrated above."


There's no concatenation in Ian's examples, illustrated below:

User uploaded file

Neither the concatenation operator ( & ) nor the CONCATENATE() function appears in that formula.


AND returns TRUE only if ALL of the comparisons in its list return TRUE. In Ian's formula, there are two conditions statements: The name in column A must match (exactly) the value in cell A1 of Table 1-1 (the text sting "James", AND the year number in column B must less than the value in B1 of Table 1-1. If either of these is not TRUE, then AND returns FALSE.


For the three FALSE results above, the second comparison returns FALSE in row 2, the first returns FALSE in row 5, and both return FASE in row 7.


"once I get your idea working (currently it's always producing FALSE in my more complex spreadsheet for reasons I don't understand yet)"


Try inserting a new column for each comparison included in the AND statement, then testing each one independently in its own column. Here's an example, Using the data and formula in Ian's table above.

User uploaded file

"but there is something very curious about your idea. You are passing an array to the AND function."


Not true.


I'm curious as to where you think Ian's formula is referencing an array. here's the formula from Ian's example:


=AND(A2='Table 1-1' :: A$1,B2<'Table 1-1' :: B$1)


Regards,

Barry

Mar 19, 2014 5:13 AM in response to Jerrold Green1

Before I posted my question I was getting very close to your solution. I had been trying COUNTIFS, in variations like this: "COUNTIFS(Tab2::Table 1::D,INDIRECT(K40))". I had also been experimenting with the concatenation operator but was having some difficulty. I didn't see any examples in the manual with concatenation the way you illustrated above. So thanks for solving my problem. I already have 1000 rows in my table and the solution still works, although it is a little sluggish. But speed isn't that important to me.

Mar 19, 2014 5:40 AM in response to MiniMe2

Hi MiniMe,


Jerry's solution using COUNTIFS is the way to go. My solution using AND (with another column in your first table) requires another formula in Table 2 to count all the rows in Table 1 that contain TRUE. That will require a COUNTIF to count all the TRUE values.


Doable, but the long way around.


There is more than one way to skin a cat. Some ways are quicker than others. Skin your cat with Jerry's method


Regards,

Ian.

Mar 19, 2014 5:47 AM in response to Yellowbox

I'm heading out to work now, but I still want to study your solution when I get back. My application is much more complex than my sample problem suggests, and once I get your idea working (currently it's always producing FALSE in my more complex spreadsheet for reasons I don't understand yet) I will possibly discover your idea works better for me.

Mar 19, 2014 8:28 AM in response to MiniMe2

MM2,


The COUNTIFS solution is inherently scalable. You may have as many conditions as you like for a given row, and these can be multiple tests on the same column or on different columns. For instance, you could test for column b content of greater than 1970 and less than 1975. Each requirement is another pair of Column&Condition cases.


Post your entire problem statement and we'll help you work it out.


Jerry

Mar 19, 2014 8:20 PM in response to Jerrold Green1

I want to share some additional results. I want to report that when my "table of names" reaches about 1000 entries then Numbers bogs down fairly badly. So badly, that when I type a word in a cell, like "Jonathan" I look up to see if the word has been typed and only the "J" has been typed, I watch as it slowly spells out "o..n..a..t..h..a..n". The reason for this is that my other table already contains 400 cells that have to be recalculated every time I modify a cell in my table of names. But for me that's not a big deal. I'm up to 1275 rows now, eventually I would reach about 10,000, but I have one trick up my sleeve: in my situation I am able to break down my 10,000 data items into about ten separate tables. I believe that should prevent too much bogging down. Once my table is built I won't be editing it a lot, so I'm okay with this performance issue.


This to me is what scalability is about. It's either the inherent nature of the COUNTIFS statement or perhaps an implementation defect that causes Numbers to exponentially slow down as the table grows. So I would say don't use COUNTIFS if your table goes much beyond 1000 lines, unless your table doesn't change very often, in which case the limit is higher.

Mar 19, 2014 8:45 PM in response to Yellowbox

Ian, I am not someone to give up, and so I'm still working on understanding your proposal. I do agree with you that Jerrold's solution is best, but there is something very curious about your idea. You are passing an array to the AND function. The documentation for that function makes no mention of what it means to pass it an array. It says "

if-expression: A logical expression. if-expression can contain anything as long as the expression can be evaluated as a boolean value. If the expression evaluates to a number, 0 is considered to be FALSE, and any other number is considered to be TRUE."

I am not yet sure but I think the AND function combines all the elements of the array using the AND operator. And I presume that the OR function would perform the OR function on each element. This is very curious behavior and may come in handy some day as an undcoumented feature. Thanks for the tip.

Mar 19, 2014 8:46 PM in response to MiniMe2

Hi MiniMe,


I am able to break down my 10,000 data items into about ten separate tables. I believe that should prevent too much bogging down. Once my table is built I won't be editing it a lot, so I'm okay with this performance issue.


That looks like a good idea. A separate COUNTIFS table for each Names table means the formula will have a smaller list to search.


Another way is to split a document into two or more smaller ones. If it suits your purpose, you could sort your "table of names" alphabetically by name. Then all the Aarons to Michaels could go into one document and all the Nigels to Zaccarias into another. Not as convenient, because Numbers will not link between documents.


In Jerry's example, the COUNTIFS formula is in Table 2 and allows for one name-age combination. If your Table 2 is building a list of name-age combinations, you could Copy then Paste Formula Results to get text instead of formulas. Maybe that will help with speed and document size. Will need updating when you edit the Names table.


Let us know how you go.


Regards,

Ian.

Mar 19, 2014 10:10 PM in response to MiniMe2

Hi MiniMe,


=AND(A2='Table 1-1' :: A$1,B2<'Table 1-1' :: B$1)

Not arrays, but cell references. The double colon :: comes between the table name and the cell reference.


The first test-expression:

A2='Table 1-1' :: A$1

A2 is the name in Row 2. This becomes A3... when filled down

Does this equal the contents of cell A1 in table 1-1?

The $ keep this reference fixed to Row 1 of Table 1-1 as the formula is filled down.


comma before the next test-expression:

B2<'Table 1-1' :: B$1

B2 is the year in Row 2. This becomes B3... when filled down

Is this less than the contents of cell B1 in table 1-1?

The $ keep this reference fixed to Row 1 of Table 1-1 as the formula is filled down.


My formulas are in the first table and refer to the second.

Jerry's COUNTIFS formula is in the second table and refers to the first.


The Numbers implementation of AND is strange, but the comma separator is shorter than typing: is this true AND is that true for a long list of comparisons.


Regards,

Ian.

Mar 20, 2014 7:50 AM in response to MiniMe2

MiniMe2 wrote:


I want to share some additional results. I want to report that when my "table of names" reaches about 1000 entries then Numbers bogs down fairly badly. So badly, that when I type a word in a cell, like "Jonathan" I look up to see if the word has been typed and only the "J" has been typed, I watch as it slowly spells out "o..n..a..t..h..a..n". The reason for this is that my other table already contains 400 cells that have to be recalculated every time I modify a cell in my table of names. But for me that's not a big deal. I'm up to 1275 rows now, eventually I would reach about 10,000, but I have one trick up my sleeve: in my situation I am able to break down my 10,000 data items into about ten separate tables. I believe that should prevent too much bogging down. Once my table is built I won't be editing it a lot, so I'm okay with this performance issue.


This to me is what scalability is about. It's either the inherent nature of the COUNTIFS statement or perhaps an implementation defect that causes Numbers to exponentially slow down as the table grows. So I would say don't use COUNTIFS if your table goes much beyond 1000 lines, unless your table doesn't change very often, in which case the limit is higher.

Numbers 2 is notoriously slow, and to add to the pain, it is notoriously dumb about when i recalculates.


If you keep TextEdit handy, you can create your entries there and Paste into Numbers as whole words or phrases to reduce the number of recalculations.


Jerry

How do I count the number of rows that meet multiple conditions?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.