COUNTIF for rows which contain a given value in another column

Lou picture Lou · Aug 31, 2014 · Viewed 7.2k times · Source

My table lists every character from all 5 of George R. R. Martin's currently published A Song of Ice and Fire novels. Each row contains a record indicating which book in the series the character is from (numbered 1-5) and a single letter indicating the character's gender (M/F). For example:

   A                  B     C
1  Character          Book  Gender
   ------------------------------
2  Arya Stark        -  1  -  F
3  Eddard Stark      -  1  -  M
4  Davos Seaworth    -  2  -  M
5  Lynesse Hightower -  2  -  F
6  Xaro Xhoan Daxos  -  2  -  M
7  Elinor Tyrell     -  3  -  F

I can use COUNTIF to find out that there are three females and three males in this table, but I want to know, for example, how many males there are in book 2. How could I write a formula that would make this count? Here is a pseudocode of what I'm trying to achieve:

=COUNTIF(C2:C7, Column B = '2' AND Column C = 'M')

This would output 2.

I'm aware that this task is far better suited to databases and a SELECT query, but I'd like to know how to solve this problem within the constraints of a LibreOffice Calc spreadsheet, without using a macro. Excel-based solutions are fine, so long as they also work in Calc. If there's no solution that uses COUNTIF, it doesn't matter, so long as it works.

Answer

Lou picture Lou · Aug 31, 2014

I worked it out, thanks to a prompt by assylias. The COUNTIFS formula produces the result I want by counting multiple search criteria. For example, this formula works out how many male characters are in Book 1 (A Game of Thrones).

=COUNTIFS($A$2:$A$2102, "=1", $L$2:$L$2102, "=M")