Forum Discussion

Greg_Deckler's avatar
Greg_Deckler
Community Champion
10 years ago
Solved

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.

  • 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.

9 Replies

  • JavierP's avatar
    JavierP
    Regular Visitor

    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

  • 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_Deckler's avatar
      Greg_Deckler
      Community 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.

      • mstefancik's avatar
        mstefancik
        Advocate 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.

  • 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? 

  • 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])
  • pdsoutherland's avatar
    pdsoutherland
    Frequent Visitor

    Worked like a charm.  I just needed a simple trend line with a start X,Y value pair, and an end X,Y value pair in x increments of 100 and this worked perfect.  I've been looking for the solution to get out of Excel to compute Y values and this was the solution.

     

    Thanks!