Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
9 years ago
Solved

Formula for trendline

I am using the October download of PBI.

I can add a trendline for scattered plot and a line chart.

However, I cannot find the option to add the formula (just like in Excel).

 

Any idea?

 

Thanks,

Tamir

  • ImkeF's avatar
    ImkeF
    9 years ago

    Hi there,

    this function replicates the Excel-Trend-function, just without the possiblity to define your own slope and intercept:

     

     (YList as list, NoOfIntervalls as number) =>
    let
    Source = Table.FromColumns({YList}),
    xAxis = Table.AddIndexColumn(Source, "Index", 1, 1),
    Rename1 = Table.RenameColumns(xAxis,{{"Column1", "y"}, {"Index", "x"}}),
    AvgX = List.Average(Rename1[x]),
    AvgY = List.Average(Rename1[y]),
    x = Table.AddColumn(Rename1, "xX", each [x]-List.Average(Rename1[x])),
    y = Table.AddColumn(x, "yY", each [y]-List.Average(x[y])),
    xy = Table.AddColumn(y, "xy", each [xX]*[yY]),
    xXx = Table.AddColumn(xy, "xXx", each [xX]*[xX]),
    a = List.Sum(xXx[xy])/List.Sum(xXx[xXx]),
    b = AvgY-(a*AvgX),
    ListIntervalls = {List.Max(Rename1[x])+1..List.Max(Rename1[x])+NoOfIntervalls},
    TableIntervalls = Table.FromList(ListIntervalls, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Rename = Table.RenameColumns(TableIntervalls,{{"Column1", "x"}}),
    Values = Table.AddColumn(Rename, "y", each [x]*a+b),
    TREND = Table.Combine({Rename1,Values})
    in
    TREND

19 Replies

  • MEhrenmueller's avatar
    MEhrenmueller
    Most Valuable Professional

    As far as I know, there is no possibility to specify a formula for the trend line.

     

    In the most recent version there is a possibility to add different kinds of lines. Would one of these fit your needs?

     

  • This is simple linear regression. Also, I did not figure this out, but it has been so long that I don't remember where I got it. When I find the guy's name, I'll credit him here.

     

    Just replace your dates (colored blue), and the measure of interest (colored red).

     

    Total Sales Trend =
    VAR Known =
    FILTER (
    SELECTCOLUMNS (
    CALCULATETABLE ( VALUES ( Dates[Date] ), ALLSELECTED( Dates) ),
    "Known[X]", Dates[Date],
    "Known[Y]", [Total Sales]
    ),
    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
    VAR Result = SUMX ( DISTINCT ( Dates[Date] ),
    Intercept + Slope * Dates[Date]
    )
    RETURN
        IF(SELECTEDVALUE( Dates[Date] ) >= TODAY(),
    BLANK(),
            Result)

     

     

    • mc2863's avatar
      mc2863
      Regular Visitor

      Thanks for sharing, do you know how we could amend this to be dynamic and account for filtered data either in a table or in an actual visual.  We are aggregating data at a product level and would like for the calculated trend to represent the selected product.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous 

     

    As of now, there is no native format option that will give you the regression summary output (Y intercept, slope, R^2, ect.)

    You can write some DAX to derive the equation or you can download the 'Craydec Regression Chart' visual from BPI marketplace.  This visual will give you the equation (Y= MX + B), R^2, and other regression output data.

    Obviously, you can also export your data set to Excel to do the complete regression analysis (P-value, T-test, F-score, std error, and much more).

     

    I hope this helps!

     

    -Eric