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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
IHOUM
Frequent Visitor

Need Help with DAX Measure Using LINESTX and Dynamic Filtering

Hi everyone,

I'm working on a DAX measure that performs a linear regression on cost data and then predicts the cost for the end of the current month. I'm using the LINESTX function to calculate the slope and intercept of the regression line. Below is the code I'm working with:

 

 

LinestX = 
VAR azcost_agg = 
    SUMMARIZECOLUMNS(
        azcost_f[DT_DATE],
        azcost_f[ID_SUBSCRIPTION],
        FILTER(
            azcost_f,
            azcost_f[DT_DATE] >= DATE(YEAR(TODAY()), 1, 1)
        ),
        "TotalCost", SUM(azcost_f[N_CostInBillingCurrency])
    )

-- Apply the LINESTX function and extract coefficients
VAR cost_prediction = 
    ADDCOLUMNS(
        LINESTX(azcost_agg, [TotalCost], [DT_DATE]),
        "Slope", SELECTCOLUMNS(LINESTX(azcost_agg, [TotalCost], [DT_DATE]), "Slope1", [Slope1]),
        "Intercept", SELECTCOLUMNS(LINESTX(azcost_agg, [TotalCost], [DT_DATE]), "Intercept", [Intercept])
    )

-- Extract specific values
VAR slope = MAXX(cost_prediction, [Slope1])
VAR intercept = MAXX(cost_prediction, [Intercept])

-- Calculate the prediction for the end of the current month
VAR prediction = slope * EOMONTH(TODAY(), 0) + intercept

RETURN 
    prediction

 

My questions:

  1. Is this measure written correctly? Specifically, I'm concerned about how I'm handling the LINESTX function and extracting the slope and intercept. Are there any improvements I should make?
  2. How can I make this measure dynamic based on user-selected filters? I want the regression to recalculate automatically when a user applies different filters in the report. Right now, I'm worried that the SUMMARIZECOLUMNS might not be respecting the user's selections. Is there a way to make this more dynamic?

Any insights or suggestions would be greatly appreciated! Thanks in advance for your help.

1 REPLY 1
bhanu_gautam
Super User
Super User

@IHOUM , To ensure that the measure respects user-selected filters, you should use ALLSELECTED or REMOVEFILTERS to control the context in which the regression is calculated. This will allow the measure to dynamically recalculate based on the filters applied in the report.

 

 

LinestX =
VAR azcost_agg =
SUMMARIZECOLUMNS(
azcost_f[DT_DATE],
azcost_f[ID_SUBSCRIPTION],
"TotalCost", SUM(azcost_f[N_CostInBillingCurrency])
)

-- Apply the LINESTX function and extract coefficients
VAR cost_prediction =
LINESTX(azcost_agg, [TotalCost], [DT_DATE])

-- Extract specific values
VAR slope = MAXX(cost_prediction, [Slope])
VAR intercept = MAXX(cost_prediction, [Intercept])

-- Calculate the prediction for the end of the current month
VAR prediction = slope * EOMONTH(TODAY(), 0) + intercept

RETURN
prediction




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

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.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.