KNOWLEDGE BASE

Dynamically Changing the Date Level


Published: 17 Mar 2017
Last Modified Date: 27 Apr 2017

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

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 dialog box, do the following:
    • Give the custom date a name. For this example, enter Order Date - Year.
    • For Detail, select Years. Leave the Date Value selected.
    • Click OK.
  4. Repeat steps 2 -3 to create custom dates for both month and day.

Step 2: Create a parameter to use as the date level selector

  1. Right-click the Data window, and then select Create Parameter.
  2. In the Create Parameter dialog 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.

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 - Year] 
    WHEN 'Month' THEN [Order Date - Month] 
    WHEN 'Day' THEN [Order Date - Day] 
    END
Step 4: Create the view
  1. In the Data window, right-click the Date Selector field, and then select Convert to Discrete.
  2. Drag Date Selector to Columns.
  3. On the Columns shelf, right-click the Date Selector field, and then select Exact Date and Discrete.
  4. Drag Sales to Rows.
Did this article resolve the issue?