I\'m trying to get the duration in hours using data stored in Google sheets using the following fields and display the data in Google Data Studio.
I w
The below looks at 2 questions:
NewStartTime
and NewEndTime
and also incorporate a Date
field;StatTime
and EndTime
.Updated the Answer with the solution using the Updated Date Time Functions which incorporates the PARSE_DATETIME and DATETIME_DIFF functions:
Upgrade the Date
field to the newer Date field type and ensure that the NewStartTime
and NewEndTime
fields are set to Text.
Added a GIF to elaborate:
DATETIME_DIFF
Copy-paste the Calculated Field below to create a value in seconds that shows the difference between the two fields:
DATETIME_DIFF(PARSE_DATETIME("%Y/%m/%d%I:%M:%S %p",CONCAT(Date,NewEndTime)), PARSE_DATETIME("%Y/%m/%d%I:%M:%S %p",CONCAT(Date,NewStartTime)), SECOND)
DATETIME_DIFF
)Number > Duration (Sec.)
Added a New Page to the Report and a GIF to demonstrate:
It can be achieved using the 3 steps below:
By default, the fields should be detected as Text fields, if not ensure that they are set to Text fields at the Data Source, such that it looks like:
Time_DIFF
Copy-paste the Calculated Field below to create a value in seconds that shows the difference between the two fields:
((CAST(REGEXP_EXTRACT(EndTime,"^(\\d+):")AS NUMBER)*60*60) + (CAST(REGEXP_EXTRACT(EndTime,"^\\d+:(\\d+)")AS NUMBER)*60) + NARY_MAX(CAST(REGEXP_REPLACE(EndTime,".*(PM)$","43200")AS NUMBER),0)) -
((CAST(REGEXP_EXTRACT(StatTime,"^(\\d+):")AS NUMBER)*60*60) + (CAST(REGEXP_EXTRACT(StatTime,"^\\d+:(\\d+)")AS NUMBER)*60) + NARY_MAX(CAST(REGEXP_REPLACE(StatTime,".*(PM)$","43200")AS NUMBER),0))
Time_DIFF
)Numeric > Duration (Sec.)
Google Data Studio Report and a GIF to elaborate: