1

This is an extension of this question, which was answered by XOR LX.

In the original question, I wanted to count the number of rows in a matrix that included values that fulfilled at least one column-specific criteria. The columns referenced were sequential (e.g., A1:D4), but the situation requires indexing columns that are not sequential.

Take the data 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     4 5 1
1 2 3 5 3 2 2 2
2 2 2 2 2 2 2 2

With the column-specific criteria:

  • Column A criteria: >2
  • Column B criteria: >2
  • Column C criteria: <2
  • Column D criteria: >4
  • Column E criteria: >2
  • Column F criteria: >3
  • Column G criteria: >2
  • Column H criteria: >3

In order to count the number of rows that have values for all columns, as well as meet at least column-specific criterion, we could use XOR LX's code:

=SUM(N(MMULT(IF(MMULT(N(A1:H5=""),TRANSPOSE(COLUMN(A1:H5)^0))=0,COUNTIF(OFFSET(A1,ROW(A1:H5)-MIN(ROW(A1:H5)),COLUMN(A1:H5)-MIN(COLUMN(A1:H5))),{">2",">2","<2",">4",">2",">3",">2",">3"}),0),TRANSPOSE(COLUMN(A1:H5)^0))>0))

...which might have some problems with handling blanks, so the alternative:

=ROWS(A1:D4)-COUNTIFS(A1:A5,"<=2",B1:B5,"<=2",C1:C5,">=2",D1:D5,"<=4",E1:E5,">=2",F1:F5,">=3",G1:G5,">=2",H1:H5,">=3")-COUNT(1/N(MMULT(N(A1:H5=""),TRANSPOSE(COLUMN(A1:H5)^0))>0))

...which as far as I have tested seems to handle blanks regardless of their frequency or location.

But what if only rows with data for certain should be counted, ignoring the other columns. Furthermore, what if only certain columns should be evaluated for their criteria?

Let's take the example above. Our goal is to count the number of rows with i) full data in certain columns and ii) entries in certain columns that meet at least one column-specific criterion.

The relevant columns that should have full data are A:C and F:H. The columns that should be evaluated for their criteria are A:C only. The solution is 2 since:

  • There are 5 rows in total, but only rows 1, 3, 4, and 5 qualify as they have full data in columns A:C and F:H. Row 2 has missing data in column A and also G, either of which should disqualify its inclusion. Notice that even though row 3 has missing data in columns D and E, it is still included because these columns are not important.
  • Rows 1 and 3 meet the specified criteria, since i) they have full data in columns A:C and F:H, and ii) at least one entry meets one of the criteria for columns A, B, and C; specifically, the value in row 1, column A (4 > 2), and row 3, column A (3 > 2) and column C (1 < 2). Notice that this is still only counted as one row meeting the criteria, even though the criteria for 2 columns is met.

Your thoughts would be most welcome. Please also show some love to XOR LX in the original post - I am sure you will find his solution helpful.

1 Answer 1

2

@PyjamaNinja has encouraged me to try and develop my 'lemma' to their earlier question.

The method is:

A Count rows with entries in all specified columns (A-C and F-H)

B Count rows with entries in all specified columns that do _not_ meet any of the criteria in the columns being evaluated (A-C).

Subtract B from A.

=COUNTIFS(A1:A5,"<>",B1:B5,"<>",C1:C5,"<>",F1:F5,"<>",G1:G5,"<>",H1:H5,"<>")-COUNTIFS(A1:A5,"<=2",B1:B5,"<=2",C1:C5,">=2",F1:F5,"<>",G1:G5,"<>",H1:H5,"<>")

With many thanks to @XOR LX.

enter image description here

4
  • 1
    I would say this is a prime example of thinking outside of the box! Nov 27, 2018 at 12:22
  • 1
    And just for fun, how would you adapt the code to count instances in rows with full data that do not fulfil at least one column-specific criterion. If I'm not mistaken, reversing what you did, e.g., "=COUNTIFS(A1:A5,"<>",B1:B5,"<>",C1:C5,"<>",F1:F5,"<>",G1:G5,"<>",H1:H5,"<>")-COUNTIFS(A1:A5,">2",B1:B5,">2",C1:C5,"<2",F1:F5,"<>",G1:G5,"<>",H1:H5,"<>")" would not work because entries would have to fulfil all column-specific criteria, rather than at least one. Nov 27, 2018 at 15:06
  • Taking your logic of subtraction, here's my rather verbose solution (but one which avoids SUMPRODUCT so it may be computationally more efficient): =COUNTIFS(A1:A5,"<>",B1:B5,"<>",C1:C5,"<>",F1:F5,"<>",G1:G5,"<>",H1:H5,"<>")-(COUNTIFS(A1:A5,"<>",B1:B5,"<>",C1:C5,"<>",F1:F5,"<>",G1:G5,"<>",H1:H5,"<>")-COUNTIFS(A1:A5,"<=2",B1:B5,"<=2",C1:C5,">=2",F1:F5,"<>",G1:G5,"<>",H1:H5,"<>")) Nov 27, 2018 at 16:22
  • Clap, clap, clap. Impressive. Very creative solution. I don't like using SUMIFs and COUNTIFs but I have to admit that my solution was about twice and long and pretty much impossible to read. Would upvote it twice if I could. Nov 27, 2018 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.