KNOWLEDGE BASE

How to Display the Most Recent N Years as Default and Enable Manual Year Switching


Published: 19 Dec 2023
Last Modified Date: 20 Dec 2023

Question

If we want to display the most recent N years when opening a workbook, but to allow users to switch years manually as well, how can it be achieved?

 

Environment

  • Tableau Desktop 

Answer

Follow along in the sample packaged workbook found in the Attachments section to display the most recent N years as default and to enable manual year switching.

1. Create a calculated field and name it Default value.
1
2. Create a parameter as follows and name it Recent N year.
 User-added image
3. Create a parameter as follows and name it How to display?.
Note: Specify the Default value field for Value when workbook opens option.
 User-added image
4. Right-click on each parameter above in the Data pane, and select Show Parameter.
5. Create a calculated field and name it Current year.
YEAR(TODAY())
6. Create a calculated field and name it Year of Order Date.
STR(YEAR([Order Date]))
7. Right-click Year of Order Date in the Data pane and select Create > Set. Name it Year of Order Date Set and Configure it as follows.
 User-added image
8. Create a calculated field and name it Filter.
CASE [How to display?]
WHEN 1 THEN YEAR([Order Date]) > [Current year] - [Recent N year] AND YEAR([Order Date]) <= [Current year]
WHEN 2 THEN [Year of Order Date Set]
END
9. Add YEAR(Order Date) to Columns.
10. Add SUM(Sales) to Rows.
11. Add Filter field to Filters shelf and select TRUE only.
12. Right-click Year of Order Date Set and select Show Set.

To show or hide the Recent N year parameter control and Year of Order Date Set control on a dashboard, do the following additionally.

13. Create a calculated field and name it T/F for param 1.
[How to display?] = 1
14. Create a calculated field and name it T/F for param 2.
[How to display?] = 2
15. Add a new dashboard and add the worksheet created by the above steps.
16. Choose "Show recent N year" in How to display? parameter control.
17. Click Recent N year parameter control on the dashboard and open Layout tab.
18. Check Control visibility using value and select T/F for param 1.
 User-added image
19. Choose "Select years manually" in How to display? parameter control.
20. Click Year of Order Date Set control on the dashboard and open Layout tab.
21. Check Control visibility using value and select T/F for param 2.
 User-added image
Did this article resolve the issue?