KNOWLEDGE BASE

How to Create Trend Line for Discrete Date Axis


Published: 08 Dec 2023
Last Modified Date: 08 Dec 2023

Question

How to create a trend line for a discrete Date axis.

It is possible to use a trend line when using the continuous date in the x-axis.

User-added image

But, if you are using a discrete date field in the x-axis, the Trend Line function is greyed out.

User-added image
 

Environment

  • Tableau Desktop

Answer

Use a table calculation to manually create a trend line and use the dual-axis function to merge with the original graph.

Follow along with the sample packaged workbook found in the Attachments section of this article to review the steps below.

1. Create a slope calculation for the trend line with the date. [Sales] is the y-axis
Field name: 1. slope
Calculation: 
WINDOW_COVAR(INDEX(), SUM([Sales])) / WINDOW_VAR(INDEX())

2. Create a slope calculation for the trend line with the date.
Field name: 2. Intercept
Calculation: 
WINDOW_AVG(SUM([Sales])) - [1. slope] * WINDOW_AVG(INDEX())

3. Create a trend line calculation.
Field name: 3. trend line
Calculation: 
INDEX()*[1. slope]+[2. Intercept]

4. Add the [3. trend line] in the [Rows] shelf and set as the dual axis. Synchronize the trend line axis.

User-added image


 
Did this article resolve the issue?