Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ioper
Frequent Visitor

Linear Regression to predict values

Hello, 

 

I am trying to predict future values based on historical data. The new values are displayed in a matrix visual with future dates as the rows. Here is my code: 

 

linReg = 
VAR Known =
    FILTER (
        SELECTCOLUMNS (
            ALLSELECTED (' Claims This Year'[ Date] ),
            "Known[X]", 'Claims This Year'[ Date],
            "Known[Y]", [totalAmount]
        ),
        AND (
            NOT ( ISBLANK ( Known[X] ) ),
            NOT ( ISBLANK ( Known[Y] ) )
        )
    )
VAR Count_Items =
    COUNTROWS ( Known )
VAR Sum_X =
    SUMX ( Known, Known[X] )
VAR Sum_X2 =
    SUMX ( Known, Known[X] ^ 2 )
VAR Sum_Y =
    SUMX ( Known, Known[Y] )
VAR Sum_XY =
    SUMX ( Known, Known[X] * Known[Y] )
VAR Average_X =
    AVERAGEX ( Known, Known[X] )
VAR Average_Y =
    AVERAGEX ( Known, Known[Y] )
VAR Slope =
    DIVIDE (
        Count_Items * Sum_XY - Sum_X * Sum_Y,
        Count_Items * Sum_X2 - Sum_X ^ 2
    )
VAR Intercept =
    Average_Y - Slope * Average_X
RETURN Intercept + Slope * [monthNumber]

I have a feeling it has something to do with the ALL SELECTED part because the historical dates are not in the visual but I am not sure. 

 

Also, please advise on how to counter blanks in the historical data matrix (Dates = rows, type = columns, claimTotal= values) being counted as nulls instead of 0. This is changing my slope. 

 

Thank you!!

3 REPLIES 3
v-lili6-msft
Community Support
Community Support

hi, @ioper 

Use ALLSELECTED, it will only calculate the date that has been filtered.

and for being counted as nulls instead of 0, if you could try to filter out blank value in the calculation.

 

and if not your case, Sample data and expected output would help tremendously.
Please see this post regarding How to Get Your Question Answered Quickly:
https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

 

Best Regards,

Lin

 

 

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-lili6-msft 

This data is summary data (a visual of sums made from the raw data

 MonthNoCar TrainTractor 
Jan-171134
Feb-172265
Mar-173356
Apr-174438
May-175567
Jun-176659
Jul-1774915
Aug-1788124
Sep-1799142
Oct-171021536
Nov-17111165
Dec-17122184
Jan-18139208
Feb-1814102056
Mar-181511215
Apr-181612 8
May-181713 24
Jun-1818142813
Jul-1819163025
Aug-182015256
Sep-1821142125
Oct-1822151436
Nov-1823163136
Dec-1824173518
Jan-192513619
Feb-1926 3720
Mar-1927184040
Apr-1928194241
May-1929214234
Jun-1930221940

** Ideally, those blanks should be counted as 0 if there is no data. 

Now, my goal is to find the slope and intercept of each column so that I can predict sales for the rest of the year. 

so that I would have

 MonthNoCarTrainTractor
Jul-1931

=(SLope * monthNo) + intercept

=(SLope * monthNo) + intercept=(SLope * monthNo) + intercept
Aug-1932=(SLope * monthNo) + intercept=(SLope * monthNo) + intercept=(SLope * monthNo) + intercept
Sep-1933=(SLope * monthNo) + intercept=(SLope * monthNo) + intercept=(SLope * monthNo) + intercept
Oct-1934=(SLope * monthNo) + intercept=(SLope * monthNo) + intercept=(SLope * monthNo) + intercept
Nov-1935=(SLope * monthNo) + intercept=(SLope * monthNo) + intercept=(SLope * monthNo) + intercept
Dec-1936=(SLope * monthNo) + intercept=(SLope * monthNo) + intercept=(SLope * monthNo) + intercept

 

 

@v-lili6-msft 

ALLSELECTED is currently being used, but the result is still incorrect. Is there anywhere else that the code needs to be changed?

 

Also with the 0 sitution, I want them to be counted as 0 and not null values (as it would in excel). Please advise

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.