How to Create Market Basket Analysis in Tableau

Cody Schneider9 min read

Ever wondered why grocery stores often place chips right next to the soda, display steak sauce by the butcher counter, or even position diapers near the beer aisle? These product placements aren't happy accidents, they're the result of Market Basket Analysis, a powerful technique used to discover which products are frequently purchased together. This article will guide you step-by-step through creating your own Market Basket Analysis using Tableau to uncover hidden purchasing patterns in your data.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

What is Market Basket Analysis, Anyway?

Market Basket Analysis is a data mining technique that identifies the strength of association between pairs of products. Think of it as looking into your customers' virtual "shopping baskets" to see what they're buying in a single transaction. The goal is to find relationships you might not have guessed existed.

The classic, almost mythical example is the "Diapers and Beer" link found by a retail chain in the 1990s. The analysis revealed that on Friday nights, men who came in to buy diapers were also highly likely to grab a six-pack of beer. This insight led the store to place the beer display next to the diapers, resulting in a significant sales increase for both items.

For your business, these insights can drive powerful strategies, such as:

  • Store Layout & Product Placement: Place complementary products closer together, both in physical stores and on your e-commerce site. For instance, "Customers who bought this also bought..."
  • Targeted Promotions & Cross-Selling: Create bundled offers or targeted email campaigns. If a customer buys a new laptop, you can offer them a discount on an accompanying mouse or keyboard.
  • Content & Marketing Strategy: If you know coffee buyers also love certain pastries, you can create marketing campaigns or content that feature both together.
GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Preparing Your Data

Before you can build anything in Tableau, you need your data in the right format. Thankfully, the data structure for Market Basket Analysis is very straightforward. All you need is transactional data with, at minimum, a unique identifier for each transaction and an identifier for each product.

Your data should look something like this, with one row for every item in every order:

OrderID, Product
1001,   "Coffee Beans"
1001,   "Milk"
1001,   "Sugar"
1002,   "Tea Bags"
1002,   "Milk"
1003,   "Coffee Beans"
1003,   "Croissants"

The essential columns are:

  • Order ID (or Transaction ID): A unique value that groups all items bought in a single purchase.
  • Product Name (or Product ID): The specific item that was purchased.

Other details like customer ID, date, or category can be useful for more advanced analysis, but these two columns are the only ones you'll need to get started.

Step 1: The Critical Self-Join in Tableau

The magic that makes Market Basket Analysis possible in Tableau is a technique called a self-join. Essentially, you are going to join your data to itself. This allows you to create pairs of products that appear in the same order.

Don't worry, it's easier than it sounds. Here’s how you do it:

  1. Connect Tableau to your data source (e.g., an Excel file or database with your transaction data).
  2. In the 'Data Source' tab, drag your orders table onto the canvas.
  3. Now, drag the same orders table onto the canvas again. Tableau will automatically try to create a join relationship.
  4. Click on the join icon (the overlapping circles) to edit the join clause.

Configuring the Join

There are two conditions you need to set up for this join to work correctly:

  1. Join on Order ID: The first condition is to join the tables where Order ID equals Order ID. This ensures you are only pairing items from the same transaction. Orders.OrderID = Orders1.OrderID
  2. Ensure Products Are Different: The second condition is critical. You need to ensure a product isn't paired with itself. To do this, add another join clause stating that Product Name from the first table does not equal Product Name from the second table. Orders.Product Name <> Orders1.Product Name

Use an inner join for this setup. Your final join configuration in the Tableau Data Source pane should connect the two tables on these two specific clauses. Once you've done this, click out of the dialog and move to a new worksheet. You are now ready to build your visualization!

Step 2: Building the Product Association Heatmap

A heatmap is a fantastic way to visualize the strength of relationships between product pairs. You'll see a grid of all your products, and the color intensity of each square will show how frequently two products were purchased together.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

1. Set Up the Rows and Columns

  • From your 'Data' pane, find Product Name from your original 'Orders' table. Drag it to the Columns shelf.
  • Next, find Product Name from the second, joined table (Tableau probably named it 'Orders1'). Drag this one to the Rows shelf.

You'll see a grid with all your product names listed on both the x-axis and y-axis. Now we just need to add the data to give it meaning.

2. Calculate the Number of Baskets (Support)

We need to tell Tableau how to measure the strength of the connection between any two products. The most basic metric is called "Support," which is simply the count of orders where both products appeared together.

  • Create a new calculated field. Go to 'Analysis' > 'Create Calculated Field'.
  • Name this calculation "Number of Baskets" or "Support".
  • The formula is straightforward. We want to count the unique number of orders. Enter: COUNTD([Order ID])
  • Using COUNTD (Count Distinct) is important because it ensures you count each transaction only once, even if it contains multiple rows for the same product pair after the join. Hit 'OK'.

3. Assemble the Heatmap

  • Drag your new calculated field, "Number of Baskets", to the Color shelf on the Marks card.
  • In the Marks card dropdown, change the Mark Type from 'Automatic' to 'Square'. This will fill in the grid, creating the heatmap effect.
  • You can also drag "Number of Baskets" to the Label shelf to display the count directly on each square for more detail. Feel free to adjust the colors to your liking (a gradient from light to dark works best).

Voila! You now have a basic Market Basket Analysis heatmap. The darker squares represent product pairs that are purchased together most often. You can immediately start spotting the strongest relationships.

Step 3: Making Your Analysis Interactive with a Parameter

A static heatmap is great, but a dynamic one is even better. Let's create a parameter that allows a user (like a category manager or marketer) to select a single product and instantly see what's most frequently purchased alongside it.

1. Create the Parameter:

  • Right-click in the empty space of your Data pane and select 'Create' > 'Parameter...'.
  • Let’s name it "Select a Product".
  • For 'Data Type', choose String.
  • For 'Allowable values', choose 'List'.
  • Click on 'Add values from' and select the Product Name field to automatically populate the list with all of your products.
  • Click 'OK'. Then, right-click on the new parameter in the data pane and select 'Show Parameter'. This will add a dropdown menu to your view.

2. Create a Calculated Field to Highlight the Selection:

Now we need a way to connect the parameter dropdown to our chart. We'll create another calculated field.

  • Name the field "Highlight Selected Product".
  • The formula will put the selected product at the top of our chart. Here’s an example formula: IF [Product Name] = [Select a Product] THEN 1 ELSE 0 END
GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

3. Filter and Sort the View:

  • Drag the newly created "Highlight Selected Product" field to the rows shelf, placing it to the left of your 'Product Name (Orders1)' pill. Right-click on it and choose 'Discrete'.
  • This will add your highlight selection filter at the left of the table. Simply sort descending to see the chosen product's matches sorted highest to lowest—making analysis much more efficient!

Now you have an interactive tool. When you select a product from the dropdown menu, the view will update to clearly show you which companion products have the highest purchase frequency.

Going Deeper: Understanding Confidence and Lift

While counting the number of baskets (Support) is a great start, a more advanced analysis also considers two other metrics: Confidence and Lift.

  • Confidence: This tells you the probability that a customer will buy Product B, given they have already bought Product A. It answers the question, "For customers who bought coffee, what percentage also bought milk?" It helps filter out products that are generally popular on their own.
  • Lift: This tells you how much more likely a customer is to buy Product B given that they bought Product A. A Lift value greater than 1 suggests a true relationship – the presence of Product A in the basket actually increases the likelihood of Product B being purchased.

Calculating these requires slightly more complex Level of Detail (LOD) expressions in Tableau, but they provide a much richer understanding of your product relationships by helping you distinguish between mere coincidence and a genuine purchasing affinity.

Final Thoughts

Market Basket Analysis is a foundational technique in retail analytics that can uncover powerful, actionable insights hidden within your sales data. By using Tableau's self-join capability, calculated fields, and interactive parameters, you can build a powerful tool to explore these product relationships yourself, guiding everything from store layout to your next marketing campaign.

Building this analysis manually in Tableau is an excellent skill, but we know it can feel complex, and maintaining it takes time you could be using to act on the insights. That's why we built Graphed. Instead of navigating joins and calculated fields, you can just connect your data and ask, "Show me which items are most frequently bought together," and get an interactive dashboard in seconds. Our goal is to save you the time spent on data wrangling so you can get straight to making smarter decisions for your business.

Related Articles