Date Functions in Tableau 16
Date Functions in Tableau 16
Date fields are very important in any data set and in the analysis as without
date values like a year, month, week, quarter, etc, we will not have time
references in our data.
Thus, with the help of date functions in Tableau, we can create date fields and
perform complex operations on the date fields.
In the next section, we will see different types of date functions and learn their
use.
Let us learn about the different date functions, their expressions, and use in
Tableau.
1. DATEADD
This function returns a date which is a result of adding a certain interval into
the original date.
Syntax:
1. DATEADD(date_part, interval, date)
Example:
Suppose we want to add 6 months to the present date.
DATEADD(‘month’, 6, #13-02-2020#)
2. DATEDIFF
This function returns a difference between two dates.
Syntax:
1. DATEDIFF(date_part, date1, date2, [start_of_week])
Example:
To see the difference of weeks between two dates.
3. DATENAME
It returns the name of the part that you specify in the date_part parameter.
Syntax:
1. DATENAME(date_part, date, [start_of_week])
Example:
DATENAME(‘year’, #20-04-15#) = “2020”
4. DATEPART
It returns the value entered in the date_part parameter as an integer value.
Syntax:
1. DATEPART(date_part, date, [start_of_week])
Example:
DATEPART(‘year’, #2020-04-15#) = 2020
DATEPART(‘month’, #2020-04-15#) = 4
5. DATETRUNC
This function performs a truncate operation on the date values. That is, it
truncates a date to a specified accuracy or condition given in the expression
and returns a new date.
Syntax:
1. DATETRUNC(date_part, date, [start_of_week])
Example:
Truncating a date at a month level gives the first day of that month. Or
truncating a date at a quarter level will give the first day of that quarter.
6. DAY
This function returns the day of a date as an integer.
Syntax:
1. DAY(date)
Example:
DAY(#2020-02-22#) = 22
7. ISDATE
It is a condition function that returns True if a given value or string is a date.
Syntax:
1. ISDATE(string)
Example:
ISDATE(“February 15, 2020”) = true
8. MAKEDATE
This function creates a date as per the specified year, month and day in the
expression.
Syntax:
1. MAKEDATE(year, month, day)
Example:
MAKEDATE(2020, 2, 15) = #February 15, 2020#
9. MAKEDATETIME
This function creates a date and time value from the given input parameters.
The date part in the input can be of date, string or datetime type but the time
part must only be of datetime type.
Syntax:
1. MAKEDATETIME(date, time)
Example:
MAKEDATETIME(“1997-12-30”, #07:59:00#) = #12/30/1997 7:59:00 AM#
10. MAKETIME
This function creates a date value from given parameters like an hour, minute
and second.
Syntax:
1. MAKETIME(hour, minute, second)
Example:
MAKETIME(10, 55, 40) = #10:55:40#
11. MAX
This function returns the bigger date when two dates are compared. Please
note that the two dates must be of the same type.
Syntax:
1. MAX(expression) or MAX(expr1, expr2)
Example:
MAX(#2020-01-01# ,#2020-03-01#) = 2020-03-01
MAX([ShipDate1], [ShipDate2])
12. MIN
This function returns the smaller date of the two dates compared. This is the
opposite of the MAX function.
Syntax:
1. MIN(expression) or MIN(expr1, expr2)
Example:
MIN(#2020-01-01# ,#2020-03-01#) = 2020-01-01
MIN([ShipDate1], [ShipDate2])
13. MONTH
This function returns the month of the given date in the form of an integer.
Syntax:
1. MONTH(date)
Example:
MONTH(#2020-02-15#) = 2
14. NOW
This function returns the current date and time.
Syntax:
1. NOW( )
Example:
NOW( ) = 2020-02-15 2:10:21 PM
15. TODAY
This function returns the current date.
Syntax:
1. TODAY()
Example:
TODAY( ) = 2020-02-15
16. YEAR
This function returns the year of a given date in the form of an integer.
Syntax:
1. YEAR (date)
Example:
YEAR(#2020-02-15#) = 2020