• 28 augustus 2023
• 0

In the world of data visualization and analysis, Tableau has emerged as a powerful tool that empowers users to extract insights from complex datasets. One common scenario involves working with datetime fields and calculating their differences. In this blog post, we’ll dive into a technique to calculate the difference between two datetime fields in an accumulated manner, providing a comprehensive understanding of the period in terms of days, hours, and minutes.

## Understanding the Need for Accumulated Date Part Differences

Imagine you’re analyzing a dataset containing records of customer service response times. You have two datetime fields: “Datetime From” and “Datetime To”. While calculating the difference in hours or days might provide some insight, it doesn’t capture the entire story. What if the response time crosses over a day boundary or even multiple days? To address this, we can calculate the accumulated difference in various date parts.

## Step-by-Step Guide

Let’s walk through the process of calculating the accumulated difference between two datetime fields, keeping track of days, hours, and minutes, using Tableau.

### 1. Create a Calculated Field

Start by creating a calculated field that calculates the difference between the two datetime fields. To do this, right-click on the “Data” pane and select “Create Calculated Field.” For this calculation, we will only calculate the total minute difference between the two datetime fields;

Let’s call this field “Minute Difference

### 2. Define the Calculation

In the calculated field formula, you’ll use the DATEDIFF function to calculate the difference between the two datetime fields. The DATEDIFF function takes three arguments: the date part you want to calculate, the start datetime, and the end datetime. Dial the following calculation in;

`DATEDIFF('minute', [Datetime From], [Datetime To])`

### 3. Calculate Accumulated Values

Now, let’s create calculated fields to calculate the accumulated difference in days, hours, and minutes. These fields will use the WINDOW_SUM function to accumulate the differences over rows. We can call these fields “ACC Days”, “ACC Hours” and “ACC Minutes”

Days:

```INT([Minute Difference] / 1440)```
``````

Hours:

```INT(([Minute Difference] - [ACC Days] * 1440) / 60)```
``````

Minutes:

`[Minute difference] - [ACC Days] * 1440 - [ACC Hours] * 60`

### 4. Display the Results

Drag the calculated fields for accumulated days, hours, and minutes onto your visualization canvas. You can choose to display these values on a table, chart, or dashboard, depending on your preference.

### 5. Formatting the Output

To present the results in a human-readable format (“X days Y hours Z minutes”), you can use additional calculated fields. Here’s an example formula for creating a formatted text field:

`STR([ACC Days]) + ' days ' + STR([ACC Hours]) + ' hours ' + STR([ACC Minutes]) + ' minutes'`

When everything is put together, our final calculation should look something like this:

In my example, I am using a very basic table with the different datetime stamps per 20 different IDs.

## Final Remarks

By calculating the accumulated difference between two datetime fields in terms of days, hours, and minutes, you gain a more insightful perspective on periods that cross date boundaries. This technique can provide a deeper understanding of data, particularly in scenarios where time intervals are critical.

Thank you for reading this blog. Also check out our other blogs page to view more blogs on Tableau, Alteryx, and Snowflake here.

Work together with one of our consultants and maximize the effects of your data.