Forum Discussion
ashmitp869
9 months agoResponsive Resident
Help with Linear Trend line ?
Hi, Help me to get the trend line like below . I have used LINESTX but I am getting the result below. I have attached my sample pbix file. https://github.com/suvechha/samp...
Syndicate_Admin
8 months agoAdministrator
Hi @Syndicate_Admin
Try These formulas.
They include monthly aggregation before the trend calculation
Infringements Trend =
VAR Months =
-- One row per month (MonthYear + numeric month order)
SUMMARIZE (
ALLSELECTED ( 'Date' ),
'Date'[Year-Month Order], -- numeric chronological key (YYYYMM)
'Date'[MonthYear] -- display label
)
VAR Known =
-- Add X and Y for regression
ADDCOLUMNS (
Months,
"KnownX",
RANKX (
Months,
'Date'[Year-Month Order], -- sorting by monthly numeric key
,
ASC
),
"KnownY",
CALCULATE ( [Infringements] ) -- monthly aggregated value
)
VAR KnownFiltered =
-- Remove empty months
FILTER ( Known, NOT ISBLANK ( [KnownY] ) )
VAR SlopeIntercept =
-- Linear regression over monthly points
LINESTX ( KnownFiltered, [KnownY], [KnownX] )
VAR Slope =
-- Regression slope (scalar)
MAXX ( SlopeIntercept, [Slope1] )
VAR Intercept =
-- Regression intercept (scalar)
MAXX ( SlopeIntercept, [Intercept] )
VAR CurrentMonthOrder =
-- The month currently in row context of the visual
SELECTEDVALUE ( 'Date'[Year-Month Order] )
VAR CurrentX =
-- Lookup the X-value (rank) for the current month
MAXX (
FILTER ( Known, 'Date'[Year-Month Order] = CurrentMonthOrder ),
[KnownX]
)
RETURN
IF (
ISBLANK ( CurrentMonthOrder ),
BLANK (),
Intercept + Slope * CurrentX -- trend value for the current month
)
DL Validation Trend =
VAR Months =
-- One row per month (MonthYear + numeric month order)
SUMMARIZE (
ALLSELECTED ( 'Date' ),
'Date'[Year-Month Order], -- numeric chronological key (YYYYMM)
'Date'[MonthYear] -- display label
)
VAR Known =
-- Add X and Y for regression
ADDCOLUMNS (
Months,
"KnownX",
RANKX (
Months,
'Date'[Year-Month Order], -- sorting by monthly numeric key
,
ASC
),
"KnownY",
CALCULATE ( [Drivers Licence Validation] ) -- monthly aggregated value
)
VAR KnownFiltered =
-- Remove empty months
FILTER ( Known, NOT ISBLANK ( [KnownY] ) )
VAR SlopeIntercept =
-- Linear regression over monthly points
LINESTX ( KnownFiltered, [KnownY], [KnownX] )
VAR Slope =
-- Regression slope (scalar)
MAXX ( SlopeIntercept, [Slope1] )
VAR Intercept =
-- Regression intercept (scalar)
MAXX ( SlopeIntercept, [Intercept] )
VAR CurrentMonthOrder =
-- The month currently in row context of the visual
SELECTEDVALUE ( 'Date'[Year-Month Order] )
VAR CurrentX =
-- Lookup the X-value (rank) for the current month
MAXX (
FILTER ( Known, 'Date'[Year-Month Order] = CurrentMonthOrder ),
[KnownX]
)
RETURN
IF (
ISBLANK ( CurrentMonthOrder ),
BLANK (),
Intercept + Slope * CurrentX -- trend value for the current month
)
Ther updated pbix is attached
If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly
Ritaf1983
8 months agoSuper User
Syndicate_Admin
This is my solution why it appears with your name?