Last Modified Date: 29 Nov 2023
Environment
Tableau DesktopAnswer
Option 1
Create a parameter that allows the user to select an anchor date for a fixed relative time range, for example the last 3 months from the anchor date.
Step 1: Create a Parameter
- In Tableau Desktop, connect to Superstore sample data.
- Right-click in the Data window and then select Create Parameter.
- In the Create Parameter dialog box, do the following, and then click OK:
- Name the parameter. In the example workbook, the parameter is named Choose Anchor Date.
- For Data type, select Date.
- For Allowable values, select All.
- Right-click the parameter and then select Show Parameter Control.
Step 2: Create a Calculated Field
- Select Analysis > Create Calculated Field.
- In the Calculated Field dialog box that opens, do the following, and then click OK:
- Name the calculated field. In the example workbook, the calculated field is named Relative Date Filter with Anchor.
- In the formula field, create a calculated field similar to the following:
DATEDIFF('month',[Order Date],[Choose Anchor Date]) <= 2
AND DATEDIFF('month',[Order Date],[Choose Anchor Date]) >= 0
- Drag the newly created calculated field from the Dimensions pane to the Filters shelf.
- In the Filter dialog box that opens, select True, and then click OK.
Step 3: Create the View
- Drag Order Date to Rows.
- Right-click Order Date on Rows, and then select Month.
- Right-click Order Date on Rows, and then select Discrete.
- Double-click Sales.
- Use the Date Control parameter control to select an anchor date.
Option 2
Create a parameter that allows the user to select an anchor date for a year to anchor date time range, for example the start of the year to the chosen anchor date.
Step 1: Create a Parameter
- In Tableau Desktop, connect to Superstore sample data.
- Right-click in the Data window and then select Create Parameter.
- In the Create Parameter dialog box, do the following, and then click OK:
- Name the parameter. In the example workbook, the parameter is named Choose Anchor Date.
- For Data type, select Date.
- For Allowable values, select All.
- Right-click the parameter and then select Show Parameter Control.
Step 2: Create a Calculated Field
- Select Analysis > Create Calculated Field.
- In the Calculated Field dialog box that opens, do the following, and then click OK:
- Name the calculated field. In the example workbook, the calculated field is named Year to Anchor Date Filter.
- In the formula field, create a calculated field similar to the following:
DATEDIFF('year',[Order Date],[Choose Anchor Date]) = 0
AND [Order Date] <= [Choose Anchor Date]
- Drag the newly created calculated field from the Dimensions pane to the Filters shelf.
- In the Filter dialog box that opens, select True, and then click OK.
Step 3: Create the View
- Drag Order Date to Rows.
- Right-click Order Date on Rows, and then select Month.
- Right-click Order Date on Rows, and then select Discrete.
- Double-click Sales.
- Use the Date Control parameter control to select an anchor date.
Additional Information
Option 1 will return the exact same results as setting a static anchor date in the Filter dialog. If there is data for dates after the chosen anchor date, this option will includes dates for the entire month of the anchor date. For example, if March 15 is chosen, the view will show dates between January 1 and March 31.Option 2 will end the date range at the chosen anchor date. For eample, if March 15 is chosen, the view will show dates between January 1 and March 15.
For added flexibility, additional parameters can be created to define the date part, such as 'month' or 'year', and the number of date parts back the filter goes. For example, the calculation in option 1 might become:
DATEDIFF([Date Part Parameter],[Order Date],[Choose Anchor Date]) <= [# of Date Parts Parameter] - 1
AND DATEDIFF([Date Part Parameter],[Order Date],[Choose Anchor Date]) >= 0
For a dynamic anchor date, such as last month, the [Choose Anchor Date] parameter can be replaced with a calculated field that defines a dynamic date. For example, a calculation that always returns the first day of last month may look like:
DATETRUNC('month', DATEADD('month', -1, TODAY() ))
To voice your support for the inclusion of this enhancement in a future product release, add your vote to the following Community Ideas:
Allow dashboard consumers to choose Relative Date Anchor
Relative date filter anchor
Thank you for providing your feedback on the effectiveness of the article.
Open new Case
Continue Searching
Knowledge Base
Community
Product Help
Training and Tutorials