Market Basket Analysis using Power BI

No comments

 

It’s been almost two years since I posted about the CONCATENATEX() DAX function here. At the end of that post I promised to publish a tutorial on how one might do Market Basket analysis using this function in Power BI. Well, I try to deliver on my promises even though it might take me two years to get there.

Please download the Zip file with the Market Basket Analysis model here.

Let’s 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

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:

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


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]

Margin % = DIVIDE(Sales[Margin], Sales[Sales])
(Please note that you should create a calculation for Margin %, I am using this field for demonstration purposes only
)

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

After that, we can add additional fields to the Market Basket table that will help us with our analysis:

Total Sales = SUMX(RELATEDTABLE(Sales)RELATED(‘Product'[Price]) * Sales[Quantity])

Total Margin = ‘Market Basket'[Total Sales] – ‘Market Basket'[Total Cost]

Total Cost = SUMX(RELATEDTABLE(Sales)RELATED(‘Product'[Cost]) * Sales[Quantity])

Margin % = DIVIDE(‘Market Basket'[Total Margin], ‘Market Basket'[Total Sales])

Frequency = COUNTX(RELATEDTABLE(Sales), Sales[Market Basket])

Avg Transaction Value = DIVIDE(‘Market Basket'[Total Sales], ‘Market Basket'[Frequency])

With all the necessary calculated elements in place, our model is ready for analysis. Please note that this is obviously a simplified case of Market Basket analysis, but hopefully it demonstrates the power of CONCATENATEX() and some of the capabilities that it enables.

You can play with the slicers to zero-in on market baskets with specific profitability and Average Transaction Value and then explore which products are part of it… The trick question is this – How can you enhance the dataset and the model so that we can start analyzing which products are good candidates to be sold on promotion because they pull through very profitable Market Baskets?

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s