KNOWLEDGE BASE

Creating a Reference Line which Appears and Disappears Based on a Condition


Published: 20 Mar 2017
Last Modified Date: 20 Jul 2023

Question

Usually, a reference line will display whether or not there are corresponding values visible in the underlying data. For example, if you have data in your view filtered to December 2012 onward, but a reference line for April 1 2012, the view will look similar to the below: 

User-added image

Environment

Tableau Desktop

Answer

Step 1: Prepare the view

This example uses Superstore-Sample data and can be seen in the attached packaged workbook. 
  1. Right-click and drag Order Date to Columns, then select continuous MONTH(Order Date) from the lower section of the menu. 
  2. Place Profit on Rows. 
  3. Place Region on color.
  4. Right-click MONTH(Order Date) on the view and drag to Filters 
  5. Select Range of Dates
  6. Right-click Order Date on the Filters shelf and select Show Filter

Step 2: Create the Reference Line

Option 1: Create a reference line that only appears when there is data in the view for its date

  1. You can create a calculated field so that the reference line only appears if the view contains data for its date. 
  2. Select Analysis > Create Calculated Field.
  3. Name the field Reference Line April, enter the following calculation, then click OK
    IF WINDOW_MAX(MAX([Order Date])) > DATE("4/1/2012") 
    AND WINDOW_MIN(MIN([Order Date])) < DATE("4/1/2012")
    THEN DATE("4/1/2012") 
    ELSE NULL END
  4. Drag Reference Line April to Detail, then right-click it and select Continuous
  5. Right-click the date axis in your view and select Add Reference Line. Enter the following options, then click OK:
    • Select Line
    • For Value, select Reference Line April
    • For Label, select Value

Adjust the date filter slider - if the range includes 4/1/2012, the reference line will be visible, if the range does not include that date, the reference line will not display. 

Option 2: Reference Line with On/Off Switch

  1. Right-click the Data window and select Create Parameter
  2. Enter the following, then click OK
    • For Name, type Reference Line Control
    • For Data Type, select Boolean
    • For Current value, select True
    • Under Aliases, for True, type On, for False, type Off
  3. Select Analysis > Create Calculated Field
  4. Name the field Parameter Reference Line, enter the following formula, then click OK
    IF [Reference Line Control] Then DATE("4/1/2012") 
    ELSE NULL END
  5. Drag Parameter Reference Line to Detail, right-click the field, and select Exact Date
  6. In the Parameters pane, right-click Reference Line Control, and select Show Parameter Control
  7. Right-click the date axis in your view and select Add Reference Line.
  8. Choose the following options, then click OK
    • Select Line
    • For Value, select Parameter Reference Line
    • For Label, select Value

You can control whether the reference line appears in the view by selecting On or Off in Reference Line Control. 
 

Additional Information

To view these steps in action, see the video below. 
Note: the video has no sound.



Did this article resolve the issue?