KNOWLEDGE BASE

Dynamically Changing the Date Level


Published: 17 Mar 2017
Last modified date: 20 Jul 2023

Question

How to dynamically change the view based on a specified date level. For example, show sales data by year, month or day. 

Environment

Tableau Desktop

Answer

The above steps can be reviewed in the attached workbook dynamically change date level.twbx.
CLICK TO EXPAND STEPS
Step 1: Create custom date fields
Create custom date fields so that you can isolate specific date levels in the existing date field.
  1. In Tableau Desktop, connect to the data source. For this example, connect to the Superstore data source.
  2. In the Data Window, right-click the Order Date field, and then select Create Custom Date.
  3. In the Create Custom Date dialogue box, do the following:
    • For Detail, select Years. Leave the Date Value selected.
    • If desired, give the custom date a name. In this example,keep default as  Order Date - Year.
    • Click OK.
  4. Repeat steps 2 -3 to create custom dates for both month and day.
CLICK TO EXPAND STEPS
Step 2: Create a parameter to use as the date level selector
  1. Select Dimension drop-down, and then select Create Parameter.
  2. In the Create Parameter dialogue box, do the following:
    • Give the parameter a name. For this example, use Date Level Selector.
    • For Data Type, select String.
    • For Allowable values, select List.
    • For List of values, enter Year, Month, and Day, then click OK.
  3. In the Data window, right-click Date Level Selector and choose Show Parameter Control.
CLICK TO EXPAND STEPS
Step 3: Create a calculated field

Create a calculated field that displays the data based on the date level specified by the date level selector parameter.

  1. Select Analysis > Create Calculated Field.
  2. Name the field Date Selector, enter the following calculation, then click OK:
    CASE [Date Level Selector] 
    WHEN 'Year' THEN [Order Date - Years] 
    WHEN 'Month' THEN [Order Date - Months] 
    WHEN 'Day' THEN [Order Date - Days] 
    END
CLICK TO EXPAND STEPS
Step 4: Create the view
  1. Drag Date Selector to Columns.
  2. On the Columns shelf, right-click the Date Selector field, and then select Exact Date and Discrete.
  3. Drag Sales to Rows.

Additional information

To view the steps showed in the below video, please expand the above section.
Note: the video has no sound.
 
Did this article resolve the issue?