SOLVED

Counting a list with two criteria - date and value

Copper Contributor

I have a table with a list of dates in column A and values in column B.

I want to count the number of occurrences when column A is a specific value (such the month) and column B is greater than zero.

 

Getting the sums for either column is easy, but when I try to use both criteria it has failed. I've tried various COUNTA, COUNTIFS, SUM, DCOUNTA configurations, including with and without AND, but cannot get this quite right. 

 

Example:

In the table below, there are 8 dates in January and 8 dates in February.
January has 6 days with values greater than zero and February has 4 days with values greater than zero.

When the user selects a month, I need the working cell to count the number of column B cells in the data table that correspond to that month that are greater than zero. January should come back with a 6, while February should come back with a 4.

This should be simple, but I'm stuck.

2024-01-015
2024-01-026
2024-01-030
2024-01-109
2024-01-117
2024-01-128
2024-01-130
2024-01-144
2024-02-0211
2024-02-030
2024-02-040
2024-02-056
2024-02-203
2024-02-210
2024-02-220
2024-02-2315



6 Replies
best response confirmed by Hans Vogelaar (MVP)
Solution

@Jeff-Nipro 

You need an array friendly function for this because the month name must be pulled from the dates. This rules out functions like COUNTIF/COUNTIFS because those functions want ranges.  Nesting TEXT around the date column to pull the month name will break the formula.

 

Try this one:

=LET(
    months, TEXT(dates, "mmmm"),
    SUMPRODUCT((months = D2) * (Val <> 0))
)

 

Patrick2788_0-1709336651417.png

 

With a little tweaking, this worked great. Thank you.
You're welcome!

@Jeff-Nipro 

One more solution. It uses filter function.

Also it will expose you the importance of using formula in a dynamic way.

 

rprsridhar_0-1709388318084.png

 

@Patrick2788 

GROUPBY (not yet widely available) can also make this task simple.   The function's last argument essentially uses FILTER for its signature (Calculation speed tests with the two functions showed similar calc times).

 

This formula would return the counts for all months:

=LET(
    months, TEXT(Demo[Date], "mmmm"),
    NoZero, Demo[Val] <> 0,
    GROUPBY(months, Demo[Val], COUNT, , , , NoZero)
)

 

@Jeff-Nipro 

It is still possible to use COUNTIFS, but separate checks are needed for the month start and end.

= COUNTIFS(
    dates,  ">=" & startDate#, 
    dates,  "<=" & endDate#, 
    values, ">0"
  )

Another formula that I believe will be increasingly important in future is that offered by @Patrick2788 

= GROUPBY(
    TEXT(dates,"mmm"), 
    IF(values, values), 
    COUNT,,
  0)

Whereas the Pivot Table tends to be part of the final presentation layer, GROUPBY and PIVOTBY fit easily into ongoing calculations.

1 best response

Accepted Solutions
best response confirmed by Hans Vogelaar (MVP)
Solution

@Jeff-Nipro 

You need an array friendly function for this because the month name must be pulled from the dates. This rules out functions like COUNTIF/COUNTIFS because those functions want ranges.  Nesting TEXT around the date column to pull the month name will break the formula.

 

Try this one:

=LET(
    months, TEXT(dates, "mmmm"),
    SUMPRODUCT((months = D2) * (Val <> 0))
)

 

Patrick2788_0-1709336651417.png

 

View solution in original post