cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Super User
Super User

Simple Linear Regression with DAX

OK, related to forecasting, I went ahead and followed my own advice and built out a forecasting model in DAX using simple linear regression. Probably can be improved but here is how I did it:

 

CSV Files:

regression.csv

X,Y
60,3.1
61,3.6
62,3.8
63,4
65,4.1

 

estimation.csv:

X
64
75
58

 

In regression table, create the following columns:

X*Y = [X]*[Y]

X*X = [X]*[X]

 

In regression table, create the following measures:

Count = COUNTAX(ALL('regression'),[X])

SumX = SUMX(ALL('regression'),[X])

SumY = SUMX(ALL('regression'),[Y])

SumX*Y = SUMX(ALL('regression'),[X*Y])

SumX*X = SUMX(ALL('regression'),[X*X])

Slope = ([Count]*[SumX*Y] - [SumX]*[SumY]) / ([Count]*[SumX*X] - [SumX]*[SumX])

Intercept = ([SumY] - [Slope]*[SumX]) / [Count]

 

In regression and estimation tables create the following column:

Estimate = [Intercept] + [Slope]*[X]

 

You can now plot your original values and the linear regression estimation values as well as plot your X values for estimation and the linear regression estimates.


I have book! Learn Power BI from Packt


Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Simple Linear Regression with DAX

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.


I have book! Learn Power BI from Packt


Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

View solution in original post

7 REPLIES 7
ALeef Member
Member

Re: Simple Linear Regression with DAX

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?

Super User
Super User

Re: Simple Linear Regression with DAX

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.


I have book! Learn Power BI from Packt


Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!

View solution in original post

mstefancik Member
Member

Re: Simple Linear Regression with DAX

Or we can wait for R scripts within power BI and then be able to do any kind of statistical analysis Smiley Happy but good job.

Luke-Bongiorno Frequent Visitor
Frequent Visitor

Re: Simple Linear Regression with DAX

I was following along with this and think I understand the basic calculation. Do you mind providing an example for how X and Y relate to a real world scenario. Am I correct to assume X may be time and Y a sales $ amount? 

Randy Frequent Visitor
Frequent Visitor

Re: Simple Linear Regression with DAX

Thx for the example!

 

I was able to use your example to do a simple lineair regression forecast using a calender table. For "X" I used the date numbers (E.g.: 20150101), which is an extra column 'DateNumber' in my date table that displays the date as a number. Because I need future dates in my calender table to plot the forecast against, I had to add an extra FILTER to exclude future years from the sample being used in the calculation.

 

Count:=COUNTAX(FILTER(ALL('Calender'); YEAR(Calender[DATE]) < YEAR(NOW()));'Calender'[DateNumber])
duncanwil Frequent Visitor
Frequent Visitor

Re: Simple Linear Regression with DAX

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!

JavierP Visitor
Visitor

Re: Simple Linear Regression with DAX

Thank You very much for the solution.

 

As say a partner .... 

 

"I was following along with this and think I understand the basic calculation. Do you mind providing an example for how X and Y relate to a real world scenario. Am I correct to assume X may be time (measure agregate months, etc) and Y a sales $ amount (measure agregate ... "Amount / personal", ..... )? "

 

Any Solution???

 

Thank you in advance

Helpful resources

Announcements
Can You Solve These Challenge

Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

Community News & Announcements

Community News & Announcements

Get your latest community news and announcements.

Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Community Kudopalooza

Win Power BI Swag with Community Kudopalooza!

Each week, complete activities and be qualified in the drawing for cool Power BI Swag.

Users Online
Currently online: 136 members 1,457 guests
Please welcome our newest community members: