How to Aggregate DateTime Differences
Scenario
Transactional product tables often have multiple date columns that need to be compared and aggregated. Many reports will require the average time between dates to track the overall progress, for example a customer transaction table might have the following date fields:
· “purchase_date”
· “ship_date”
· “delivery_date"
Understanding the time between DateTime columns and aggregating those times to a total is a common way to track the progress, but by design, DateTime differences will result in a DateTime filed and not a calculatable integer.
Beast Mode Sample Code:
This code works on the DomoStats Activity Log dataset and is subtracting the Batch date from the Event date.
Beast Mode Sample Code Results:
The results are displayed in Hour:Minute:Second:Millisecond, but the column is still a DateTime field and not a calculatable integer.
Solution
- Convert the DateTime columns to seconds. This changes the datatype to integer so it can then be calculated.
- Perform the calculation on the integer.
- Convert the seconds integer back to a DateTime datatype.
Beast Mode Example:
Here are a few Beast Mode code examples that use the above steps. These Beast Mode codes work with the DomoStats Activity log dataset.
Total Event Time in Seconds:
First, convert the DateTime to seconds and sum the results.
I am using the UNIX_TIMESTAMP and FROM_UNIX functions for their speed and simplicity. The TIME_TO_SEC and SEC_TO_TIME are popular options as well, but might need additional functions to get the format as needed.
SUM(UNIX_TIMESTAMP(`Event_Time`))
Time difference in Seconds:
Repeat the steps for each DateTime field and preform the calculation.
The "Time Difference in Seconds" is the Numerator for the "Average Time per Event" calculation.
SUM(UNIX_TIMESTAMP(`_BATCH_LAST_RUN_`)) - SUM(UNIX_TIMESTAMP(`Event_Time`))
Total Events:
The "Total Events" count is the Denominator for the "Average Time per Event" calculation.
COUNT(`Event_Time`)
Average Time per Event:
This function is set to format the time in Day:Hour:Minute:Second.
FROM_UNIXTIME((SUM(UNIX_TIMESTAMP(`_BATCH_LAST_RUN_`)) - SUM(UNIX_TIMESTAMP(`Event_Time`))) / COUNT(`Event_Time`), '%d:%h:%i:%s')
Outcome & Observations
Using this method you can now calculate the difference between DateTime columns and aggregate those times up to a total.
Comments
-
@JedP - this is great. Thanks for sharing.
@AnnaYardley - this seems like the kind of post that might make a better blog post then a Dojo topic as it really isn't a question, just a well thought out solution to a common problem.
“There is a superhero in all of us, we just need the courage to put on the cape.” -Superman3
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 8 Federated
- 2.9K Transform
- 100 SQL DataFlows
- 616 Datasets
- 2.2K Magic ETL
- 3.8K Visualize
- 2.5K Charting
- 731 Beast Mode
- 55 App Studio
- 40 Variables
- 682 Automate
- 175 Apps
- 451 APIs & Domo Developer
- 46 Workflows
- 10 DomoAI
- 35 Predict
- 14 Jupyter Workspaces
- 21 R & Python Tiles
- 394 Distribute
- 113 Domo Everywhere
- 275 Scheduled Reports
- 6 Software Integrations
- 122 Manage
- 119 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 10 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 107 Community Announcements
- 4.8K Archive