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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
EnrichedUser
Helper III
Helper III

Dynamic Trend Line with LINESTX

Hi, 

 

I am hoping that the new release of LINESTX function makes this request more reasonable. I would like to create a measure to be used as trend line (linear regression) on a the line visual. My x-axis is my date table that will have the normal granularity options (Date, Week, Month, QTR). The y-axis is a measure that is an aggregate from a sales table. The sales table provide transaction level details (all transactions each day). 

 

The expected output would allow me filter the visual by fields in both the date table and sales tableTrend Line Date Example.PNG

- Change X Axis to day, week, month, etc (Date Table)

- Change Y Axis to specific regions, customer codes, items (Sales Table)

 

Lastly, from my reading it does seem needed to be able to reflect a direction of the reggressional analysis and my date table does have those index fields in 'DateKey', 'YearMonthNumber', etc.

y-axis measure would be [Avg Unit Price]

 

Unit Price = 
SUM( 'Sales History'[Unit Price BC] )

Invoices = 
COUNTROWS( 'Sales History' )

Avg Unit Price = 
DIVIDE( [Unit Price], [Invoices], 0 )

 


Attempts:

 

Trend = 
VAR dateset = 
    FILTER(
        SELECTCOLUMNS(
            ALLSELECTED( 'Sales History' ),
            "Known[X]", 'Sales History'[Invoice Date],
            "Known[Y]", [Avg Unit Price]
            ),
            NOT( ISBLANK( [Avg Unit Price] ) )
            )
VAR line =
    LINESTX(dateset, Known[Y], Known[X])
VAR slope = 
    SELECTCOLUMNS( line, [Slope1] )
VAR Intercept =
    SELECTCOLUMNS( line, [Intercept] )
VAR x = SELECTEDVALUE( 'Sales History'[Invoice Date] )
VAR y = x * slope + Intercept
RETURN y

Trend2 = 
VAR Known =
    FILTER (
        SELECTCOLUMNS (
            CALCULATETABLE ( VALUES ( 'Date'[Date] ), ALLSELECTED ('Date') ),
            "Known[X]", 'Date'[Date],
            "Known[Y]", [Avg Unit Price]
        ),
        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
    SUMX ( DISTINCT ( 'Date'[Date] ),
    Intercept + Slope * 'Date'[Date]
)

 


Related Articles: 
https://www.sqlbi.com/articles/implementing-linear-regression-in-power-bi/
https://learn.microsoft.com/en-us/dax/linestx-function-dax
https://kerrykolosko.com/kpi-trend-indicators-on-core-visuals/
https://community.fabric.microsoft.com/t5/Desktop/DAX-to-create-a-Trend-line/m-p/400663#M183065


 

3 REPLIES 3
lbendlin
Super User
Super User

Can you please reframe your request? What exactly are you asking for?

I would like to create a measure to be used as trend line.

Please provide sample data (with sensitive information removed) that covers your issue or question completely, in a usable format (not as a screenshot).
https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Please show the expected outcome based on the sample data you provided.

https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.