KNOWLEDGE BASE

## Count the Number of Customer Who Purchased A and B at the Same Time During the Sale Period

Published: 13 Apr 2022

### Question

How to count the number of customers who purchased products A and B during the sale period, excluding the customers who purchased only A or B at the same time.

### Environment

• Tableau Desktop

Step 1 Create a Set to group customers who purchased product A

1. Right click on [Customer ID] in the data pane and click [Create]>[Set]
2. In the Create Set dialog, do the following and click OK
a. Name the set  [Customers who purchased A]
b. Select [Condition] tab and put in the following formula into [By formula]
COUNT(IF [Product]="A" AND [Sales]="Y" THEN [Customer ID] END)>0

Step 2 Create a Set to group customers who purchased product B

1. Right click on [Customer ID] in the data pane and click [Create]>[Set]
2. In the Create Set dialog, do the following and click OK
a. Name the set  [Customers who purchased B]
b. Select [Condition] tab and put in the following formula into [By formula]
COUNT(IF [Product]="B" AND [Sales]="Y" THEN [Customer ID] END)>0

Step 3 Create a Combined Set to group customers who purchased both product A and product B

1. Multi-select [Customers who purchased A] and [Customers who purchased B] in the data pane and right click on them > click [Create combined set]
2. In the Create Set dialog, do the following and click OK
a. Name the set  [Customers who purchased A and B (Set)]
b. From the list, select [Shared members in both sets]
※- [Create Combined Sets]

Step 4 Display the number of customers who purchased both A and B on the view

1. Click the down arrow next to Dimensions in the data pane and select Create Calculation Field…
2. In the Calculation dialog, put in the following formula and click OK
a. Name the calculation field  [Customer who purchased both A and B ?]
b. put in the following formula

IF [Customers who purchased A and B (Set)] THEN "Yes"
ELSE "No"
END
3. Drag calculation field  [Customer who purchased both A and B ?] to Row shelf
4. Click the down arrow next to Dimensions in the data pane and select Create Calculation Field…
5. In the Calculation dialog, put in the following formula and click OK
a. Name the calculation field  [#CutomerID]
b. put in the following formula

COUNTD([Customer ID])
6. Drag calculation field  [#CutomerID] to [Text] Marks card.

Please refer to "Sample.twbx" attached for more details.