{"id":48029,"date":"2021-08-19T12:59:32","date_gmt":"2021-08-19T10:59:32","guid":{"rendered":"https:\/\/www.makingscience.com\/blog\/connect-google-sheets-with-bigquery-for-fun-and-profit\/"},"modified":"2021-08-19T12:59:32","modified_gmt":"2021-08-19T10:59:32","slug":"connect-google-sheets-with-bigquery-for-fun-and-profit","status":"publish","type":"post","link":"https:\/\/www.makingscience.com\/us\/blog\/connect-google-sheets-with-bigquery-for-fun-and-profit\/","title":{"rendered":"Connect Google Sheets with BigQuery for Fun and Profit"},"content":{"rendered":"<p>(or: Saving $$$ doing Invoice Calculation and Generation using Google BigQuery and Google Sheets).<\/p>\n<p>Since VisiCalc and Lotus 1-2-3 popularized spreadsheet programs, they have become an ubiquitous tool. Spreadsheet\u2019s tabular format and dynamic calculations make them a perfect fit for accounting and data analysis. But like multifunctional \u201cswiss army knives\u201d, spreadsheets have some rough edges.<\/p>\n<p>Many of these rough edges, such as the lack of concurrent editing, no access authorization, or data protection, are solved with SaaS spreadsheets, such as Google Sheets. However these cloud-based spreadsheets also have their shortcomings when handling huge amounts of data and, even when the analyzed data fits in their limits, the fact that they are not in sync with the data source leads to the problem of \u201cmultiple versions of the truth\u201d.<\/p>\n<p>The solution would be being able to connect cloud spreadsheets with Big Data sources. A \u2018holy grail\u2019 that would allow overcoming these shortcomings and provide a secure and reliable way of analyzing data, and even generating customer quality level documents. And that is exactly what\u00a0<i>Google Connected Sheets<\/i>\u00a0does. This feature allows connecting\u00a0<i>Google Sheets<\/i>\u00a0with\u00a0<i>BigQuery<\/i>\u00a0datasets.<\/p>\n<p>&nbsp;<\/p>\n<h2><b>Google Connected Sheets Tips &amp; Tricks<\/b><\/h2>\n<p>What is needed to use this feature? Access to Google Cloud Platform and Enterprise level accounts. Users also need access permissions to BigQuery datasets and tables to be able to fully use this feature.<\/p>\n<p>Without access permissions to BigQuery, it\u2019s not possible to connect new spreadsheets to them, and if already connected spreadsheets are shared with users without these access permissions to BigQuery sources, they will be able to use Sheets features, but won\u2019t be able to refresh BigQuery data in the sheet or schedule these refreshes, as explained by this\u00a0<a href=\"https:\/\/support.google.com\/docs\/answer\/9702507\" target=\"_blank\" rel=\"noopener\">Google help article<\/a>.<\/p>\n<p>There is no point in describing in this post how this connection is done, as\u00a0<a href=\"https:\/\/cloud.google.com\/bigquery\/docs\/connected-sheets\" target=\"_blank\" rel=\"noopener\">Google documentation<\/a>\u00a0does that already. But there are several tips worth sharing here.<\/p>\n<p>First of all, is the use of\u00a0<b>parameterized queries<\/b>. The most straightforward way of using Connected Sheets is creating a direct connection to a single Big Query table, and so, to all its rows.<\/p>\n<p>But, perhaps we need some level of aggregation for the data we fetch? Or perhaps the data must come from several different tables?\u00a0Or, even if all aggregation and analysis calculations are going to be done in the spreadsheet, maybe we don\u2019t need all rows, just a specific range? Or perhaps we need all of these?<\/p>\n<p>In all of the cases aforementioned, the\u00a0<i>Write Custom Query<\/i>\u00a0option is what needs to be used. The\u00a0<i>Write Custom Query<\/i>\u00a0option allows you to specify which data must be fetched from BigQuery using an SQL query.<\/p>\n<p>As well as the\u00a0<i>Write Custom Query<\/i>\u00a0option, for further flexibility, it\u2019s possible to parametrize your query with values from selected cells in the spreadsheet.<\/p>\n<p>In the image below we can see an example of a parameter configuration:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-10963\" src=\"https:\/\/www.makingscience.co.uk\/wp-content\/uploads\/2021\/09\/e1ff2392-captura-de-pantalla-2021-08-13-a-las-10.05.48.png\" alt=\"\" width=\"1446\" height=\"1060\" \/>You can pick a cell in any of the existing sheets to create a named parameter. The named parameter can then be referenced in the query using the\u00a0<i>\u201c@PARAMETER_NAME\u201d<\/i>\u00a0notation. In the example we are showing above, we have named the parameters @START_DATE and @END_DATE and they have been related to cells C2 and D2 respectively.<\/p>\n<p>(<b>Note<\/b>: when creating the query parameter, the selected cell must already contain a value). Once the value of any of these parameters is changed, the cells that depend on the affected connection query can be updated by just clicking their popups\u00a0<i>refresh<\/i>\u00a0text. Or go to the\u00a0<i>Data -&gt; Data connectors -&gt; Refresh options<\/i>\u00a0to update a specific or just all connections in the spreadsheet.<\/p>\n<p>A very important factor related to query parameters is to know how tables are\u00a0<a href=\"https:\/\/cloud.google.com\/bigquery\/docs\/partitioned-tables\" target=\"_blank\" rel=\"noopener\"><b>partitioned<\/b><\/a>\u00a0and\u00a0<a href=\"https:\/\/cloud.google.com\/bigquery\/docs\/clustered-tables\" target=\"_blank\" rel=\"noopener\"><b>clustered<\/b><\/a>\u00a0in BigQuery. To make a query more efficient, it is important to use, if possible, a\u00a0<i>where<\/i>\u00a0clause, using columns that the searched table uses for partitioning or clustering. This allows the BigQuery query engine to restrict the data it needs to scan in order to process the query and thus improving performance.<\/p>\n<p>The use of partitioning or clustering not only improves BigQuery usage performance but IMPORTANTLY reduces costs; as Google charges for BigQuery usage based on the amount of scanned data, a wise use of partitions and clusters can reduce costs, a lot.<\/p>\n<p>&nbsp;<\/p>\n<p>The lesson here is that when linking Sheets with BigQuery, try to use partitioning and clustering as much as possible. If the data tables are given and it\u2019s not possible to change its schema, try to use their partition and cluster columns as parameters for the spreadsheet connecting queries. If you can design the tables schemas, set their partitioning and clustering based on the parameters the spreadsheet requires. That way, BigQuery\u2019s performance, and cost will be optimal.<\/p>\n<p>Another interesting option related to Connected Sheets is\u00a0<b>Scheduled refresh<\/b>. It allows you to set the refresh timing and frequency of connected sheets and related objects. If we add to the mix the\u00a0<b>Publish to the web<\/b>\u00a0option, then Connected Sheets become a way to create quite capable web applications.<\/p>\n<p>&nbsp;<\/p>\n<h2><b>Invoicing; an Interesting Use Case<\/b><\/h2>\n<p>Google documentation lists\u00a0<i>Business planning<\/i>,\u00a0<i>Customer service<\/i>,\u00a0<i>Sales reports<\/i>\u00a0as possible use cases for Connected Sheets. We found another possible use:\u00a0<i>Invoicing<\/i>.<\/p>\n<p>This is especially salient when the source for such invoices is millions of data rows, for example, for\u00a0<b>large accounts<\/b>\u00a0in the telecom sector. These kinds of customers usually have invoicing requirements that are very different from normal subscribers and require a high grade of flexibility.<\/p>\n<p>Manually invoicing shouldn\u2019t be an option, as it is prone to errors and mistakes. Connected Sheets are a very handy tool to use in this case. They are highly configurable and flexible, which allows them to conform to the customer billing requirements and once the spreadsheet is set up, invoice generation becomes an automatic or semi-automated process where human errors are taken out of the scene.<\/p>\n<p>Also, the fact that the invoicing data comes from BigQuery and is available to other tools, like Data Studio, makes it possible to give the customer additional information related to their invoices, such as reports and dashboards. All this with the confidence that, as all of them use the same\u00a0<i>Single Source of Truth<\/i>, they are totally consistent with each other.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(or: Saving $$$ doing Invoice Calculation and Generation using Google BigQuery and Google Sheets). Since VisiCalc and Lotus 1-2-3 popularized spreadsheet programs, they have become an ubiquitous tool. Spreadsheet\u2019s tabular format and dynamic calculations make them a perfect fit for accounting and data analysis. But like multifunctional \u201cswiss army knives\u201d, spreadsheets have some rough edges. [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":42869,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[848],"tags":[65,145],"class_list":["post-48029","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-google-cloud-en","tag-bigquery","tag-google-sheets"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.makingscience.com\/us\/wp-json\/wp\/v2\/posts\/48029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.makingscience.com\/us\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.makingscience.com\/us\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.makingscience.com\/us\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.makingscience.com\/us\/wp-json\/wp\/v2\/comments?post=48029"}],"version-history":[{"count":0,"href":"https:\/\/www.makingscience.com\/us\/wp-json\/wp\/v2\/posts\/48029\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.makingscience.com\/us\/wp-json\/wp\/v2\/media\/42869"}],"wp:attachment":[{"href":"https:\/\/www.makingscience.com\/us\/wp-json\/wp\/v2\/media?parent=48029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.makingscience.com\/us\/wp-json\/wp\/v2\/categories?post=48029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.makingscience.com\/us\/wp-json\/wp\/v2\/tags?post=48029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}