How to Aggregate DateTime Differences
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:
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.
- 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.
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`))
The "Total Events" count is the Denominator for the "Average Time per Event" calculation.
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.
@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.” -Superman
- 7.7K All Categories
- 3 Connect
- 919 Connectors
- 244 Workbench
- 477 Transform
- 1.8K Magic ETL
- 60 SQL DataFlows
- 446 Datasets
- 37 Visualize
- 198 Beast Mode
- 2K Charting
- 8 Variables
- 18 Cards, Dashboards, Stories
- 1 Automate
- 348 APIs & Domo Developer
- 82 Apps
- 14 Predict
- 3 Jupyter Workspaces
- 11 R & Python Tiles
- 241 Distribute
- 59 Domo Everywhere
- 241 Scheduled Reports
- 15 Manage
- 36 Governance & Security
- 27 Product Ideas
- 1.1K Ideas Exchange
- Community Forums
- 14 Getting Started
- 1 Community Member Introductions
- 49 Community News
- 18 Event Recordings
- 579 日本支部