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:

  1. Create a parameter that you will use to dynamically modify the view based on the item you select.

  2. Create calculated fields that you will use to return which items are also ordered when a particular item is ordered.

  3. Create a set to determine whether an order has the item that was selected in the parameter control.

  4. 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.

  1. Connect to the Sample - Superstore data source.

  2. Right-click (control-click on a Mac) in the Data pane and select Create > Parameter.

  3. In the Create Parameter dialog box, do the following:

    1. Name the parameter Order Contains.

    2. For the Data Type, choose String.

    3. For Allowable Values, choose List.

    4. In the List of values section click Add from Field > Sub-Category.

    5. Click OK.

  4. 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.

  1. 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 &lt;&gt; with <> after you paste.

  2. 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

  1. Now create a set to determine whether an order has the item that was selected in the parameter control.

  2. 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.

  3. In the Create Set dialog box, type Order Has Selected Product in the Name text box.

  4. Go to the Condition tab, select By field, and in the drop-down lists make the following selections and entries to build the condition:

    1. In the first drop-down list, select Product Matches.
    2. In the second drop-down list, select Sum.
    3. In the next drop-down list, select >=.
    4. In the last text box, type 1.
    5. Click OK.
  5. Click OK.

Build the View

Finally, build the view to display which items are also contained in an order with the selected item.

  1. Drag Then Order Also Contains to Columns.

  2. Drag Order ID to Rows.

    In the warning dialog box, click Add all members.

  3. Click the Order ID field on Rows and choose Measure > Count (Distinct) to change the aggregation.

  4. Right-click (control-click on a Mac) the Null bar on the x-axis and choose Exclude.

  5. Drag the Order Has Selected Product set to the Filters shelf.

  6. 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.

Thanks for your feedback!Your feedback has been successfully submitted. Thank you!