Forum Discussion

Jeff-Nipro's avatar
Jeff-Nipro
Copper Contributor
Mar 01, 2024

Counting a list with two criteria - date and value

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



  • 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's avatar
    Patrick2788
    Silver Contributor

    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))
    )

     

     

  • 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.

Resources