As you might have seen, I wrote a blog article to calculate correlation in DAX. This was done by defining multiple measures that were used in the final correlation formula. The drawback of this approach was that it was quite time consuming to create a new correlation formula with other columns, the number of calculated measures increased and performance was not optimal.
I tried to tacle the above drawbacks and this resulted in the following formula:
Correlation = var cv = VALUES ( Data[Perioden] ) var cas = ALLSELECTED ( Data[Perioden] ) var x_sd = CALCULATE (STDEVX.P ( cv, [x]), cas ) var y_sd = CALCULATE (STDEVX.P ( cv, [y]), cas ) var SD_Product = CALCULATE ( x_sd * y_sd ) var x_mean = CALCULATE ( AVERAGEX ( cv, [x] ), cas ) var y_mean = CALCULATE (AVERAGEX ( cv, [y] ), cas ) var vDiff_Mean_Product = CALCULATE ( AVERAGEX ( cv, ( [y] - y_mean ) * ([x] - x_mean ) ), cas ) return CALCULATE ( vDiff_Mean_Product / SD_Product)
All you need to do is replace X and Y with the (calculated) measures for which you would like to know the correlation.
The first two variables cs and cas represent the dimension that define the array of values used to calculate the correlation. So your slicers could result in subset for a certain product, geo location, etc. Then with that selection made you would like to know the correlation over a selected period. In that case period is respresented by 'Data[Perioded]'.
By using variables, performance might be improved because the number of times that specific calculation is done will be limited.
I hope you find this calculation usefull. Suggestions and feedback are much appreciated.
In this post I will show how to run an R script from Power BI which will execute an Association rule learning script to perform market basket analysis.
In this example we will not look at products sold, but products sharing shelf space.
The dataset
Our basic dataset looks like this.
Our products:
The distribution / presence of products on the shelf of a customer:
The Power BI building blocks
The data model
As for the DAX part we will start with this post of Marco Russo and Alberto Ferrari.
So the data model in Power BI looks like this:
The R visualization
We will look at the DAX part later on. First we add an R component with a script that will return the AR rules it found.
The table contains the basic output that is to be expected from AR. We will try to build these measures in DAX later on.
Unfortunately Power BI initializes a new R sessions each time the R visualization is run / cross filtered. Therefore I tried to use a much base R as possible. As for the libraries that need to be loaded. I put these in a separate folder on my local drive and specified the folder name in the library command.
Building it in DAX
Support
The output of the arules R script can be built in DAX whenever it concerns single item combinations, so X -> Y. So not A, B -> Y. The 'support' measure is basically the '[Orders with Both Products %]' described by Russo and Ferrari. Just to show how its implemented on our dataset.
Customers with Both Products % = IF ( NOT ( [SameProductSelection] ); DIVIDE ( [Customers with Both Products]; [Unique Customers All] ) )
In this blogpost I show how to calculate the correlation in DAX. This post will be refined in the future, also to show the comparison with R. The code is shown so you see how to run it in DAX studio. We will investigate the correlation between visits and sales. We define the standard deviation for visits: