COUNTIFS

Short answer

Prove every ledger row landed in one bucket

COUNTIFS(criteria_range1, criteria1, [criteria_range2, criteria2], ...)
Excel version
2007+
Also searched as
conditional count, count by criteria, find duplicates in excel, aging buckets count, count unmapped accounts

What it returns

COUNTIFS counts the rows where every supplied condition is true. The criteria syntax is the same as SUMIFS: a range to test, then what to test for, in pairs. Conditions join with AND.

When nothing matches, it returns 0. It never returns an error for a no-match, which makes it safe to put in a check row and compare against an expected number.

In a model the count is rarely the answer anyone reports. It is the control that proves the reported numbers are complete. A SUMIFS grid can look healthy while ledger rows fall outside it. A count of those rows cannot hide them.

Arguments that matter

ArgumentRequiredWhat it does
criteria_range1YesThe column to test. GL[Line]. Must be a range or Table column, not a calculated array.
criteria1YesWhat to test for. A cell, a literal, or an operator string such as ">30".
further pairsNoUp to 127 range and criteria pairs. Every range must be the same size.

The criteria rules match SUMIFS:

  • $B12 matches the value in B12.
  • ">="&D$10 reads a threshold from a cell. The operator is quoted, the reference is not.
  • "" counts blank cells. "<>" counts non-blank cells.
  • * and ? are wildcards. A tilde before one matches it literally.

There is no separate range to count. COUNTIFS counts rows, so it needs only the tests.

In a financial model

COUNTIFS belongs in the check row. The play on the check row lives at the top says where that row sits. Build a check row and make it loud says what goes in it: a list of tests that should each read zero, and a flag that turns red when one does not.

Five counts cover most of a ledger-driven model:

  • Completeness: the number of ledger rows that landed in the grid equals the number in the extract.
  • Unmapped codes: rows where the XLOOKUP against the mapping table returned its if_not_found flag, per one mapping table per dimension.
  • Malformed codes: a REGEXTEST column flags each row against the account format, and COUNTIFS(GL[Valid], FALSE) counts the failures.
  • Duplicates: journal lines that appear more than once on the same key.
  • Out-of-period rows: transactions dated outside the modelled months.

The same function also does reporting work. AR aging buckets, headcount by grade, and the number of open POs by approver are all COUNTIFS. The control use comes first because it is the one people skip.

Worked example

A general ledger extract on tab GL, a Table with columns Date, DocNo, LineNo, Code, Line, Amount. Line was filled by XLOOKUP with "UNMAPPED" as the fallback. The statement grid on tab IS lists line items in B12:B40 and month-end dates in D10:O10.

Unmapped rows:

=COUNTIFS(GL[Line], "UNMAPPED")

Rows dated outside the twelve modelled months:

=ROWS(GL[Date])-COUNTIFS(GL[Date], ">="&EOMONTH(IS!$D$10,-1)+1, GL[Date], "<"&IS!$O$10+1)

Rows whose line item appears in the grid. Feeding the whole list of grid lines in as the criteria returns one count per line, and SUM adds them:

=ROWS(GL[Line])-SUM(COUNTIFS(GL[Line], IS!$B$12:$B$40))

All three should read zero. The third catches a line item that exists in the ledger but was never added to the grid. For the one-item version of that question, is this line in the grid, HAS returns TRUE or FALSE with no >0 comparison. It is an Insiders preview, so keep COUNTIFS in any file someone else opens. In Excel 2019 and earlier, wrap the array version in SUMPRODUCT instead of SUM.

A duplicate flag, as a column in the GL Table:

=COUNTIFS([DocNo], [@DocNo], [LineNo], [@LineNo])>1

Then count the TRUE values in the check row. A journal loaded twice shows up here before it doubles a month.

AR aging from an open-items Table AR with a DaysPastDue column:

BucketFormulaCount
Current=COUNTIFS(AR[DaysPastDue], "<=0")412
1 to 30=COUNTIFS(AR[DaysPastDue], ">0", AR[DaysPastDue], "<=30")96
31 to 60=COUNTIFS(AR[DaysPastDue], ">30", AR[DaysPastDue], "<=60")31
Over 60=COUNTIFS(AR[DaysPastDue], ">60")12

The buckets should sum to ROWS(AR[DaysPastDue]). If they do not, a row has a blank or text value in the days column.

Old way vs new way

The old tie-out was manual: filter the ledger, read the row count off the status bar, compare it to a pivot’s count, and tick it on a printout. In formula form, the closest anyone got was a typed number:

=ROWS(GL[Line])-4318

The COUNTIFS version recomputes every time the data changes:

=ROWS(GL[Line])-SUM(COUNTIFS(GL[Line], IS!$B$12:$B$40))

The manual check was right once, on the day someone did it. The formula is right every time the workbook calculates, and it sits in the check row where a failure is visible.

Where it breaks

The duplicate check flags rows that are not duplicates. The key is a long number stored as text, such as a 16-digit invoice ID. COUNTIFS converts numeric-looking text to a number, and Excel keeps only 15 significant digits, so different IDs compare as equal. Fix: force a text comparison by appending a wildcard to the criteria, [@InvoiceID]&"*".

Codes stored as text and as numbers count together. A criteria of "4000" matches both the number 4000 and the text “4000”. That is different from XLOOKUP, which treats them as different values, so the two functions can disagree about the same column. Fix: settle the type in Power Query where the extract lands.

Excel refuses the formula. A criteria range was a calculated array, such as MONTH(GL[Date]). COUNTIFS accepts only ranges there. Fix: add a helper column to the Table, or switch to SUMPRODUCT, which takes arrays.

COUNTIFS returns #VALUE!. The criteria ranges are different sizes. Mixing a whole column with a Table column is the usual cause. Fix: use Table columns for every range.

An exclusion count returns the whole column. "<>"&$B12, meant to count rows outside one line item, meets an empty B12 and becomes "<>", which counts every non-blank row. Fix: guard the formula so an empty label returns zero, or keep blank rows out of the label list.

The aging buckets miss rows. One bucket ends at "<30" and the next starts at ">30", so day 30 belongs to neither. Fix: use <= on the upper bound and > on the lower bound of the next bucket, and tie the bucket total to the row count.

Related functions

  • SUMIFS. The same criteria, returning a total. COUNTIFS proves the rows are there, SUMIFS adds them up.
  • XLOOKUP. Fills the Line column with a visible fallback, which COUNTIFS then counts.
  • SUMPRODUCT. For counts on a computed condition, such as the month of a date, which COUNTIFS cannot accept as a range.
  • UNIQUE. Lists the distinct keys. Comparing its row count to the source’s is another duplicate check.
  • EOMONTH. Sets the lower bound of the out-of-period test, the day after the month-end before the first header.
  • FILTER. Returns the actual rows behind a nonzero check, so you can see which journals failed.
  • The play build a check row and make it loud is where every count on this page should end up.

All functions