How to Get Month and Year from Date in Tableau
Working with dates in Tableau is fundamental, but getting them into the right format, like isolating the month and year, can sometimes feel tricky. This guide will walk you through several methods to extract month and year from a date field, starting with the simple drag-and-drop approach and moving on to more flexible custom calculations for specific needs.
The Easiest Way: Using Tableau's Built-in Date Parts
Tableau’s default behavior for dates is incredibly intuitive once you understand a key concept: the difference between Date Parts (discrete, blue pills) and Date Values (continuous, green pills).
- Date Parts (Discreet) are individual components of a date. Think of them as labels. The "Month" part of "April 15, 2024" is simply "April." It doesn't know about the year. When you use a discrete date part, Tableau creates headers for each part, like an "April" column showing data for all Aprils in your dataset, regardless of the year. This is great for comparing seasonality (e.g., how do all Januaries compare?).
- Date Values (Continuous) represent a specific point on a timeline. The "Month" value of "April 15, 2024" is "April 2024." This creates a continuous axis, which is perfect for showing trends over time in a line chart.
For most simple visualizations, leveraging these built-in options is the fastest way to get what you need.
Step-by-Step with the Drag-and-Drop Method
Let's use the Sample - Superstore dataset that comes with Tableau. We want to see Sales by Month and Year.
- Drag your date field (in this case,
Order Date) onto the Columns shelf. By default, Tableau usually shows this asYEAR(Order Date), treating the year as a discrete Date Part. - Drag your measure field (like
Sales) onto the Rows shelf. You'll now see a simple bar chart with total sales for each year. - To get the month, right-click the blue
YEAR(Order Date)pill on the Columns shelf. A context menu will appear.
This menu is the key. It’s divided into two sections:
To get the standalone month name (Discrete Date Part):
Notice the first section of the menu has options like Year, Quarter, Month, and Day. These are the discrete Date Parts.
If you select Month from this list, your axis will now show month names like "January," "February," etc. The bars will show the sum of sales for each month across all years in the dataset. This is perfect for seeing which month is consistently the strongest or weakest for sales.
To get a continuous month and year on an axis (Continuous Date Value):
Now, look at the second section of the menu. You'll see options like "Month" but they look different, often showing an example like "May 2015". These are the continuous Date Values.
If you right-click your YEAR(Order Date) pill again and select the second Month option (the continuous one), two things will happen:
- Your pill will turn green. Green pills in Tableau signify a continuous field.
- Your view will change into a line chart running from the earliest month-year in your data to the latest, creating a proper timeline.
This is often what people are looking for when analyzing trends over time. With just a few clicks, you have a visualization showing monthly sales performance in chronological order.
Creating Calculations for Month and Year
While the drag-and-drop method is great for building visuals, you'll often need to isolate the month or year for use in filters, parameters, or more complex calculations. That’s where calculated fields come in.
Extracting Month and Year as Numbers with DATEPART()
The DATEPART() function returns a specific part of your date as an integer. This is the calculated field equivalent of the discrete blue pills.
DATEPART('year', [Your Date Field])will return the year as a number (e.g., 2024).DATEPART('month', [Your Date Field])will return the month as a number (1 for January, 2 for February, etc.).
How to create a calculated field:
- Click the dropdown arrow in the Data pane and select Create Calculated Field.
- Give your calculation a name, like "Order Month Number."
- Enter the formula:
DATEPART('month', [Order Date])Now you have a new field in your data pane called "Order Month Number" that you can use anywhere. For example, you could create another calculation to group sales by semester:
IF [Order Month Number] <= 6 THEN "First Half" ELSE "Second Half" ENDGetting Month Names with DATENAME()
A month number is useful, but business reports usually require month names like "January". The DATENAME() function is built for this, returning the name of the date part as a string.
To get the full month name, create a new calculated field named "Month Name" with this formula:
DATENAME('month', [Order Date])This will output "January", "February", and so on. This is incredibly useful for creating clean filter controls or readable labels in your dashboards.
Combining Month and Year for Trend Analysis
Sometimes you need a specific formatted label like "January 2024" that you can use in text tables or titles. This requires combining the month name and the year into a single string.
Creating a Custom 'Month-Year' Label
Let's create a custom field that displays dates as, for example, "April 2024".
- Create a new calculated field, perhaps named "Month Year."
- Use the following formula to combine the month name and the year:
DATENAME('month', [Order Date]) + " " + STR(YEAR([Order Date]))Here's what each part does:
DATENAME('month', [Order Date]): Gets the full name of the month ("April").+ " " +: Adds a space for readability.STR(YEAR([Order Date])): TheYEAR()function gets the year as a number (2024), and theSTR()function converts that number into a string ("2024") so you can combine it with the month name. You can't combine strings and integers directly.
Pro Tip: Fixing the Sort Order for Custom Date Strings
A common problem immediately arises: if you use your new "Month Year" string field in a table or as a filter, Tableau will sort it alphabetically: April 2024, August 2023, December 2024, etc. This is NOT good for timelines!
Here’s how to force Tableau to sort your custom date string chronologically:
- First, create a true date field that represents the start of each month. We'll use the
DATETRUNCfunction. Create a calculated field named "Month Start Date" with this formula:
DATETRUNC('month', [Order Date])DATETRUNC('month', [Order Date]) will result in a true date of April 1, 2024. Every date in April will be truncated to the first of the month.
- Now, find your custom "Month Year" string field in the Data pane.
- Right-click it and navigate to Default Properties > Sort...
- In the Sort dialog box, change the "Sort by" option from "Data Source Order" to "Field".
- From the "Field Name" dropdown, select your newly created Month Start Date field. Leave the aggregation as Minimum or Maximum, it doesn't matter since all dates for a given month will have the same start date.
- Click OK.
Now, whenever you use your "Month Year" string field, Tableau will use the underlying "Month Start Date" field to sort it correctly. January 2023 will appear before February 2023, just as it should.
Creating Advanced Custom Formats
What if your organization requires a very specific format like "MM-YYYY" (e.g., 04-2024) or "Mon-YY" (e.g., Apr-24)? You can 'build' these formats by combining string and date functions creatively.
Example 1: Format as "MM-YYYY" (e.g., "04-2024")
Getting the leading zero for single-digit months is the trickiest part. Here’s a clever formula:
RIGHT("0" + STR(DATEPART('month', [Order Date])), 2) + "-" + STR(DATEPART('year', [Order Date]))The magic is in RIGHT("0" + STR(...), 2):
- If the month is 9, it becomes
STR(9)→ "9", then"0" + "9"→ "09".RIGHT("09", 2)returns "09". - If the month is 12, it becomes
STR(12)→ "12", then"0" + "12"→ "012".RIGHT("012", 2)returns "12".
This method elegantly handles both single and double-digit months.
Example 2: Format as "Mon-YY" (e.g., "Apr-24")
For an abbreviated format, we can crop the strings from DATENAME and YEAR.
LEFT(DATENAME('month', [Order Date]), 3) + "-" + RIGHT(STR(YEAR([Order Date])), 2)LEFT(DATENAME('month', ...), 3): Takes the first three letters of the month name (e.g., "Apr").RIGHT(STR(YEAR(...)), 2): Takes the last two characters of the year (e.g., "24").
Final Thoughts
Mastering dates in Tableau moves you from a beginner to a confident analyst. Whether you're using the simple right-click menu for quick charts or crafting custom calculations with functions like DATENAME and DATEPART, you now have a full toolkit for manipulating months and years to tell a clear data story.
While building sophisticated reports in programs like Tableau is a powerful skill, sometimes you just need an answer fast, without having to stop and write a calculated field. We've simplified this process with Graphed. After easily connecting your data sources, you can ask questions in plain English like, "What were our monthly sales in 2023 as a bar chart?" and get a live, interactive visualization instantly. We designed it to handle the tedious data wrangling so you can get directly to the insight, whether you're a seasoned analyst or someone who just needs a quick answer.
Related Articles
How to Enable Data Analysis in Excel
Enable Excel's hidden data analysis tools with our step-by-step guide. Uncover trends, make forecasts, and turn raw numbers into actionable insights today!
What SEO Tools Work with Google Analytics?
Discover which SEO tools integrate seamlessly with Google Analytics to provide a comprehensive view of your site's performance. Optimize your SEO strategy now!
Looker Studio vs Metabase: Which BI Tool Actually Fits Your Team?
Looker Studio and Metabase both help you turn raw data into dashboards, but they take completely different approaches. This guide breaks down where each tool fits, what they are good at, and which one matches your actual workflow.