Posts

Showing posts with the label BigQuery

Query BigQuery conveniently with an Apps Script library

If you use Google Apps Script a lot, and particularly for working with BigQuery, then you might be interested in this library: BigQuery Helper . Just include the library's key "MjmmwOcqTeIFoRwuA8KZV5MtILCH4r83t" in your Apps Script script and you're ready to go! Why use my library? What this library does is it saves you from writing all of the "plumbing" code to interact with BigQuery. You can then concentrate on writing your query and any other code for manipulating the queried data. Increase your productivity! The Apps Script way – not productive! Apps Script's developer documentation for BigQuery has some sample code for working with BigQuery. But it forces you to write this code:   var queryResults = BigQuery.Jobs.query(request, projectId);   var jobId = queryResults.jobReference.jobId;   // Check on status of the Query Job.   var sleepTimeMs = 500;   while (!queryResults.jobComplete) {     Utilities.sleep(sleepTimeMs);  ...

How to "unpivot" a table in BigQuery

Recently, I was presented with a problem in BigQuery: How do I make a table's metrics be returned as rows? Normally, metrics are presented as columns and dimensions as rows. But in this case, I needed the metrics to appear as rows too. In short, I needed to "unpivot" a table like this: Dimension1 Dimension2 Metric1 Metric2 Metric3 to this: Dimension1 Dimension2 Metric Data Metric1 Metric2 Metric3 Metric1 Metric2 Metric3 Metric1 Metric2 Metric3 The solutions lay with three BigQuery features: STRUCT type ARRAY s CROSS JOIN with the UNNEST operator How to get unpivoted results in BigQuery Let's say I have data like this in a table called "Campaign_Results": Row Superhero Title Date Impressions Clicks Conversions 1 Superman Man of Steel 2013-06-14 5670096 796191 82538 2 Batman Batman v Superman: Dawn of Justice 2016-03-25 9742525 876292 63812 3 Wonder Woman Wonder Woman 2017-06-02 4847506 420674 23428 Bu...

Querying Google Analytics data as flat tables from BigQuery: The Definitive Guide and Recipe

Image
If you're using Google Analytics 360, the premium version of Google Analytics, then chances are you have setup an automatic export of your Google Analytics data into BigQuery. This allows your hit-level data in Google Analytics to be exported to BigQuery datasets every day. But Google does not provide much help with figuring out how to query your data! Google Analytics provides a cookbook of sample BigQuery queries . But these are very limited in scope. And they omit the most trivial -- yet important -- query that you will ever run: Get a flat table of results that you can export into a CSV file or a SQL database Flat tables are essential to perform further work on the results with Python, R and other data science languages. Another flaw in the cookbook is that it uses BigQuery's older Legacy SQL. BigQuery is already moving to its Standard SQL. But examples based on Google Analytics data were either difficult to find or based on guesswork that had not been tested...