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
yaman123
Post Patron
Post Patron

DAX Trend Line with Month Year column

Hi, 

 

Is there a way to add a trend line using a month year column (mmmm yyyy).  The Month Year column is calculated from the SAMPLE DATE column.

 

The below is the query to calculate the trend - 

 

Linear regression =
VAR Known =
FILTER (
SELECTCOLUMNS (
ALLSELECTED ( Query1[SAMPLE_DATE] ),
"Known[X]", Query1[SAMPLE_DATE],
"Known[Y]", [Count of Member Code]
),
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] * Known[X] )
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 * Sum_X
)
VAR Intercept = Average_Y - Slope * Average_X
RETURN
SUMX ( DISTINCT ( Query1[SAMPLE_DATE]), Intercept + Slope * 'Query1'[SAMPLE_DATE])
 
But i get the below results when i use the Line and Clustered Column Chart visual. 
 
yaman123_0-1652704917219.png

 

 

Thanks

 
1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @yaman123 ,

 

This post will help you: Use Custom DAX to create a Linear Regression Trendline with a Dynamic Date Hierarchy in Power BI - M....

Regression2.png

 

It create a trend line by week-year. And here is the corresponding video:

Use Custom DAX to create a Linear Regression Trendline with a Dynamic Date Hierarchy in Power BI - Y....

 

 

Best Regards,

Icey

 

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

View solution in original post

6 REPLIES 6
Icey
Community Support
Community Support

Hi @yaman123 ,

 

This post will help you: Use Custom DAX to create a Linear Regression Trendline with a Dynamic Date Hierarchy in Power BI - M....

Regression2.png

 

It create a trend line by week-year. And here is the corresponding video:

Use Custom DAX to create a Linear Regression Trendline with a Dynamic Date Hierarchy in Power BI - Y....

 

 

Best Regards,

Icey

 

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

johnt75
Super User
Super User

Set up a date table linked to the sample date column and then in your measure definition replace every mention of the sample date column with the column from your date table which you are using in the visual - e.g. 'Date'[Year month].

Its important that the column used in the trend line measure matches exactly with the column you are using as the axis

hI @johnt75 

 

I get the below error when changing the measure definitions and adding the new column as the X axis? 

 

yaman123_0-1652706910376.png

 

My new measure - 

 

Linear regression =
VAR Known =
FILTER (
SELECTCOLUMNS (
ALLSELECTED ( 'Date Table'[Date Month Year] ),
"Known[X]", 'Date Table'[Date Month Year],
"Known[Y]", [Count of Member Code]
),
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] * Known[X] )
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 * Sum_X
)
VAR Intercept = Average_Y - Slope * Average_X
RETURN
SUMX ( DISTINCT ( 'Date Table'[Date Month Year]), Intercept + Slope * 'Date Table'[Date Month Year])

 

Try changing the Date Month Year column to be of type date. If you store it as 01/mm/yyyy then you can set the format string to display just month and year, it should give the results you're looking for

Still not giving me the results unfortunately 

 

yaman123_0-1652707886374.png

Date Month column is - 

Date Month Year = FORMAT('Date Table'[Date], "mmmm yyyy")
Data Type is Date and Format to Month Year
 
Linear regression =
VAR Known =
FILTER (
SELECTCOLUMNS (
ALLSELECTED ( 'Date Table'[Date Month Year] ),
"Known[X]", 'Date Table'[Date Month Year] ,
"Known[Y]", [Count of Member Code]
),
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] * Known[X] )
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 * Sum_X
)
VAR Intercept = Average_Y - Slope * Average_X
RETURN
SUMX ( DISTINCT ( 'Date Table'[Date Month Year]), Intercept + Slope * 'Date Table'[Date Month Year])
 
 

The only other thing I can suggest is trying

Date Month Year = DATE( YEAR('Date Table'[Date]), MONTH('Date Table'[Date]), 1)

Other than that, your code appears to be the same as what I have used for trend lines in the past.

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.