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

Add Trend Line to Column Chart with Categorical X Axis

Hi all, 

 

Is there any way to add a trend line to a chart with categorical x axis? Or are there custom visuals which can do this? Its a bit strange you dont have the option to do this in power bi 

 

Thanks

1 ACCEPTED SOLUTION

Hi, @yaman123 

You need use  line and clustered column chart visual to replace the clustered column chart visual.

So that you can put this measure to line value.

 10.png

Best Regards,
Community Support Team _ Eason
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

9 REPLIES 9
Gabriel_Walkman
Continued Contributor
Continued Contributor

Hi!

 

Could you describe your scenario a bit. Like what kind of categories would you have on your x axis? Its a bit strange to want this.

I have a date column in the x axis and count of a column in the y axis. When i change the x axis to continuous, the graph comes out in a weird format and with thin lines and spaced out. Whereas if its categorical, it shows correctly. I need to show a trendline but the only way to do this is when you change the x axis to continuous

Hi,  @yaman123 

This is by design . If the x axis is type Categorical, you won't get the trend option.As a workaround, you can  create trend lines through dax.

Linear regression = 
VAR Known =
    FILTER (
        SELECTCOLUMNS (
            ALLSELECTED ( 'Table'[Date] ),
            "Known[X]", 'Table'[Date],
            "Known[Y]", CALCULATE ( SUM ( 'Table'[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] * 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 ( 'Table'[Date] ), Intercept + Slope * 'Table'[Date] )

Please refer to these related threads for more details.

Simple linear regression in DAX 

analytics-with-power-bi-desktop-for-trend-analysis/ 

DAX-to-create-a-Trend-line 

 

Best Regards,
Community Support Team _ Eason

Hi, 

 

Where do i put this measure in the clustered column chart visual? How would i show a trend line on the chart? 

Hi, @yaman123 

You need use  line and clustered column chart visual to replace the clustered column chart visual.

So that you can put this measure to line value.

 10.png

Best Regards,
Community Support Team _ Eason
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @v-easonf-msft 

 

Does this work with Count? My Y Axis is a count of a column. When i add the measure to the chart, it doesnt show as a trend line. Attached image Capture.PNG

Hi,  @yaman123 

Could you please tell me whether your problem has been solved?

For now, there is no content of description in the thread. If you still need help, please share more details to us.

 

Best Regards,
Community Support Team _ Eason

Hi,  @yaman123 

Yes, the same applies. You need to replace the sum(sales) with count(id) in the original formula . If the problem is still not resolved, please share your sample pbix file for further research.

Best Regards,
Community Support Team _ Eason

 

When you say replace sum(sales) with count(id), do you mean in the estimated sales calculation? Instead of Sum, what if Actual Sales = SUM(Sales[Amount]) was Actual Sales = AVERAGE(Sales[Amount])?

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.