How to Extract Time from DateTime in Tableau

Cody Schneider8 min read

Working with dates and times in Tableau is usually straightforward, but sometimes you need to isolate just the time portion of a datetime field for analysis. Whether you want to see peak sales hours, analyze website traffic patterns by time of day, or track support ticket volume, pulling the time out is a critical first step. This article will walk you through several methods to extract time from a datetime value in Tableau, from the simple right-click method to more flexible calculated fields.

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

Why Extract Time from a DateTime Field?

Before jumping into the “how,” let’s briefly touch on the “why.” A full datetime stamp like “January 15, 2024, 09:32:15 AM” is great for pinpointing exact moments, but it’s not ideal for spotting trends that happen at the same time every day. By isolating the time (e.g., 09:32:15), you can aggregate data across many days to answer questions like:

  • What are our busiest hours for online orders?
  • When do we receive the most customer service calls?
  • Is website engagement higher in the morning or the afternoon?
  • Are our freight shipments consistently leaving on time each day?

Answering these questions requires you to look at the time of day, independent of the specific date. Let's explore how to do that in Tableau.

Method 1: The Quick and Easy Way with Date Parts

The simplest method to start analyzing your data by a time component is using Tableau's built-in functionality. This approach is perfect for quick exploration and doesn't require writing any calculations.

Imagine you have a field named [Order Date] that is a DateTime data type. Here’s how you can use it to build a view by hour:

  1. Drag your DateTime field (e.g., [Order Date]) from the Data pane onto the Columns or Rows shelf.
  2. By default, Tableau will likely aggregate it to the YEAR level (you’ll see a blue pill that says YEAR([Order Date])).
  3. Right-click the pill. You will see a menu with different date aggregation options (Year, Quarter, Month, Day).
  4. Hover over "More" at the bottom of the first section, and a second menu will appear.
  5. In this second menu, select "Hour".

The pill on your shelf will now read HOUR([Order Date]), and your view will update to show data aggregated for each hour of the day (0-23). You can do the same for Minute or Second.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

Understanding Discrete vs. Continuous Time

When you right-click the date pill, you'll notice two sets of options for Hour, Minute, and Second. These correspond to Discrete (blue pills) and Continuous (green pills) fields.

  • Discrete (the first set of options): Choosing Hour from the first section (e.g., "Hour: May 8, 2015, 8:00:00 AM") treats each hour of each day as a unique header or label. This will give you a very long axis if your data spans many days. Choosing Hour from the "More" menu treats "8:00 AM" as a category, lumping all data from 8:00 AM together regardless of the day it occurred. This is usually what you want for time-of-day analysis.
  • Continuous (the second set of options): Choosing the continuous Hour option plots the time on a continuous axis. This is useful for creating line charts that show trends over the hours of a single day but may blend multiple days together in an undesirable way if not filtered properly.

For most "time of day" analysis, you’ll want to use the discrete Hour option to group all 9 AM data together, all 10 AM data together, and so on.

Method 2: Using Calculated Fields for Full Control

While the right-click method is fast, calculated fields offer far more power and flexibility. They allow you to define exactly how you want your time value represented - as a number, a formatted string, or even as a proper time value that you can use on a continuous axis.

Extracting Time Components with DATEPART

The DATEPART function is a fundamental tool for this job. It isolates a specific part of a date - like the year, month, or in our case, the hour - and returns it as an integer.

The syntax is: DATEPART('date_part', [datetime_field])

Let's create a few calculated fields:

To get the hour (as a number 0-23):

  1. Go to Analysis > Create Calculated Field.
  2. Name it "Hour of Day".
  3. Enter the formula:

DATEPART('hour', [Order Date])

To get the minute (0-59):

  1. Create a new calculated field named "Minute of Hour".
  2. Enter the formula:

DATEPART('minute', [Order Date])

Once you create these, you'll have new numeric measures in your Data pane that you can convert to dimensions and use to build visualizations, just like with the right-click method. The main benefit is that these fields are reusable across multiple worksheets.

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

Creating a Formatted Time String (HH:MM:SS)

Sometimes you need to display the time in a conventional format, such as "09:30:15". This is especially useful for tables and labels. You can achieve this by combining DATEPART with the STR (string) function to build the text yourself.

A simple attempt would look like this:

STR(DATEPART('hour', [Order Date])) + ":" + STR(DATEPART('minute', [Order Date])) + ":" + STR(DATEPART('second', [Order Date]))

However, this formula has a small problem: it won't include leading zeros. A time like 9:05:03 AM would appear as "9:5:3". To fix this, we can use the RIGHT function to pad single-digit numbers with a leading zero.

Here’s the improved formula for a clean, properly formatted time string:

RIGHT('0' + STR(DATEPART('hour', [Order Date])), 2) + ":" + RIGHT('0' + STR(DATEPART('minute', [Order Date])), 2) + ":" + RIGHT('0' + STR(DATEPART('second', [Order Date])), 2)

How it works: The formula prepends a '0' to each number and then takes the rightmost two characters. For '9', '0' + '9' becomes "09", and RIGHT("09", 2) is "09". For '10', '0' + '10' becomes "010", and RIGHT("010", 2) is "10".

The downside? This result is a string. You can’t easily plot it on a continuous axis or calculate averages. It's best used for display purposes.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

Extracting a Pure Time Value for Continuous Analysis

What if you want a value that represents only time, which you can format and use on a continuous axis? This is where a clever trick involving how Tableau stores dates and times comes in handy.

Tableau, like Excel, stores datetimes as decimal numbers. The integer part represents the date (the number of days since a starting point), and the decimal/fractional part represents the time of day (e.g., 0.5 is noon, 0.75 is 6 PM).

To isolate the time, you can subtract the date portion from the full datetime value.

  1. Create a calculated field and name it "Time of Day".
  2. Enter this formula:

FLOAT([Order Date]) - INT([Order Date])

How it works:

  • FLOAT([Order Date]) converts the datetime into its numerical representation (e.g., 45291.5).
  • INT([Order Date]) truncates the value, keeping only the integer part (e.g., 45291).
  • Subtracting the two leaves only the fractional part (e.g., 0.5), which is your time!

Now, this field will just look like a decimal number. A crucial final step is to format it:

  1. Find your new [Time of Day] calculated field in the Data pane.
  2. Right-click it and select Default Properties > Number Format...
  3. Choose Custom and in the format box, enter hh:mm:ss.

Now you have a truly reusable, purely time-based field. You can place it on a continuous axis to create scatter plots or line charts that show events happening throughout the day, and it will sort correctly from morning to night.

Putting It All Together: A Practical Example

Let’s visualize total sales by the hour of the day using the techniques we've learned.

  1. Create the Hour Calculation: First, create a calculated field named "Hour of Sale" with the formula: DATEPART('hour', [Order Date])
  2. Prepare your calculated field: Drag your new [Hour of Sale] field from the Measures section up to the Dimensions section in the Data pane. Tableau initially treats it as a number to be summed, but we want to use it as a category.
  3. Build the View:
  4. Refine the Chart: Sort the hours if needed, add color based on sales amount, and give your chart a descriptive title like "Hourly Sales Performance."

You now have a bar chart clearly showing which hours of the day generate the most revenue, letting you make data-driven decisions about staffing, ad spend timing, or promotional offers.

Final Thoughts

You now have a complete toolkit for extracting time from datetime fields in Tableau. Whether you need a quick look with the right-click menu or a robust, reusable format via calculated fields, you can now slice your data by time to uncover powerful, time-of-day trends that were previously hidden.

Often, the reason we perform these transformations is to quickly answer a business question. Instead of getting bogged down in writing calculations for every tool you use, we built Graphed to simplify the entire reporting process. You just ask your question in plain English - like "what were my sales by hour of day last month?" - and Graphed connects to your live data sources to instantly generate the report you need, leaving you more time to act on insights instead of just finding them.

Related Articles