Spreadhseet pro-tip: Using countifs over multiple columns
Countifs is a extremely versatile formula that can be used in Google Sheets to generate frequency tables and proportions of complex interrelated data. This is a short introduction to the formula and the autofill properties that are required to make it work magic
Spreadsheet applications like Microsoft Excel and Google Sheets have incredible features that are not obvious to everyone. Even seasoned users sometimes get stumped when a new kind of data creates a new class of problem.
Here is one such problem that I came across yesterday when trying to create antibiograms from the data of antibiotic susceptibility from an EMR. Google Sheets was used to solve this challenge.
The tables below have been simplified for the sake of clarity.
Here is the initial table:
Antibiotic susceptibility pattern
The challenge was to go from the above to a table like this:
Resistance pattern (Numbers are in percentages)
This requires some formula magic to avoid repitition of text.
The spell is named
COUNTIFS. We should start with a table like this:
B12 then what we need to write is
=COUNTIFS(B$1:B$6, "Resistant", $A$1:$A$6, "="&$A12)/COUNTIF($A$1:$A$6, "="&$A12)*100
This can then be dragged down and then right to cells
C13 and the formula automatically updates to the correct value.
COUNTIFS is an excellent formula which does count whatever we ask it to count based on the value in other columns. Read Google’s guide on what the formula exactly does.
Autofill is magic. It automatically changes the formula when we drag (or paste) formula in a new cell based on the offset. But when we want formula to refer to specific cells without changing when autofilling, we need to use the
$ sign. For example, the
B$1:B$6 when being dragged right will become
C$1:C$6, but when it is dragged down it remains
$A$1:$A$6 will remain
$A$1:$A$6 no matter which direction it is dragged.
Another interesting trick I used is the
"="&$A12. Here the
& tells Google Sheets to use the value from the cell that is being referenced next. The
= is the criteria for
COUNTIF. So, in short it translates to