How to Calculate Duration in Hours Using Google Data Studio - time

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 was able to get the results I wanted in Google sheets using =(H2-I2)*1440. However, I want the managed the calulation using Google Data Studio. I tired using CAST(EndTime AS NUMBER ) - CAST(StartTime AS NUMBER ) in Google Data Studio but that didn't seem to work.

0) Summary
The below looks at 2 questions:
Updated Question (DATETIME_DIFF): Find the difference between the two time fields, NewStartTime and NewEndTime and also incorporate a Date field;
Original Question (TIME_DIFF): Looks for the difference between 2 Time fields, StatTime and EndTime.
1) Update (17 Sep 2020 Dates & Time Update)
Updated the Answer with the solution using the Updated Date Time Functions which incorporates the PARSE_DATETIME and DATETIME_DIFF functions:
1.1) Upgrade the Date Field
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:
1.2) 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)
1.3) Type (DATETIME_DIFF)
Number > Duration (Sec.)
Added a New Page to the Report and a GIF to demonstrate:
2) Original Post
It can be achieved using the 3 steps below:
2.1) Type (HH:MM Fields)
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:
2.2) 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))
2.3) Type (Time_DIFF)
Numeric > Duration (Sec.)
Google Data Studio Report and a GIF to elaborate:

Related

Time between Google Data Studio

I have two Fields:
ACCEPTED_DRIVER_HOUR
DELIVERY_HOUR
18:42:01
18:49:00
In Google Data Studio, these two fields are shown as Text Fields, so what I want to do is:
SUM(ACCEPTED_DRIVER_HOUR) - SUM (DELIVERY_HOUR)
This way, I can get how much time the driver took to deliver and order.
The problem is that Google Data Studio says that I can't do that because the field is Text and if I change to Number, it says NULL because the numbers have : between them; if I try to change to Date Hour it's not possible.
I already tried to create new fields using CAST and it shows the same error (NULL)
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:

How to extract "last updated" date from this URL to google sheets

I have tried this, but I get three dots
=IMPORTXML("https://covid19.sabah.digital/covid19/","//span[#class='number-last_updated']")
When I saw the HTML data, it seems that the last updated date is displayed by Javascript. By this, unfortunately, the value cannot be directly retrieved with IMPORTXML. This has already been mentioned in the comments.
When I saw the HTML data again, I noticed that the information of date is included in https://data.covid19.sabah.digital/global.json. From this data, how about retrieving the last updated date? In this answer, as a workaround, in order to retrieve the last updated value, I would like to propose to retrieve the data using the following sample formula.
Sample formula:
=TEXT(MAX(ARRAYFORMULA(DATEVALUE(REGEXEXTRACT(QUERY(IMPORTDATA(A1),"SELECT Col1 WHERE Col1 contains 'date'"),"\d{4}-\d{1,2}-\d{1,2}"))))+1,"yyyy-MM-dd")
In this formula, please put the URL of https://data.covid19.sabah.digital/global.json to the cell "A1".
The flow of this formula is as follows.
Retrieve the JSON data using IMPORTDATA.
Retrieve the data values from the retrieved data using QUERY.
Convert the text to the serial number using DATEVALUE.
Retrieve the max value using MAX.
It seems that when this value is added by 1, it is the updated date.
If you don't need this, please remove +1 from the formula.
Convert the serial number to the text using TEXT.
Result:
References:
IMPORTDATA
QUERY
DATEVALUE
MAX
TEXT

Data Studio Table Chart is not sorting correctly

I have a working chart of podcast episodes by download count in a query. That query is used to create a table chart in Data Studio. The file name formats are as follows: 2020/889-Jan-16-2020-DMP.mp3
Well Episode 1000 isn't showing at the top now in the sorting order. Because it thinks 1000 is less than 999. See table below:
2020/999-Jun-24-2020-DMP.mp3
2020/998-Jun-23-2020-DMP.mp3
2020/997-Jun-22-2020-DMP.mp3
2020/996-Jun-21-2020-DMP.mp3
2020/995-Jun-18-2020-DMP.mp3
2020/994-Jun-17-2020-DMP.mp3
2020/993-Jun-16-2020-DMP.mp3
continuing ...
2020/886-Jan-13-2019-DMP.mp3
2020/885-Jan-12-2019-DMP.mp3
2020/884-Jan-9-2019-DMP.mp3
2020/883-Jan-8-2019-DMP.mp3
2020/882-Jan-7-2019-DMP.mp3
2020/881-Jan-6-2019-DMP.mp3
2020/880-Jan-5-2019-DMP.mp3
2020/879-Jan-2-2019-DMP.mp3
2020/1001-Jun-30-2020-DMP.mp3 <-------Should be at the top of the table
2020/1000-Jun-29-2020-DMP.mp3 <-------Should be at the top of the table
2019/878-Dec-19-2019-DMP.mp3
2019/877-Dec-18-2019-DMP.mp3
2019/876-Dec-17-2019-DMP.mp3
Let me know if that makes sense...
1) REGEXP_EXTRACT
It can be achieved by adding the REGEXP_EXTRACT Calculated Field below as the Sort field and setting the order to Descending (The RegEx extracts the respective number component, for example 1001):
AVG(CAST(REGEXP_EXTRACT(Field, "^\\d+/(\\d+)-") AS NUMBER ) )
Google Data Studio Report and a GIF to elaborate:
2) Troubleshooting Calculated Fields (Invalid Formula)
Adding a section on general troubleshooting for Calculated Fields based on an Earlier Post on the Google Data Studio Forum:
Field Editing: Have a look at whether Field Editing is Enabled (Although it shouldn't affect creating Data Source Calculated fields);
Refresh: Refresh the Data Source Fields as well as a Fields in the Report;
Page Reload: Shortcut - F5;
Hard Page Reload: Shortcut - Ctrl + F5;
Chart-level Calculated Field: Double check whether using a Chart-level Calculated Field instead of a Data Source-level Calculated Field, resolves the issue.
I found that doing this worked too - sort date granularity by Year Week
Sort date granularity by Year Week

Google sheets time formatting won't work for me

I'm rather new to google sheets, and I'm trying to make a timesheet that will calculate hours and minutes automatically. The problem I'm facing is that the formula always returns a "#VALUE!"-error because "23:15 is a text and cannot be coerced to a number".
I've formatted the cells to time, but it won't recognize the input as hours and minutes. What am I doing wrong here?
It happened to me too, I discovered that you have to change the location:
File > Spreadsheet Setting > Locale > United Kingdom
you will see that now you can type your time correctly with the ":"
Try going to Format, Number, More Formats, More Date time formats.Select Hour Minute (13:30). Apply this format to all columns (or cells) involved in the calculation.
Here is a copy of my test spreadsheet. I can't get yours to work(?), but this does:
https://docs.google.com/spreadsheets/d/1Qg1JJU9MELesqllHFOhKFO-jxDf1fJMiIVznU1vNP0w/edit?usp=sharing
It is set to your country.

Duration format in google spreadsheet

I'm trying to apply a duration format to some cells in google spreadsheet. I would like to convert an integer number in a format: X days x hours x minutes.
I've tried with some formats like: d:h:mm but i found a problem when I apply the format. It always put one day less. When I write 1 in the cell the convert to 31:0:00. When I write 2 the cells changes to 1:00:00.
That is because the duration format is actually a date / time format (for comparing dates).
If you simply enter a number (1) google will interpret that as midnight (as times are stored as fractions of whole days) of the reference day number 1.
Reference day in Google Sheets is 31/12/1899 - IE the 31st day of the month. That is why your result returns days=31.
To achieve what you want you effectively want to add 1 to your values. so that 1 (+1) actually becomes "2 days since 31/12/1899 - ie 01/01/1900 - ie 1 day, and you could then use custom format for display, but this wont work when you have >31 days.
I think the best way is to simply concatenate the data you have with relavent parts like so (where A1 is a cell containg your data - 1,2,1.5 etc):
=int(A1)&" days "&int(MOD(A1,1)*24)&" hours " & mod(MOD(A1,1)*24,1)*60 & " minutes"

Resources