Market Basket Analysis (Power BI DAX Tutorial)

I have decided to spend some more time on Market Basket Analysis, but before I get into the storytelling part of it, I had to clean up and fix a few things in the model file for the Market Basket Analysis that I created a few years ago.

Market Basket Analysis is very important as retailers and CPG companies try to understand consumer behavior, particularly when it comes to deciding which products should be promoted, as the hope is that although we might be taking a margin hit on the promoted product, the rest of the products that we are hoping will get purchased in the same market basket will hopefully more than compensate for the cost of promotion.

You can download the updated model here.

Let us walk through the data first… Initially, we have two tables in our model, Sales and Product. The Sales table has every customer transaction, what products where part of the transaction and also quantity sold.


The product table has the Price and Cost information about the product that we will use to analyze size and profitability of our Market Baskets

Market Basket = 
CALCULATE(
    CONCATENATEX(Sales, Sales[Product ID], "-"), 
    FILTER(
        ALL(Sales), 
        Sales[Transaction ID] = EARLIER(Sales[Transaction ID])
    )
)

After we have the two tables loaded and related by Product ID, we can start enhancing the model.

The first order of business is to assign our sales transactions into Market Baskets. To do that, we need to create a new field on the Sales table. We will identify a market basket as a string of all Product IDs in that basket separated by “-” character. The code to add the Market Basket field to the Sales table is below:

Now our Sales table will look like the image below (please note that records with the same Transaction ID have the same value for the Market Basket)


While we are in the Sales table, we can add Sales, Cost, Margin and Margin% fields to it so we can do sales and margin analysis of individual products.

Sales = Sales[Quantity] * RELATED(‘Product'[Price])
Cost = Sales[Quantity] * RELATED(‘Product'[Cost])
Margin =Sales[Sales] – Sales[Cost]

The next step is to create the Market Basket table. To do that, make sure you are in the Report view then click on Modeling->New Table. The code to create the new table is below:

Market Basket = DISTINCT(Sales[Market Basket])
Now we need to make sure that all of our tables are properly related:

You can find the additional DAX calculations used in the model below:

Total Sales = [Sales]
Total Cost = SUMX(RELATEDTABLE(Sales), RELATED('Product'[Cost])*Sales[Quantity])
Total Margin = 
'Market Basket'[Total Sales] - 'Market Basket'[Total Cost]
Transactions = DISTINCTCOUNT(Sales[Transaction ID])
Avg Transaction Value = 
DIVIDE(
    'Market Basket'[Total Sales], 
    [Transactions]
)

3 thoughts on “Market Basket Analysis (Power BI DAX Tutorial)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s