Forum Discussion
Simple Linear Regression with DAX
- 10 years ago
In theory, I believe that you could use any filtering mechanism you wanted, you would not have to use ALL. The issue is that if you don't use some sort of aggregation function like SUMX and try to just use SUM, the process will not work because when you place those measures into a column formula, they become filtered by the row they are in and it jacks everything up (technical term). So, you HAVE to use some sort of FILTER. I used ALL as an example since I theorized that would be a common requirement to do a linear regression using all available data.
It is a good thing to call out, I originally tried to just use COUNT and SUM in my measures and was getting a lot of "Not a number" in my Estimate column until I realized that I needed to use COUNTAX and SUMX instead so it is definitely worth noting. Thanks for calling it out.
I think that a logical next step would be to come up with a Simple Rolling Linear Regression Pattern, that would be pretty spiffy. Probably could do that using a combination on the rolling aggregations patterns on daxpatterns.com and this pattern here.
Cool, thanks for the step-by-step.
With using "ALL" in the measures, does that limit filtering based on a date range for the time intelligence functions? I.E. - projecting next quarter based on only last quarter? Or is that intentional based on wanting the max available time horizon for analysis?
- Greg_Deckler10 years agoCommunity Champion
In theory, I believe that you could use any filtering mechanism you wanted, you would not have to use ALL. The issue is that if you don't use some sort of aggregation function like SUMX and try to just use SUM, the process will not work because when you place those measures into a column formula, they become filtered by the row they are in and it jacks everything up (technical term). So, you HAVE to use some sort of FILTER. I used ALL as an example since I theorized that would be a common requirement to do a linear regression using all available data.
It is a good thing to call out, I originally tried to just use COUNT and SUM in my measures and was getting a lot of "Not a number" in my Estimate column until I realized that I needed to use COUNTAX and SUMX instead so it is definitely worth noting. Thanks for calling it out.
I think that a logical next step would be to come up with a Simple Rolling Linear Regression Pattern, that would be pretty spiffy. Probably could do that using a combination on the rolling aggregations patterns on daxpatterns.com and this pattern here.
- mstefancik10 years agoAdvocate IV
Or we can wait for R scripts within power BI and then be able to do any kind of statistical analysis :) but good job.
- duncanwil9 years agoFrequent Visitor
That's very useful I think. I want to add a couple of things:
I plotted the sales v profit graph for my data, added the trendline and then, using the results of your DAX, put the intercept and slope values in a Card ... very easy to do I am sure you agree!
To take this a step further, we should also look at the residuals so I created a new column for that, which in my case is
Residuals = sales_profit[Sales]-sales_profit[Profit]
I can then easily plot the estimates or fitted values on a new graph on the X axis and the residuals on the Y axis
I haven't done this yet but the next step might be to add another column for Studentised Residuals: residual/standard deviation of residuals ... then use that for further analysis.
I am aware that we can do all of this in R in Power BI but I still think you did us all a favour!