Tableau In, SQL Out: The Tableau Data Lifecycle – Simple Aggregates, Disaggregate Measures & Simple Filters

Data

Tableau In, SQL Out: The Tableau Data Lifecycle – Simple Aggregates, Disaggregate Measures & Simple Filters

by Fabian Zimmer
//

Tableau is a great piece of software geared towards business users and rapid, iterative data analysis. Its interface makes it as easy as never before to connect to a vast number of different data sources and create stunning and meaningful visualizations. But what’s happening in Tableau’s engine room when you drop that blue pill on the rows shelf, set a filter or use table calculations? If you don’t fear some technical terms (only a few, I promise!) and want to learn more about the product you’re using on a daily basis, stay tuned for more posts of this blog series! Tableau is a powerful query generator and in-memory calculator, meaning that it can push most of the work to the database as well as calculate measures based on the returned results. Not bad at all! Easy does it – let’s start with the basics:

Simple Aggregates

Simple aggregations happen when the data requested for viewing in Tableau is rolled up to a lower level of detail than its original level of detail in the data source. Since a picture (or a visualisation) says a thousand words I captured this fact by adjusting the size of the individual boxes. Lots of rows of raw data, less rows of aggregated data. This aggregated data is then processed by Tableau, either going straight to rendering your visualisation, or calculating certain metrics that you have set up in calculating fields. This chapter will focus on how this aggregation happens and provide all the necessary details needed to lift the veil on what goes on in the background. Screen-Shot-2015-07-24-at-12.49.16-pm-1024x629 When you drop [Region] and [Sales] onto your worksheet, Tableau will generate the matching query and run it against the database you’re connected to. Once the database returns the results they are rendered as marks in your visualization, which by now probably looks something like this: 1 But what happened in the background? While you were watching the magic happen, Tableau sent the following query to the database. To those of you who suffer from a severe adversity towards code, hang in there, you’ll be surprised how easy it is: 2 This roughly translates to “Give me the sum of [Sales] for each [Region]”.  By that point, Tableau itself never had to dig through countless rows, but instead got served the already aggregated results to render as you saw fit. You can check this by clicking analysis -> view data, … 3 … which opens a window looking like this: 4 Neat! Let’s introduce a second measure to the party, shall we? Dropping [Profit] onto our visualization will prompt Tableau to generate a query along the lines of “Give me the sum of [Sales] as well as the sum of [Profit] for each [Region].”. Lo and behold, the query: 5 Again, all work at this point is done by the database and Tableau only reads the resulting aggregated dataset: 6 Now let’s do something with these two measures, like dividing profit by sales in order to find our profit margin. We can do this by using calculated fields, but there are two ways of doing this: the right way and the wrong way (I’ll circle back to this statement in a bit). Users who are new to Tableau might create a calculated field like this: 7While this will return arithmetically correct results, the analytical use is somewhat questionable. What just happened is that Tableau was told to ask the database for the profit margin for every single row and then calculate the average of those margins for each [Region]. 8 More seasoned users will create a calculated field as follows: 9This will prompt Tableau to ask the database for the sum of profit as well as for the sum of sales for each Region, letting the database do most of the work and return the result set. Tableau then does the math in memory and presents you with a crisp profit margin for each region. 10   Much better. Remember when I said that there’s a right and a wrong way? It really depends on what you want to see, but in this particular case a weighted profit margin is the desirable measure. Imagine the following: One salesman sells a smartphone for $600 with a profit of $450, so at a profit margin of 75%.  Another salesman sells one of those European small cars for $20000 with a profit of $2000, so at a profit margin of 10% – calculating the average of those two margins returns 42.5%, a number simply too good to be true. Instead we want to sum up all the sales as well as all the profits, and only then run the calculation: $20600 in sales and $2450 in profit computes to a more realistic profit margin of 11.9%. Phew.

Disaggregate Measures

Now that we learned about the ins and outs of basic aggregate measures in Tableau, let’s move on to the opposite: row level data. Sometimes you don’t want or need Tableau to aggregate measures, for example when creating a scatter plot on the hunt for outliers. Instead of receiving a shorter and rolled up set of data Tableau will request the data at row level before computing and rendering it. Again, the size of the individual boxes on below image capture that fact. Screen-Shot-2015-07-24-at-12.49.40-pm-1024x633 Back to the scatter plot, in this example based on [Region], [Sales] and [Profit], like so: 11 Telling Tableau to plot every single profit and sales value including the respective region is not a particularly hard thing to do, but as you probably have correctly guessed I have a screenshot for that, too. Here we go! 12 Now watch as the marks count on your scatter plot skyrockets. This happens because by unticking the “Aggregate Measures” option you changed the way Tableau queries the database. Instead of returning the sum of profit and the sum of sales, the database now delivers every single value for profit and every single value for sales to Tableau: 13   And the query behind this visualization looks like this: 14   As you can see this query does not have a group by clause, which leads the database to return every single value for region, sales and profit. Feel free to experiment with your own data and Tableau’s view data feature. Pretty simple, right? Time to move on to the next topic:

Simple Filters

All Tableau users will sooner or later have used a filter, so let’s take a closer look at how they work. Filters can be easily created by either dropping dimensions or measures alike onto the filters shelf and setting which members of a dimension or values are allowed to show in your visualisation. Tableau then adds them to the query it runs against the database to limit the returned results as desired. Those filters can apply both to data on row level as well as aggregated values, as depicted on the image below, and in this chapter I will explain how that all comes together. Screen-Shot-2015-07-24-at-12.49.55-pm-1024x629 First up: filters on dimensions. Let’s assume we want to see the profit and sales for one Region only, for example East. Tableau smartly generates a new query that looks like this: 15 As you can see Tableau added a where clause to the query that tells the database to scan through the regions and only return results for the East region. But what is so smart about this? Tableau will try to minimize the work the database has to do and will adjust the query accordingly. That way, when we want to see the results for all regions but the East region, it doesn’t tell the database to include the regions North, South, West and Central, but instead tells it to exclude the East region: 16   Not bad, huh? Now that we’ve covered a filter on dimensions, how about those filters on measures? We might want to see all the regions, where the sum of profit and the sum of sales are above or below a certain threshold. Simply drag the measures on the filter shelf and set the measures to be at least or at most a certain value and hit okay. 17 The matching generated query reads as follows: 18   See how Tableau left out the where clause but instead wrote a having clause which sits after thegroup by bit? This happens by necessity as integral part of all SQL flavours and Tableau takes this load off your mind, allowing you to focus on the analysis at hand. And if you want to filter for measures in a specific range, just set the filter to a range of values and Tableau will translate this range into a having clause: 19   Great, you have made it that far. That wasn’t too complicated, right? How about taking that little break you have been putting off? We’ll be right back with the second blog post of this series featuring more interesting insights around data segmentation using Tableau’s sets, table calculations and Tableau 9’s brand new Level of Detail calculations.   All generated queries have been tracked using the Performance Analyzer, one of the tools available with the InterWorks Workbook Tools Suite that was created to make batch and automatedinteractions with Tableau workbooks possible. Did you find this useful? Let us know in the comments section and keep tuned for more blog posts covering the most interesting topics on Tableau. If you have any questions, please feel free to send me an email or, if you’re ready to download Tableau, click here to get started on a free 14 day trial!

More About the Author

Fabian Zimmer

Senior Tableau Consultant
Tableau In, SQL Out: The Tableau Data Lifecycle – Simple Aggregates, Disaggregate Measures & Simple Filters Tableau is a great piece of software geared towards business users and rapid, iterative data analysis. Its interface makes it as easy ...
Tableau Conference On Tour – Next Stop: Munich! Is there a better way to share your passion for Tableau, learn new things by the hour and touch base with the community than visiting a ...

See more from this author →

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072

×

Love our blog? You should see our emails. Sign up for our newsletter!