tableau-apilevel-of-detail

COUNTIFS where one criteria is "number must be unique" in Tableau


Is there a way to count unique numbers in a column in Tableau?

COUNT(IF [Type] = "Boxes" AND [Ordernumber] is unique THEN 1 END)

In my data, several rows have the same order number. I want all the rows where [Type] = "Boxes" AND have a unique order number. Example:

Type Ordernumber
Boxes 10000000
Boxes 10000000
Boxes 10000001
Misc 10000002
Boxes 10000003

Should be equal to 2.


Solution

  • Here’s one approach, relatively easy.

    1. Put Type on the filter shelf, select only boxes, and (important) add it to the context (right click on the field on the filter shelf to access the menu). Filters that are part of the “context” are applied early in the order of operations.
    2. Put OrderNumber on the filter shelf. On the filter dialog, go to the Condition tab and choose COUNT([OrderNumber]) = 1.
    3. Now your data is filtered as desired. The data that passes the filter will have one record for each order number that only has one record -- after you first ignore any data rows that don't reference boxes.

    Note, it is possible that you want different semantics regarding the Type="boxes" filter. Perhaps you want to first eliminate orders with multiple rows and then afters only view the records with boxes. If so, experiment with changing which filter(s) is (are) in the context to see the impact of applying the filters in different orders.