May 31, 2024

[Tableau] Combining Multiple Date Fields

 

There was a question from my colleague on how to combine multiple date fields into one date field in Tableau.  This scenario is applicable when visualizations with different metrics use different date fields and you want to display only one date filter in the dashboard.


Tableau has this KB article which lists many options to resolve this issue.  However, there's also another way to resolve this by using UNION ALL in a custom query.  Here're the steps: 


1)  Step 1:  Create a custom query with UNION ALL and a [data_type] field based on different dates.

SELECT number, closed_date, created_date, sales, profit,

'Closed Date' as data_type

FROM table

UNION ALL

SELECT number, closed_date, created_date, sales, profit,

'Created Date' as data_type

FROM table


2)  Step 2:  Create a 'Date' formula in Tableau Desktop where the logic is when [data_type] = 'Closed Date', use [closed_date]. If data_type = 'Created Date', use [created_date].


@Date

DATE(CASE [data_type]

When 'Closed Date' then [closed_date]

When 'Created Date' then [created_date]

END)


3)  Step 3:  Create visualizations based on different dates.  For example, create viz1 as a line chart based on Closed Date.  Use the @Date formula as x-axis and filter for [data_type] = 'Closed Date'.  In the same viz1, include @Date in the Filters pane, which will be used as a common date filter among the different visualizations.  

Using the same procedure, create viz2 based on Created Date with [data_type] = 'Created Date'.


4) Step 4:  Create a dashboard with different visualizations.  Display @Date as filter, which now acts as a common date filter among the visualizations using different dates.


No comments:

Post a Comment