This is the third and final part of a series of questions on how to count entries in a matrix based on multiple conditions, only some of which needed to be true to qualify (Part 1), and when dealing with non-sequential columns (Part 2).
There have been some creative solutions that you can apply beyond the current context, and some fantastic camaraderie along the process.
The final step is this. Take the following matrix:
A B C D E F G H 4 2 2 2 1 4 2 4 5 2 1 3 4 1 3 2 1 1 1 3 1 2 3 5 3 2 2 2 3 3 1 2 2 2 2 2
The goal is to count the number of rows that meet the following criteria:
- They contain entries for columns A:C and F:H (so row 2 should be ignored. Note row 3 has missing entries in columns D & E but this is irrelevant).
- They contain entries in columns A:C that meet at least one category-specific criteria (e.g., either column A = >2, column B criteria = >2, or column C = <2), AND
- They contain entries in columns F:H that do not meet any of the following criteria: column F criteria = >2, column G = >2, and column H = <2. NB. While the criterion values are the same between conditions 2 and 3, they could differ, so one’s code should be flexible enough to cope with this).
The answer for the current matrix is 2 because row 3 and row 5 both have full data for the relevant columns (A:C and F:G), and they meet at least one of the criteria for columns A:C (e.g., A3 (3) and C3 (1) in row 3, and A5 (3), B5 (3), and C5 (1) in row 5) and do not meet any criteria for columns F:G.
I strongly recommend checking out XOR LX’s creative approach to the question (only one set of criteria and sequential columns), as well as Tom Sharpe’s excellent progression (for non-sequential columns but still one set of criteria), not least because you are guaranteed to learn something from their ingenuity.
I will update the question title names to reflect the best solution in each case.
Many thanks and let the games begin!
This time you have complex conditions that can’t be factored out into countifs as before, and have to be evaluated on a row-by-row basis. I suggest that common sense must eventually prevail and rather than a grandiose array formula I would propose three helper columns:
(1) Check that first three columns are non-blank (in col I):
(2) Check whether first three columns satisfy all the inverse criteria (in col J):
(3) Check whether last three columns also satisfy all the inverse criteria (in col K):
The results we want are the rows where (1) is true, (2) is false and (3) is true (in col M):
Here is the conventional array-formula approach for comparison:
(here we have to test that every column is non-blank because blanks are treated as zeroes and will incorrectly satisfy some of the criteria. The exception is column H because we are already checking that it is >= 2 so this could be omitted).
There is probably some advantage in using Countifs where possible because you can exclude blanks at the same time as testing the criteria. This is true at least for the last three columns, but not for the first three columns where a result of zero could either mean that not all of the inverse criteria are satisfied (therefore at least one of the original ones), or that there are blanks in those columns.