What is DATENAME function in Tableau
The DATENAME function in Tableau is used to extract a specific part of a date or datetime value, such as the year, month, or day. It returns a string value that represents the name of the specified date part. It will helpful if you want to extract month name from date filed to show the trend in Bar chart, Combined chart and other type of chart.
The syntax for using the DATENAME function in Tableau is given below
DATENAME(datepart, date)
- datepart: It specifies the part of the date that you want to return the name for. This can be one of the following: ‘year’, ‘quarter’, ‘month’, ‘week’, ‘weekday’, ‘day’, ‘hour’, ‘minute’, or ‘second’.
- date: it is the date or datetime field for which you want to retrieve the name of the specified datepart.
Below are the 5 example which explained the DATENAME in Tableau , these 5 examples will make you to understand and usage of this function in tableau.
Example #1: Extract the year from a date:
Let take easy example with specific date to understand how output will look like as shown below from specific date to extract year
DATENAME('year', #2022-02-15#)
This returns the string ‘2022′.
Now lets take example from Dataset filed from Ship Date, and try to get Years from date
DATENAME('year', [Ship Date])
The output will look like as show below image
Example #2: Extract the month from a date:
Let take first simple example with specific date to understand how output will look like as shown below from specific date to extract month
DATENAME('month', #2022-02-15#)
This returns the string ‘February‘.
Now lets take example from Dataset filed from Ship Date, and try to get month from Ship Date
DATENAME('month', [Ship Date])
The output will look like as show below image
Lets take another example if you want to represent the Bar chart with month name, then you can use DATENAME in Tableau to show trend month wise as shown below image.
Example #3: Extract the weekday from a date:
Let take simple example with specific date to understand how output will look like as shown below from specific date to extract weekday
DATENAME('weekday', #2022-02-15#)
This returns the string ‘Monday‘.
Now lets take example from Dataset filed from Ship Date, and try to get Weekdays from Ship Date
DATENAME('weekday', [Ship Date])
The output will look like as show below image
Example #4: Extract the quarter from a date:
Let take one line example with specific date to understand how output will look like as shown below from specific date to extract quarter
DATENAME('quarter', #2022-02-15#)
This returns the string ‘Q1’.
Now lets take example from Dataset filed from Ship Date, and try to get quarter from Ship Date
DATENAME('quarter', [Ship Date])
The output will look like as show below image
Example #5: Extract the hour from a datetime:
Now below example with specific date to understand how output will look like as shown below from specific date to extracted hours.
DATENAME('hour', #2022-02-15 10:30:00#)
This returns the string ’10’.
In each example, the DATENAME() function extracts a specific part of a date or datetime value, and returns a string value that represents the name of the specified date part. The resulting value can be used in calculations or displayed in a visualization.