0) Summary
Use EITHER #1 or #2; If #1 (DATETIME_DIFF) doesn't seem to work, try #2 (CAST & RegEx)
1) DATETIME_DIFF
One way that it can be achieved is by creating Date Time fields and then using the DATETIME_DIFF
function to find the difference in SECOND
after which the type can be set to Duration (sec.)
; it can be achieved in a single Calculated Field as shown below where the Date used is 01 Jan 1970 (feel free to change the date as required, though ensure that both line 2 and line 3 below use the same date):
1.1) Calculated Field (Formula)
DATETIME_DIFF(
PARSE_DATETIME("%F%T", CONCAT("1970-01-01 ", DELIVERY_HOUR)),
PARSE_DATETIME("%F%T", CONCAT("1970-01-01 ", ACCEPTED_DRIVER_HOUR)),
SECOND)
1.2) Calculated Field (Type)
Numeric > Duration (sec.)
1.3) Calculated Field (Aggregation)
- It's set to
SUM
by default, though it can be changed as required, e.g. AVG
.
Editable Google Data Studio Report and a GIF to elaborate:
2) CAST & RegEx
2.1) Calculated Field (Formula)
(CAST(REGEXP_EXTRACT(DELIVERY_HOUR, "^([^:]+):") AS NUMBER)*60*60 +
CAST(REGEXP_EXTRACT(DELIVERY_HOUR, ":([^:]+):") AS NUMBER)*60 +
CAST(REGEXP_EXTRACT(DELIVERY_HOUR, ":(\\d+)$") AS NUMBER)) -
(CAST(REGEXP_EXTRACT(ACCEPTED_DRIVER_HOUR, "^([^:]+):") AS NUMBER)*60*60 +
CAST(REGEXP_EXTRACT(ACCEPTED_DRIVER_HOUR, ":([^:]+):") AS NUMBER)*60 +
CAST(REGEXP_EXTRACT(ACCEPTED_DRIVER_HOUR, ":(\\d+)$") AS NUMBER))
2.2) Calculated Field (Type)
Numeric > Duration (sec.)
2.3) Calculated Field (Aggregation)
- It's set to
SUM
by default, though it can be changed as required, e.g. AVG
.
Added a New Page to the Editable Google Data Studio Report and a GIF to demonstrate: