Time between Google Data Studio - time

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:

Related

Get 1 Month low price of a stock with Google Finance using Google Sheets

For 1 Month low: I tried this Code in Google Sheet does not working
enter code here
=MIN(INDEX(GOOGLEFINANCE("NYSE:UBER","price",DATE(MONTH(TODAY()-30,MONTH(TODAY()),DAY(TODAY())), TODAY()),0,2))
Does not returning any value
When entered into Google sheets your formula comes up with the following error message:
"Wrong number of arguments to DATE. Expected 3 arguments, but received 2 arguments."
Looking at the formula there are a number of issues -
The date function is being fed three elements, but they do not make up a correct date. The function is being fed the month value twice instead of a year, month, day value.
So
=DATE(MONTH(TODAY()-30,MONTH(TODAY()),DAY(TODAY()))
should be
=DATE(year(TODAY()),MONTH(TODAY()-30),DAY(TODAY()))
In your original formula there is also a ) bracket missing as part of the year element of the date formula, so the formula wasen't returning the right number of elements.
When viewing the Google Docs editors help file webpage, if your intention is to return the low value of a stock, you are also using the incorrect attribute element to pull this value out. You have used "price" which returns
‘price’ – Real-time price quote, delayed by up to 20 minutes.
but should be using
'low' – The current day's low price.
Again referancing the Google Docs editors help file webpage you can also shorten your date element to only use the TODAY()-30 element instead of entering the a full date string.
As such your origional formula can be changed from:
=MIN(INDEX(GOOGLEFINANCE("NYSE:UBER","price",DATE(MONTH(TODAY()-30,MONTH(TODAY()),DAY(TODAY())), TODAY()),0,2))
to
=MIN(INDEX(GOOGLEFINANCE("NYSE:UBER","price",MONTH(TODAY()-30), TODAY()),0,2))

Tableau - Fixed calculated field depending on date filter

I'm new to Tableau. I am trying to make an inventory report which tells the user how much of certain product he/she should buy in advance.
Depending on the amount of days selected on the filter, the difference in days of the complete period should be calculated. For example: If the filtered dates are from 1/03/2021 to 09/03/2021, the result should be equal to 9. The formula I used is the following: date_difference = DATEDIFF("day",MIN(DATE([Fecha])), MAX(DATE([Fecha]))) + 1
The problem comes when I try to use the value given by such date filter. My next calculation should be:
calc = Quantity Inventory / (Units sold / date_difference). Both Quantity Inventory and Units sold are calculated fields in which I have no problem. However, instead of having a fixed value of 9, date_difference changes as shown in the image, giving me incorrect results for the desired calculation.
How can I make sure that the calculated field date_difference has the value of 9 on all rows?. Actually, if I add date_difference field by itself in a different Page it does show the proper value. The problem occurs when calculating calc and trying to add it to the table.
Note: Remember that the value of date_difference will change, depending on the range of time selected on the date filter
Thanks a lot in advance.
Step-1 Use this calculation for date_difference instead
DATEDIFF('day', {min(DATE([Fecha]))}, {max(DATE([Fecha]))}) +1
Step-2 Add Fecha filter to context, by right clicking it in filters shelf.
This will solve your problem. See the GIF

How to Calculate Duration in Hours Using Google Data Studio

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:

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

Quicksight parse date into month

Maybe I missed it but I'm attempting to create a dynamic 'Month' parameter based on a datetime field - but can't seem to get just the month! ? Am I missing something ?
here's my source DTTM date/time field -
In Manage Data > Edit [selected] Data Set > Data source
Just add 'calculated field':
truncDate('MM', date)
where MM returns the month portion of the date.
See manual of truncDate function
The only place in Quicksight that you can get just a month, e.g. "September" is on a date-based axis of a visual. To do so, click the dropdown arrow next to the field name in the fields list, select "Format: (date)" then "More Formatting Options..." then "Custom" and enter MMMM in the Custom format input box.
Quicksight menu selection as described
This will then show the full month name on the date axis in your visual. NB It will use the full month name on this visual for ALL time period "Aggregations" - e.g. if you change the visual to aggregate by Quarter, it will display the full name of the quarter's first month etc.
If you are talking about "Parameters" in the Quicksight analysis view then you can only create a "Datetime" formatted parameter and then only use the "Date picker" box format for this parameter in a control (+ filter).
If you use a calculated field in either data preparation or analysis view the only date functions do not allow full month names as an output, you can get the month number as an integer or one of the allowed date formats here:
https://docs.aws.amazon.com/quicksight/latest/user/data-source-limits.html#supported-date-formats
You'll need to hardcode the desired results using ifelse, min, and extract.
Extract will pull out the month as an integer. Quicksight has a desire to beginning summing integers, so we'll put MIN in place to prevent that.
ifelse(min(extract('MM',Date)) = 1,'January',min(extract('MM',Date)) = 2,'February',min(extract('MM',Date)) = 3,'March',min(extract('MM',Date)) = 4,'April',min(extract('MM',Date)) = 5,'May',min(extract('MM',Date)) = 6,'June',min(extract('MM',Date)) = 7,'July',min(extract('MM',Date)) = 8,'August',min(extract('MM',Date)) = 9,'September',min(extract('MM',Date)) = 10,'October',min(extract('MM',Date)) = 11,'November',min(extract('MM',Date)) = 12,'December','Error')
Also, I apologize if this misses the mark. I'm not able to see the screeshot you posted due to security controls here at the office.
You can use the extract function. Works like this:
event_timestamp Nov 9, 2021
extract('MM', event_timestamp)
11
You can add a calculated field using the extract function:
extract returns a specified portion of a date value. Requesting a time-related portion of a date that doesn't contain time information returns 0.
extract('MM', date_field)

Resources