Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel Count Unique Values Between Dates

· 5 min read · Home Tech

In this article
  1. Formula
  2. Worked Example
  3. Using UNIQUE and FILTER (Excel 365)
  4. How to Choose the Right Method
  5. Common Problems and Mistakes
  6. Comparing with Other Methods
  7. Tips for Accurate Results
  8. Frequently Asked Questions

To count unique values in Excel between two dates, use a combination of the SUMPRODUCT, COUNTIFS, and MATCH functions, or the newer UNIQUE and FILTER functions if you have Excel 365. The exact formula depends on your data layout and Excel version, but the core approach filters rows by date range and then counts distinct entries.

in t | Start date | - | 2023-01-01 | 0
in d | End date | - | 2023-12-31 | 0
in n | Number of unique values | - | 0 | 0
out result | Unique count | - | n | 0

Formula

The general formula for counting unique values between two dates (when your data has dates in column A and the values to count in column B) is:

=SUMPRODUCT((A2:A100>=start_date)*(A2:A100<=end_date)/COUNTIFS(B2:B100,B2:B100,A2:A100,">="&start_date,A2:A100,"<="&end_date))

Where start_date and end_date are cell references or date values. This works by dividing 1 by the count of each value within the date range and summing the results, which gives the number of distinct values.

Worked Example

Suppose your data has dates in A2:A10 and values in B2:B10 as follows:

  • A2: 2023-01-05, B2: Apple
  • A3: 2023-02-10, B3: Banana
  • A4: 2023-03-15, B4: Apple
  • A5: 2023-04-20, B5: Cherry
  • A6: 2023-05-25, B6: Apple
  • A7: 2023-06-30, B7: Banana
  • A8: 2023-07-05, B8: Date
  • A9: 2023-08-10, B9: Apple
  • A10: 2023-09-15, B10: Fig

If you set start_date = 2023-01-01 and end_date = 2023-12-31, the formula evaluates as:

  1. For each row, check if the date is within range (all are).
  2. Count how many times each value appears in the range: Apple appears 4 times, Banana 2 times, Cherry 1, Date 1, Fig 1.
  3. Sum 1/4 + 1/2 + 1/4 + 1/1 + 1/4 + 1/2 + 1/1 + 1/4 + 1/1 = 5.

The result is 5 unique values: Apple, Banana, Cherry, Date, Fig.

Using UNIQUE and FILTER (Excel 365)

If you have Excel 365 or Excel 2021, you can use the dynamic array functions:

=COUNTA(UNIQUE(FILTER(B2:B100,(A2:A100>=start_date)*(A2:A100<=end_date))))

This filters column B to rows where the date is between the two dates, then UNIQUE removes duplicates, and COUNTA counts the remaining cells. If there are blank cells in the filtered result, use =ROWS(UNIQUE(FILTER(...))) instead, but be aware that ROWS counts empty cells as well, so you may need to filter out blanks: =COUNTA(UNIQUE(FILTER(B2:B100,(A2:A100>=start_date)*(A2:A100<=end_date)*(B2:B100<>"")))).

How to Choose the Right Method

Your choice depends on your Excel version and data size:

  • Excel 365 / 2021: Use UNIQUE and FILTER – it's simpler and more readable.
  • Older Excel (2019 and earlier): Use the SUMPRODUCT/COUNTIFS formula, as array formulas are less reliable.
  • Very large datasets (tens of thousands of rows): SUMPRODUCT can be slow; consider using a pivot table with a calculated field or Power Query.

For a one-off analysis, the formula approach is fine. For recurring reports, consider creating a helper column that flags rows within the date range, then use a pivot table to count distinct values.

Common Problems and Mistakes

  • Date format issues: If your dates are text, the comparison won't work. Ensure they are actual date values, not strings.
  • Including headers: Make sure your ranges start at the first data row, not the header row.
  • Blank cells in the value column: They can cause incorrect counts. Filter them out as shown above.
  • Array formula entry: In older Excel, you might need to press Ctrl+Shift+Enter instead of Enter.
  • Using entire columns (A:A): This can slow down calculations; limit the range to your actual data.

Also, note that COUNTIFS is case-insensitive, so "Apple" and "apple" are considered the same.

Comparing with Other Methods

There are several ways to count unique values between dates:

  • Pivot Table: Add a date filter and use "Distinct Count" in the Value Field Settings (available in Excel 2013+). This is intuitive but requires manual steps.
  • Power Query: Load your data, filter by date, then group by the value column with a distinct count. This is powerful for large data and repeatable.
  • VBA macro: Can automate the process but requires coding knowledge and may be overkill.

The formula method is best for quick, one-off calculations without changing your data structure.

Tips for Accurate Results

  • Always use absolute cell references for the start and end dates if you plan to copy the formula.
  • Use Excel's DATE function to avoid text-date issues: =DATE(2023,1,1) instead of typing "2023-01-01".
  • If your data has multiple columns (e.g., product, region, date), you can adapt the formula to count unique combinations by concatenating values.

For example, to count unique product-region pairs between dates, you could use =SUMPRODUCT((A2:A100>=start)*(A2:A100<=end)/COUNTIFS(B2:B100,B2:B100,C2:C100,C2:C100,A2:A100,">="&start,A2:A100,"<="&end)).

Frequently Asked Questions

Why does my formula return a #DIV/0! error?

This happens when there are rows where the date is within range but the value is blank, causing COUNTIFS to return 0. To fix it, add a condition to exclude blanks: COUNTIFS(B:B,B:B,A:A,">="&start,A:A,"<="&end) will still count blanks, so you need to add a check for non-blank values in the numerator: (B2:B100<>"").

Can I count unique values that appear only once?

Yes, you can use =SUMPRODUCT((A2:A100>=start)*(A2:A100<=end)/COUNTIFS(B2:B100,B2:B100,A2:A100,">="&start,A2:A100,"<="&end)*(COUNTIFS(B2:B100,B2:B100,A2:A100,">="&start,A2:A100,"<="&end)=1)). This sums only the rows where the count is 1, giving the number of values that appear exactly once.

How do I count unique values between dates in a pivot table?

Add your date field to the Filters area and set a custom date range. Then add the value field to the Values area, right-click, choose Value Field Settings, and select "Distinct Count" under Summarize by. This option is available in Excel 2013 and later.

What if my dates are in a different column than the values?

Simply adjust the ranges in the formula. For example, if dates are in column C and values in column D, replace A2:A100 with C2:C100 and B2:B100 with D2:D100. The logic remains the same.

Can I use wildcards in the value column?

No, wildcards like * and ? are not supported in the COUNTIFS second argument when you're using a range as the criteria. If you need to count values that start with a certain text, you'd need a helper column or a different approach.