10 Dec Tableau – Date Calculations
Date calculations in Tableau are used to manipulate and analyze date and time fields. Let us see some examples to understand the concept.
Types of Date Calculations
The following are some common types of date calculations. Consider fields such as Date Field 1, Date Field 2, etc.
Basic Date Calculations
- Date: TODAY() (current date)
- Date Part: YEAR([Date Field]), MONTH([Date Field]), DAY([Date Field]) (extract year, month, day)
- Date Difference: DATEDIFF(‘day’, [Date Field1], [Date Field2]) (calculate difference)
- Date Add: DATEADD(‘day’, 30, [Date Field]) (add days, months, years)
Date Manipulation Calculations
- Date Trunc: TRUNC([Date Field], ‘month’) (truncate to month, quarter, year)
- Date Floor: FLOOR([Date Field], ‘quarter’) (round down to quarter, year)
- Date Ceiling: CEILING([Date Field], ‘year’) (round up to year)
- Date Format: DATETOSTRING([Date Field], ‘YYYY-MM-DD’) (format date)
Relative Date Calculations
- Yesterday: TODAY() – 1
- Last Week: TODAY() – 7
- Last Month: TODAY() – 30
- Last Quarter: TODAY() – 90
- Year-to-Date (YTD): TODAY() – (TODAY() – 1) % 365
Period-over-period (PoP) Calculations
- Year-over-Year (YoY): DATEADD(‘year’, -1, [Date Field])
- Quarter-over-Quarter (QoQ): DATEADD(‘quarter’, -1, [Date Field])
- Month-over-Month (MoM): DATEADD(‘month’, -1, [Date Field])
Advanced Date Calculations
- Age: DATEDIFF(‘day’, [Birthdate Field], TODAY())
- Next/Previous Occurrence: DATEADD(‘day’, 1, [Date Field]) or DATEADD(‘day’, -1, [Date Field])
- Date Range: DATEDIFF(‘day’, [Start Date Field], [End Date Field])
Example
We will use the data calculation to find the average days it takes to ship products in all the regions.
Let us create a new sheet and name it Date Calculations in the same Amit_Tableau_Calculations.twb Tableau Workbook. We created the Amit_Tableau_Calculations.twb Tableau workbook in the Numeric Calculations lesson.
Go to the Analysis menu and click on it. After that, click Create Calculated Field:
Click on the arrow to display all the built-in functions. From the dropdown, click Date to display only the date functions:
Set the name of the calculation. We will set it to Date_Difference. From the right, select the DATEDIFF() function. After selecting, on the left, set the formulae i.e., DATEDIFF(‘day’, [Order Date], [Ship Date], ‘Monday’ ). Here:
The DATEDIFF displays the difference between two dates (Order and Ship date here) and the result in the day units. The week of the day is set to Monday. We will get the days it takes to ship products.
The formulae can be seen and there are no error messages. Click OK:
The new field Date_Difference is visible:
Drag the Region to the Rows shelf and the Date_Difference to the Text in the Marks shelf. Through this, we will get the days it takes to ship products in all the regions:
Right-click on the SUM(Date_Difference) in the Marks pane and select AVERAGE to get the average of the date difference (Order and Ship) region-wise. Here, we are changing the aggregation level to average:
Now, the average date difference can be seen. This shows the average days it takes to ship products in all the regions.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments