Problem: Tableau makes selecting and changing the aggregation of a date dimension very easy while you are building a view. However, unless an end user is viewing an individual sheet in Tableau Desktop, they can’t easily pivot the date part between day, week, month, quarter, and/or year on their own.
Solution: Create a parameter that includes each date granularity option you want your end users to have access to (i.e. Day, Week, etc.), and create a calculated field that will act as your aggregation-changeable date.
In many cases, it makes sense to change the granularity of a line graph over time. The Sample -Superstore dataset, for example, includes four years of daily data. If you are trying to view your sales over time and you set the date aggregation to year, you are provided a 10,000 foot view of your sales trend, but no seasonal insight. On the other hand, set the date granularity to continuous day, and while outliers stand out, it is nearly impossible to differentiate between individual days because you are looking at more than a thousand marks at the same time.
As you can see, viewing your sales over time at different levels of date granularity will tell very different stories. Why permanently choose the date aggregation of your view when you can allow your end users to choose for themselves?
Related video tutorial:
Premier Tableau eLearning from Playfair Data TV
How to change date aggregation using parameters
Step 1: Create a string-based parameter with each level of date aggregation, as follows:
Note that the values have to be lowercase for this to work properly.
Step 2: Create a calculated field, leveraging the DATETRUNC function to change the date aggregation to the appropriate level based on which parameter option is selected. Here is the logic:
DATETRUNC([Date Granularity parameter from step 1],[Order Date])
Step 3: Instead of Order Date, use your newly created Date Granularity field. For best results, add the date field to the columns shelf by right-clicking and dragging; then choose the first option (Continuous).
Step 4: Ensure you right-click on the Date Granularity parameter and choose “Show Parameter Control” so your end users can choose their level of date aggregation. Your final product will look like this:
Thanks for reading,
This content is excerpted from my book Practical Tableau: 100 Tips, Tutorials, and Strategies from a Tableau Zen Master published by O’Reilly Media Inc., 2018, ISBN: 978-1491977316. Get the book at Amazon.