Search code examples
excelstringdistinctcontains

Count Distinct Values in Column that contain same text string


I have a column with hundreds of items for each year. The items all start with a two digit year identifier, followed by a dash. Ex: "18-" , "19-" , "20-" After the dash is a 4+ digit unique number for the line item. However, for my main table there can be multiple sub items tied to the same main item.

Ex: 18-1 could be for apples & pears. What is in column B is does not matter, just that it forces a duplicate on column A. (And I need to see these duplicates in the final table I am gathering this count for)

Would like to count the number of unique items by year. Something like "contains, Or starts in this case" "18-" , "19-" , "20-*"

Example: counting distinct with "18-*" = 3

     Column-A   Column-B
     18-1       Apple
     18-1       Pear
     18-2       widget11
     18-3       widget12
     18-3       widget13
     19-1       widget14
     19-2       widget15
     19-3       widget16
     19-4       widget17

Is it possible to counting distinct values that "start" with a specified text string from a single cell in excel?


Solution

  • Utilising your example, this array-formula calculates unique counts in the range A1:A9, based on the prefix in D1:

    =SUM(IFERROR(1/COUNTIFS($A$1:$A$9,$A$1:$A$9,$A$1:$A$9,D1&"*"),0))

    It needs to be entered into a single cell and saved pressing CTRL+SHIFT+ENTER before filling down.

    enter image description here