Create a Co-Occurrence Visualization Using a Parameter and a Set
You can create co-occurrence visualizations in Tableau. With a co-occurrence visualization, users can pick one field value and then see which other fields values it co-occurs with, and how often. One practical application of such analysis is a market basket analysis, which you can use to discover and understand customer purchasing behavior. You can use a market basket analysis to answer questions like the following:
-
How many people bought both Product A and Product B?
-
What other products do people who purchased Product A generally buy?
-
Which other courses do students who enrolled in Course A frequently enroll in?
Follow the steps in this article to create a co-occurrence visualization (in this case, a market basket analysis view) using a parameter, calculated fields, and a set. At a high level, the steps involved are:
-
Create a parameter that you will use to dynamically modify the view based on the item you select.
-
Create calculated fields that you will use to return which items are also ordered when a particular item is ordered.
-
Create a set to determine whether an order has the item that was selected in the parameter control.
-
Build the view to display which items are also contained in an order with the selected item.
The scenario uses the Sample - Superstore data source provided with Tableau Desktop. The details are covered in the following sections.
Create a Parameter
Follow these steps to create a parameter and then display a parameter control that users can use to pick a Sub-Category value.
-
Connect to the Sample - Superstore data source.
-
Right-click (control-click on a Mac) in the Data pane and select Create > Parameter.
-
In the Create Parameter dialog box, do the following:
-
Name the parameter Order Contains.
-
For the Data Type, choose String.
-
For Allowable Values, choose List.
-
In the List of values section click Add from Field > Sub-Category.
-
Click OK.
-
- Right-click the Order Contains parameter in the Data pane and choose Show Parameter Control.
Create Calculated Fields
Follow these steps to create the calculated fields that you will use to show which items are also ordered when the item selected by the user (via the parameter control) is ordered.
-
Create a calculated field to identify products that the order also contains (in addition to the one that the user selects).
Choose Analysis > Create Calculated Field to open the calculation editor. Name the calculated field Then Order Also Contains and type or paste the following in the formula area:
IF [Sub-Category] <> [Order Contains] THEN [Sub-Category] END
You may have to replace
<>
with<>
after you paste. -
Create another calculated field to identify matching products.
Name the field Product Matches and type or paste the following in the formula area:
IF [Sub-Category] = [Order Contains] THEN 1 END
Create a Set
-
Now create a set to determine whether an order has the item that was selected in the parameter control.
-
Hover the cursor over the Order ID dimension in the Data pane, click the down arrow at the right end of the field, and choose Create > Set.
-
In the Create Set dialog box, type Order Has Selected Product in the Name text box.
-
Go to the Condition tab, select By field, and in the drop-down lists make the following selections and entries to build the condition:
- In the first drop-down list, select Product Matches.
- In the second drop-down list, select Sum.
- In the next drop-down list, select >=.
- In the last text box, type 1.
- Click OK.
-
Click OK.
Build the View
Finally, build the view to display which items are also contained in an order with the selected item.
-
Drag Then Order Also Contains to Columns.
-
Drag Order ID to Rows.
In the warning dialog box, click Add all members.
-
Click the Order ID field on Rows and choose Measure > Count (Distinct) to change the aggregation.
-
Right-click (control-click on a Mac) the Null bar on the x-axis and choose Exclude.
-
Drag the Order Has Selected Product set to the Filters shelf.
-
Press Ctrl+W to swap the fields on Rows and Columns.
You (or your users) can now use the Order Contains parameter control to select an item in an order, and then see a bar chart showing which other items are also included in orders with the selected item.