Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Sign InSign Up

Softans

Softans Logo Softans Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 1012
Answered
Ghulam Nabi
Ghulam Nabi
Asked: December 28, 20212021-12-28T04:38:22+00:00 2021-12-28T04:38:22+00:00

Bucketizing record counts by the hour of this day only and showing hours in axis even without record

I’m starting with Google Data Studio. So I have this event_date  (YYYYMMDD) and event_timestamp  (Unix Epoch in microseconds) columns for each record from BigQuery (original data source is Firebase). I created a time-series / bar chart (I tried both) that displays the x-axis as the hour of the current day (I filtered on event_date) And the y-axis as the number of records.

The way I did this is to create a calculated field using the Data Studio function HOUR(event_timestamp) and then displayed as HH. The problem here is for certain hours that we don’t have any corresponding record, there is no entry for the x-axis. What I want is even if there are no records for that specific hour, it will still show. So basically, the x-axis numbers look like this:

00, 01, 02, ..., 22, 23

Side question: I noticed that the time series chart sorts from most recent to oldest. How can I reverse the sort?

google querygoogle-bigquerygoogle-data-studio
  • 2
  • 1 1 Answer
  • 33 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  1. Best Answer
    Ghulam Nabi
    2021-12-28T04:40:09+00:00Added an answer on December 28, 2021 at 4:40 am

    I can think of a couple ways to do so that basically rely on an OUTER JOIN With all possible dimension values (0-23h).

    To frame the problem I will use a BigQuery public dataset: bigquery-public-data.london_bicycles.cycle_hire where start_date is a TIMESTAMP A field containing the time when each ride started. I will filter out all trips that started before 7 AM with the following query:

    SELECT
      *
    FROM
      bigquery<span>-</span>public<span>-</span>data.london_bicycles.cycle_hire
    WHERE
      EXTRACT(HOUR FROM start_date) > 6
    

    Therefore, I get the same problem when creating a data source with the previous custom query (hours without data are not displayed):

    enter image description here


    Blending with range data

    The first and quick fix would be to create a data source that contains all possible dimension values. This could be done in a spreadsheet or, for example, with an additional 0 B custom query:

    SELECT
      *
    FROM
      UNNEST(GENERATE_ARRAY(0, 23)) AS hour
    

    and now we blend the data so that the hour array is on the left side (Data Studio uses LEFT OUTER JOIN) and join with HOUR(start_date) from the cycle_hire table:

    enter image description here

    Then we get the desired histogram (be sure to change the number of bars up to 24 in the STYLE tab and sort by hour ascending in the DATA one):

    enter image description here


    Aggregate in BigQuery

    Another alternative would be to run the calculations for the hourly buckets already in the custom query. For example, in this case, we would generate the hour array, left outer join it with the aggregated data and substitute NULL for 0:

    WITH
    hours AS (
      SELECT
        *
      FROM
        UNNEST(GENERATE_ARRAY(0, 23)) AS hour),
    bike_rides AS (
      SELECT
        EXTRACT(HOUR FROM start_date) AS hour,
        COUNT(*) AS total
      FROM
        bigquery<span>-</span>public<span>-</span>data.london_bicycles.cycle_hire
      WHERE
        EXTRACT(HOUR FROM start_date) > 6
      GROUP BY
        hour )
    
    SELECT
      hour,
      IF(total IS NULL,0,total) AS total
    FROM
      bike_rides
    RIGHT OUTER JOIN
      hours
    USING
      (hour)
    

    Which gives the same result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • Ghulam Nabi

    Why are the British confused about us calling bread rolls ...

    • 5 Answers
  • Alex

    application has failed to start because no appropriate graphics hardware ...

    • 4 Answers
  • Jerry

    Add file to native target programmatically via tuist/XcodeProj

    • 4 Answers
  • Ghulam Nabi
    Ghulam Nabi added an answer To resolve the NullPointerException, you need to identify the variable… March 15, 2023 at 8:25 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can replace the PnP code in your Azure Function… February 13, 2023 at 7:11 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can use the $match stage in the aggregate pipeline… February 10, 2023 at 6:20 am

Trending Tags

android c++ cypress flutter java javascript python selenium testng webdriver

Top Members

Robert

Robert

  • 3 Questions
  • 1k Points
Luci

Luci

  • 5 Questions
  • 1k Points
Kevin O Brien

Kevin O Brien

  • 2 Questions
  • 1k Points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Softans

Softans is a social questions & Answers Engine which will help you establish your community and connect with other people.

About Us

  • Blog
  • Jobs
  • About Us
  • Meet The Team
  • Contact Us

Legal Stuff

Help

Follow

© 2021 Softans. All Rights Reserved
With Love by Softans.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.