avatarZach Quinn

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4224

Abstract

eport date. Screenshot by the author.</figcaption></figure><p id="45aa">I now need to isolate just the month, 10, using the EXTRACT function.</p><div id="92cc"><pre><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">EXTRACT</span>(<span class="hljs-keyword">MONTH</span> <span class="hljs-keyword">FROM</span> report_date) <span class="hljs-keyword">AS</span> report_month</pre></div><figure id="71fc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Shz9hE8YEZo_w0G_huKrhw.png"><figcaption>BigQuery output — report month. Screenshot by the author.</figcaption></figure><p id="eb4a">I can now use a neat trick I developed for a prior build: A lookup table that matches a numeric month to its string equivalent.</p><p id="624b">If you want more details on how that works, the story is below:</p><div id="592d" class="link-block"> <a href="https://readmedium.com/sql-users-reduce-your-case-statements-with-a-lookup-table-d09da47bf65"> <div> <div> <h2>SQL Users: Reduce Your CASE Statements With A Lookup Table</h2> <div><h3>Map fields in your SQL tables without using CASE.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*hMmNxlT4JCb_Qn__Ascfag.jpeg)"></div> </div> </div> </a> </div><p id="279f">Here’s my CTE for that table reference; it’s nothing fancy or complex.</p><div id="9130"><pre><span class="hljs-keyword">WITH</span> lookup_month <span class="hljs-keyword">AS</span> (

<span class="hljs-keyword">SELECT</span> month_str, SAFE_CAST(month_num <span class="hljs-keyword">AS</span> <span class="hljs-type">INTEGER</span>) <span class="hljs-keyword">AS</span> month_num <span class="hljs-keyword">FROM</span> ornate<span class="hljs-operator">-</span>reef<span class="hljs-number">-332816.</span>billing_sample.lookup_month )</pre></div><figure id="33f1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*tYj9FAlDeKWkn42ItIM3rw.png"><figcaption>Month with corresponding numerical representation. Screenshot by the author.</figcaption></figure><p id="6337">And the JOIN that brings everything together… again, just a simple LEFT JOIN of two tables.</p><div id="fbb2"><pre><span class="hljs-keyword">SELECT</span> month_str <span class="hljs-keyword">FROM</span> ( <span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">EXTRACT</span>(<span class="hljs-keyword">MONTH</span> <span class="hljs-keyword">FROM</span> report_date) <span class="hljs-keyword">AS</span> report_month <span class="hljs-keyword">FROM</span> ( <span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">CURRENT_DATE</span>("America/New_York") <span class="hljs-keyword">AS</span> report_date )) <span class="hljs-keyword">LEFT</span> <span class="hljs-keyword">JOIN</span> lookup_month <span class="hljs-keyword">ON</span> lookup_month.month_num <span class="hljs-operator">=</span> report_month</pre></div><p id="a477">Which produces our desired output:</p><figure id="ed32"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*pvYZm3EC8GogMopqck0_4g.png"><figcaption>String representation of month of October. Screenshot by the author.</figcaption></figure><p id="2c42">So I’ve solved 50% of my problem. Now I need to this month string to be visible in my Looker report.</p><p id="9b2e"><i>Pardon the interruption: To receive more data science-oriented content, <a href="https://medium.com/pipeline-a-data-engineering-resource"><b>consider following Pipeline</b></a><b>.</b></i></p><p id="c924"><i>To receive my latest writing, you can <a href="https://medium.com/@zachl-quinn"><b>follow me</b></a> as well.</i></p><h1 id="c96e">Connecting And Prettify-ing The Output In Looker</h1><p id="4e63">First, we’ll connect the view:</p><figure id="388e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*fZOtygm5T0RY4xfnGLlAJg.png"><figcaption>Looker UI view connection. Screenshot by the author.</figcaption></figure><p id="da7c">And see that the Record Count field is a default me

Options

tric. This will cause problems and, at first, not allow us to see “October” by itself.</p><p id="5a43">In addition to Record Count, the row number and pagination that prevents us from isolating “October.”</p><p id="1c89">First, we’ll get rid of Record Count (note the x next to the field).</p><figure id="4247"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_3xdsyyDaz5zbMQvMqoYpg.png"><figcaption>Looker UI metric selection. Screenshot by the author.</figcaption></figure><figure id="f917"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*cD5X7MENBHaZlb4VNFtkLQ.png"><figcaption>Looker UI data. Screenshot by the author.</figcaption></figure><p id="7dbd">Next we’ll get rid of the header, month_str.</p><figure id="40b3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*LjgPYD-HwBXmJLjcperHMQ.png"><figcaption>Looker UI styling. Screenshot by the author.</figcaption></figure><figure id="f651"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-tL0A_uvZgT2e9bAgBPzGQ.png"><figcaption>1-row representation of October. Screenshot by the author.</figcaption></figure><p id="0ae7">And uncheck the boxes for both row number and pagination…</p><figure id="399e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*M-WdbXzo_wjCCZi8CwNmDg.png"><figcaption>Looker style elements. Screenshot by the author.</figcaption></figure><p id="5b6a">Now we finally have “October” isolated as a text header.</p><figure id="5c90"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*JG-dIcDocyi5xaStVErGiw.png"><figcaption>October isolated. Screenshot by the author.</figcaption></figure><p id="f825">Converting this row value to a header is just a matter of playing with the sizing, font and orientation.</p><figure id="56e8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*X5MOrw221nMgNeigqpm1PQ.png"><figcaption>Final header before applying formatting. Screenshot by the author.</figcaption></figure><p id="de62">And, to match the initial example, we’ll change the background and bold the text.</p><figure id="e0bd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*uDGmvESUPmTzfm7EYxEbLg.png"><figcaption>Final header. Screenshot by the author.</figcaption></figure><p id="bf01">There we go!</p><p id="f06a">The best part of this is that this value is not a regular, static text header. You won’t need to change this to “November” or “December” when the time comes because the view will always get the month for the current date.</p><div id="8aa1"><pre><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">EXTRACT</span>(<span class="hljs-keyword">MONTH</span> <span class="hljs-keyword">FROM</span> report_date) <span class="hljs-keyword">AS</span> report_month <span class="hljs-keyword">FROM</span> ( <span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">CURRENT_DATE</span>("America/New_York") <span class="hljs-keyword">AS</span> report_date ))</pre></div><h1 id="20d8">Frustration →Solution</h1><p id="1a08">Even as a professional, I am probably overly reliant on group think solutions like those posted on Stack Overflow and other forums.</p><p id="7d33">Now, a few years into my role, every time I encounter a problem, whether personal or professional, it takes me back to grad school days when professors would go out of their way to pick problem sets that didn’t live on the Internet.</p><p id="e34c">When you solve a problem with little to no documentation or template, you get a different kind of satisfaction than finding that magical line of Python or SQL that cuts down your work load by a few hours.</p><p id="1674">Each time you solve a problem with no existing solution, you not only gain knowledge but you also gain a bit of confidence. For newcomers it’s that “I can do it mindset.” For established professionals it’s more of an “I can still do this” feeling.</p><p id="8ba5">Either reaction distills the technological role down to its essence: Solving problems. No matter how incrementally.</p><p id="3814"><b>Create a job-worthy data portfolio. Learn how <a href="https://pipe_line.ck.page/e97fc26c83">with my free project guide</a>.</b></p></article></body>

Generating Automated Headers In Looker For More Dynamic Reporting

In less than 10 minutes create a Looker header that dynamically displays attributes — like the current month — for your users.

Author’s note: In technical roles like data engineering, sometimes you come across problems so unique to your use case that solutions don’t yet exist. This was one of those problems. On behalf of my past self, I hope this is helpful for you.

Date time operations, despite being rooted in geographies hundreds of years old, continue to pose programmatic challenges across languages; I know this because at PyCon 23 I attended a 45-minute talk solely on time operations and timezone conversions.

And while I’ve written about challenges SQL users face when conducting date and time operations, I want to address a specific use case at a point I rarely speak about: The end of the pipeline. A.K.A. the visualization platform the data consumer uses to access reporting.

In this case, because I’m partial to a Google Cloud Platform-oriented stack, I’m talking about Looker. Instead of delving into nuanced calculations like UTC conversion or handling daylight savings time, I want to focus on one deceptively simple problem I recently encountered when creating a dash.

My question: How do I create a string representation of the current month above a monthly engagement report, like the below image?

String representation of a month. Screenshot by the author.

The added challenge: How do I get that string representation to seamlessly and automatically change to the next month at the correct interval?

And, finally, how do I take a table (like the one below) and turn it into a clean, concise month?

One-row table output with current month. Screenshot by the author.

I wish my thought process was as elegant as the above break down. Instead, like solving many data-oriented or software problems it involved a close reading of any available documentation, iteration and, in the end, good old trial and error.

Whether you’re attempting to create a month header or a similar dynamic string variable, my hope is that stepping through this process with me will help you better understand how to think not only about how to structure your data — but also about the experience of your end user.

Remember who your end user is. Photo by Melanie Deziel on Unsplash

Laying The Foundation: The Data

Since I’m a data engineer, I began the only way I know how: Developing a precise and clean dataset. In this case, the ideal output would be one row.

Thankfully, I was able to leverage BigQuery’s built-in date functions to jumpstart my attempts.

In order to get the current month, I need the current date.

SELECT CURRENT_DATE("America/New_York") AS report_date
BigQuery output — report date. Screenshot by the author.

I now need to isolate just the month, 10, using the EXTRACT function.

SELECT EXTRACT(MONTH FROM report_date) AS report_month
BigQuery output — report month. Screenshot by the author.

I can now use a neat trick I developed for a prior build: A lookup table that matches a numeric month to its string equivalent.

If you want more details on how that works, the story is below:

Here’s my CTE for that table reference; it’s nothing fancy or complex.

WITH lookup_month AS (

  SELECT month_str, SAFE_CAST(month_num AS INTEGER) AS month_num
  FROM `ornate-reef-332816.billing_sample.lookup_month`
)
Month with corresponding numerical representation. Screenshot by the author.

And the JOIN that brings everything together… again, just a simple LEFT JOIN of two tables.

SELECT month_str
FROM (
SELECT EXTRACT(MONTH FROM report_date) AS report_month
FROM (
SELECT CURRENT_DATE("America/New_York") AS report_date
))
LEFT JOIN lookup_month
ON lookup_month.month_num = report_month

Which produces our desired output:

String representation of month of October. Screenshot by the author.

So I’ve solved 50% of my problem. Now I need to this month string to be visible in my Looker report.

Pardon the interruption: To receive more data science-oriented content, consider following Pipeline.

To receive my latest writing, you can follow me as well.

Connecting And Prettify-ing The Output In Looker

First, we’ll connect the view:

Looker UI view connection. Screenshot by the author.

And see that the Record Count field is a default metric. This will cause problems and, at first, not allow us to see “October” by itself.

In addition to Record Count, the row number and pagination that prevents us from isolating “October.”

First, we’ll get rid of Record Count (note the x next to the field).

Looker UI metric selection. Screenshot by the author.
Looker UI data. Screenshot by the author.

Next we’ll get rid of the header, month_str.

Looker UI styling. Screenshot by the author.
1-row representation of October. Screenshot by the author.

And uncheck the boxes for both row number and pagination…

Looker style elements. Screenshot by the author.

Now we finally have “October” isolated as a text header.

October isolated. Screenshot by the author.

Converting this row value to a header is just a matter of playing with the sizing, font and orientation.

Final header before applying formatting. Screenshot by the author.

And, to match the initial example, we’ll change the background and bold the text.

Final header. Screenshot by the author.

There we go!

The best part of this is that this value is not a regular, static text header. You won’t need to change this to “November” or “December” when the time comes because the view will always get the month for the current date.

SELECT EXTRACT(MONTH FROM report_date) AS report_month
FROM (
SELECT CURRENT_DATE("America/New_York") AS report_date
))

Frustration →Solution

Even as a professional, I am probably overly reliant on group think solutions like those posted on Stack Overflow and other forums.

Now, a few years into my role, every time I encounter a problem, whether personal or professional, it takes me back to grad school days when professors would go out of their way to pick problem sets that didn’t live on the Internet.

When you solve a problem with little to no documentation or template, you get a different kind of satisfaction than finding that magical line of Python or SQL that cuts down your work load by a few hours.

Each time you solve a problem with no existing solution, you not only gain knowledge but you also gain a bit of confidence. For newcomers it’s that “I can do it mindset.” For established professionals it’s more of an “I can still do this” feeling.

Either reaction distills the technological role down to its essence: Solving problems. No matter how incrementally.

Create a job-worthy data portfolio. Learn how with my free project guide.

Data Engineering
Data Science
Data Analysis
Data Visualization
Business Intelligence
Recommended from ReadMedium