How to Calculate Duration in Hours Using Google Data Studio

后端 未结 1 1821
醉酒成梦
醉酒成梦 2020-12-06 22:19

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

1条回答
  •  有刺的猬
    2020-12-06 23:07

    0) Summary

    The below looks at 2 questions:

    1. Updated Question (DATETIME_DIFF): Find the difference between the two time fields, NewStartTime and NewEndTime and also incorporate a Date field;
    2. 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:

    0 讨论(0)
提交回复
热议问题