Site Catchment Analysis with Tableau 6

Use Parameters and Table Calculations in Tableau 6 for a Site Catchment Analysis of Germany

© R.-B. / pixelio.deFor a recent post called Calculated Fields in Tableau I used a site catchment analysis of Germany to explain how to use and take advantage of Calculated Fields in Tableau. The example posted back then was produced with Tableau 5.2.

The main disadvantage of that workbook is the fact that you have to change a constant in a Calculated Field if you want to switch to another center location for the site analysis. Inconvenient and even impossible if you only have the Tableau Reader available.

With version 6, available since November 10, 2010, Tableau introduced an extremely powerful new feature: Parameters. Parameters are user-defined constants that can be used in Calculated Fields. They can be entered by the user of the workbook either on worksheets or dashboards, are valid throughout the whole workbook (e.g. they are global) and can be used in any Calculated Field. Thus, it wasn’t far to seek that with parameters Tableau 6 would easily overcome the main drawback of the Tableau 5 site analysis workbook.

Today’s post shows how to transform the site analysis workbook to Tableau 6 using parameters. As usual including the Tableau Public workbook for direct access and free download.

The Idea and Implementation of the Tableau 5 Site Analysis

The basic idea of the site catchment analysis was calculating and visualizing the German population living within any given distance (“the radius”) around any given postcode (“the center”) as the crow flies.

The implementation used different Calculated Fields. The most important ones retrieved latitude and longitude of the selected center from the source data using Tableau’s pass through functions (RAW_SQL) and a mathematical formula for calculating the distance between the center and all other locations using the spherical law of cosines.

You will find a more detailed description of all Calculated Fields in the original article Calculated Fields in Tableau.

The Drawbacks of the Tableau 5 Site Analysis

The Tableau 5 workbook came with 3 major disadvantages

  1. First and foremost you couldn’t easily switch to another center location. The postcode of the center was defined as a constant in a Calculated Field. If you wanted to see the results for another postcode, you had to edit this Calculated Field.
  2. The pass through functions (RAW_SQL) made it impossible to publish the workbook on Tableau Public, since Tableau Public does not support workbooks using Custom SQL statements.
  3. An additional disadvantage appeared after the introduction of Tableau 6: the Tableau 6 data engine does not support RAW SQL statements on the Tableau Extract. In other words, you can still use the existing Tableau 5 workbook with Tableau 6, but you cannot refresh the extract.

Well, number 2 and 3 may or may not be a problem. However, disadvantage number one was really an obstacle.

The Implementation in Tableau 6

Here are the main changes in the Tableau 6 version of the workbook:

Parameters

The first idea for transferring the workbook to Tableau 6 is obvious: take advantage of the fantastic new Tableau feature “Parameters”.

Instead of using a constant in a Calculated Field, we create a parameter called [Center] based on a list of values coming from another Calculated Field called [PLZ5_str] (the postcode). Since we are talking about more than 8,000 postcodes, we set the parameter control to a Type-In-control. After creating this parameter, the rest is a piece of cake: simply replace the original constant Calculated Field [Center_PLZ5] by the new parameter in all Calculated Fields. Parameters can be used in Calculated Fields just like fields from the source data or other Calculated Fields.

In the Tableau 5 version we used an at-most-filter to define the radius of the site analysis. That was fine, but we will use a parameter for the radius in the Tableau 6 version as well. Create a new parameter called [Radius], set the data type to Integer, set allowable values to Range and define a minimum of 0 and a maximum of whatever you find appropriate. Later on we will use this parameter in another Calculated Field and get rid of the global filter [Distance] used in the Tableau 5 version.

Table Calculations instead of RAW SQL pass through functions

Replacing the RAW SQL functions by table calculations requires a complete new logic of the Calculated Fields used in the workbook. There is one intermediate step necessary to get from the latitude and longitude in the source data to the geographical data of the center:

[Lat_Center]:

IF [PLZ5_str] = [Center] Then
[Latitude]
Else
0
End

This Calculated Field assigns the latitude only to the center location. All other locations get a zero from this Calculated Field. [Long_Center] is defined accordingly.

[Lat_Center_Global]

TOTAL(SUM([Lat_Center]))

This Calculated Field is aggregating all results of the defined field [Lat_Center], i.e. it is summing up the latitude of the center and a lot of zero values (all other locations). Again [Long_Center_Global] is defined accordingly.

We now have the values of the latitude and the longitude of the center in 2 Calculated Fields without using a pass through function (RAW SQL).

Next step is changing the Calculated Field [Distance].

[Distance]

ROUND(6371 *
ACOS(SIN(RADIANS([Lat_Center_Global])) *
SIN(RADIANS(SUM([Latitude]))) + COS(RADIANS([Lat_Center_Global])) *
COS(RADIANS(SUM([Latitude]))) *
COS(RADIANS(SUM([Longitude])) – RADIANS([Long_Center_Global]))),2)

Please notice that you have to aggregate the latitude and longitude of the locations coming from the source data to make this function work. [Lat_Center_Global] and [Long_Center_Global] are already aggregations (see above) and you can’t mix aggregate and non-aggregate functions in one calculation.

And here is exactly where we bring a limitation into our workbook. This is working fine as long as you are on the lowest aggregation level, i.e. on the level of [PLZ_5]. A further aggregation is not possible, since we are already aggregating on this level. See below for the resulting consequences from this limitation.

The next step is defining a Calculated Field to be used as a filter:

[Is_Within_Radius]

IF [Distance] <= [Radius] Then
1
Else
0
End

This function returns a 1 if the location is within our defined distance from the defined center. Otherwise it returns a zero. We will use this Calculated Field as a filter on our worksheets to only include the locations in our views that are within the radius.

Finally we define a Calculated Field to sum up the covered population:

[Covered_Population]

SUM([Population])*[Is_Within_Radius]

That’s it. 

All other Calculated Fields ([PLZ_str], [PLZ5_str], [Location_type], [URL_link], [Map_Population]) stay as they were, except for the fact that the parameter [Center] is used instead of the Calculated Field with the constant postcode.

Changes to the Dashboard

The new logic described above requires some changes to the dashboard of the workbook as well. Instead of the filter to set the radius we have now 2 parameter controls on the dashboard.

The logic of aggregating the data already on the level of latitude and longitude of the center makes it impossible to do further aggregations of the data. Thus, the views showing the results by state and by PLZ_2 (first 2 digits of the postcodes) had to disappear from the dashboard.

I can live with that, but I am truly unhappy with the fact that I failed hard in trying to create the simple 3 numbers on top right of the Tableau 5 dashboard, showing the main results of the analysis (total population covered, population covered in % of total population Germany and covered locations). From my point of view this is a major disadvantage of the logic implemented in the new workbook.

As I said, “I failed hard”. Maybe I am missing something and you have a better idea (see below).

The Workbook on Tableau Public

Here is the workbook on Tableau Public for direct access and free download:

 

The Drawbacks of the Tableau 6 Version

As already implied in the sections above, the Tableau 6 version of the site analysis has some disadvantages as well:

  1. No further aggregation of already aggregated measures
    In the Tableau 6 workbook, latitude and longitude of the center are calculated using aggregation functions. The Calculated Fields [Distance] and [Is_Within_Radius] are based on the results. Thus, it is not possible to do further aggregations. All views have to be based on the deepest level of aggregation (i.e. PLZ5_str). A simple overview, e.g. of the sum of the population covered or the sum of the locations covered is not possible.
  2. No triggering of an action from a parameter
    It is not possible to trigger an action from the change of a parameter. Thus, if you change the center location using the parameter [Center], the Google Map View will not be updated. The user has to click on the center on the map to update the view in the web page on the dashboard. 
  3. The performance
    I have to admit that I haven’t made any reliable performance comparisons of the two versions. However, I do have the impression that the Tableau 6 workbook is slower than the Tableau 5 version. From my point of view this makes sense: The version 5 workbook uses 2 simple RAW SQL statement to retrieve the latitude and longitude of the center from the source data whereas the version 6 workbook calculates the center’s geographical data for all data rows using table calculations. This different logic suggests the assumption that the logic in the Tableau 6 workbook needs more time for computation.

Ideas anyone?

To be honest, I am quite unhappy with the drawbacks of the Tableau 6 version, especially regarding the aggregation problem mentioned above. I liked the display of the 3 simple total numbers at the top of the Tableau 5 version, showing the total covered population (absolute and as a percentage of Germany’s total population) and the numbers of postcodes covered within the defined radius at a glance. In my humble opinion these three dashboard elements provided a very good quick overview of the main results and they are sorely missed in the new version.

I would have loved to include them in the Tableau 6 version as well, but I failed hard. It might well be that I do not see the wood for the trees and missing something quite obvious.

Do you have any ideas on how to overcome this major weakness of today’s showcase? If so, I would highly appreciate if you would take the time to leave a comment and tell me about it. Many thanks!

Acknowledgement

What do you think? Who helped me this time? No surprise at all. The usual suspect again: many thanks to Giedre Aleknonyte for spending some of her precious time meticulously reviewing my workbook and for her great feedback. Thanks Giedre!

What’s next?

I should probably stop writing about what’s supposed to come next here, because I am very often failing in keeping my promises. For instance, at the end of Calculated Fields in Tableau I announced some more Excel articles here.

This time I will, for sure. Thus, for all you Excel aficionados coming here, please stay tuned. There are some interesting Excel models in the pipeline, among other things an Excel version of the site catchment analysis discussed today.

Stay tuned.

Comments

19 responses to “Site Catchment Analysis with Tableau 6”

  1. Joe Mako Avatar
    Joe Mako

    The reason you cannot calculate the numbers you want currently in Tableau 6 with custom table calculations is because you cannot use the result of a table calculation to partition another table calculation. Hopefully in a future release this ability will be added, or the ability to write a pass through function in Tableau’s unique language for their Data Engine.

  2. Robert Avatar

    Joe,
    many thanks for your comment.
    I understand. It took me quite a while until I figured out why I couldn’t get the results I wanted. You got it right off the bat. Thanks for the explanation.
    To be honest, I am already sorely missing the pass through functions. Very often they have been the last resort for the more complex workbooks.

  3. James Baker Avatar

    Thank you for the excellent post, Robert. I’ve passed along your feedback to several people here within Tableau.

  4. Richard Leeke Avatar
    Richard Leeke

    Good example.
    I agree about the drawback of the loss of RAWSQL – I was on the technology preview for the data engine and that was the number one drawback – in fact pretty much the only drawback – as far as I was concerned (but outweighed many times over by the speed benefit). I raised the same suggestion Joe mentions about exposing an interface to the data engine’s own language, but having discussed it with the Tableau folk at the conference I think that’s very unlikely to happen – and on balance I would agree that that isn’t the right way to go.
    I have also been running into the same restrictions that you and Joe have commented on about what you can do with the results of calculated fields. Hopefully some of those restrictions might be able to be lifted in future.
    On your comment on the performance of the calculated fields, I have also seen examples where calculated field performance is very slow – particularly with some of the new table calculations. One tip is to steer clear of the WINDOW_xxx functions on large result sets if you can (I’m not sure if you are using those – I haven’t had a chance to look at your workbook yet because it just times out before I get a chance to download).
    Again, I don’t know if this tip will be any use in your example, but one trick I have used to get around the multiple level of aggregation issue with extracts is to create a second connection back to the same extract and use data blending to get summary values. That works very well and very fast with v 6.0 data extracts. You do get a prompt telling you that you are using blending to join two connections to the same data source and that you should use a join, but if you say “OK” – which I treat as meaning “If only” 😉 – you can go ahead and do that.
    If I actually manage to download your workbook I’ll take a detaild look and see if any of the above comments actually apply in this case.

  5. Dean Glasener Avatar
    Dean Glasener

    I learned a great deal from your work, and Joe’s help in directing me to your site.
    Thoughts on your point #2
    “2) No triggering of an action from a parameter
    It is not possible to trigger an action from the change of a parameter. Thus, if you change the center location using the parameter [Center], the Google Map View will not be updated. The user has to click on the center on the map to update the view in the web page on the dashboard. ”
    The new features have opened a great power window in Tableau – in the rush of new ideas – onClick, onChange, onEvent and being able to express all Tableau surfaces as Objects – with the ability of the developer to refine and redraw the Tableau in response to many events: onWatchEvent, onListenEvent, onParameterChange, ….
    I’ve come to Tableau from Coldfusion – I’m a TABLEAU user after 14+ years of CF. I found CF a breath of fresh air in Nov 1996 – and found the same love for Tableau in Aug 2010.
    Tks

  6. Robert Avatar

    Dean,
    many thanks for your comment and the appreciation. I am glad you like my blog.
    I fully agree. I hope you didn’t get me wrong. I truly love working with Tableau and I didn’t want to sound off. However, if you are pushing Tableau to its limits and you find some things that could be improved, I consider it a good idea to talk about it.
    Tableau is a great product. However, better is the enemy of good, isn’t it?

  7. lucas Avatar

    Hi Robert,
    Congratulations on your Blog. It is very inspiring. I tried replicating your approach with one of my visuals, but I don’t seem to get consistent results when i change the radius. For example, when i select a higher radius, i see marks appearing that are closer to the center point that did not render when the radius was smaller. I published it to Tableau public in hope that you can suggest any issues. http://public.tableausoftware.com/profile/#!/vizhome/Book1_1644/Dashboard1
    thank you very much

  8. Robert Avatar

    Lucas,
    thanks for your comment and your kind words.
    I had a quick look. It seems to be a data problem. Compare the values of Lat and Long in your data with Latitude (generated) and Longitude (generated) Tableau provides with the geographical dimension “Zip”. Some are the same, some are different. You are using filled maps and for filled maps Tableau uses the geographical information provided in Latitude (generated) and Longitude (generated). The calculated field distance, however, uses Lat and Long from your database. I am not sure, but I guess this is the root cause of the issue.

  9. Alexander Mou Avatar

    Robert, seems both v5 and v6 dashboard can’t display google maps.

  10. Robert Avatar

    Alexander,
    you are right. If you download the Tableau workbook and click on a data point on the Tableau Map you receive an error message in the web object of the dashboard.
    It is not a Tableau, it is a Google issue. Apparently Google nowadays requires a mandatory API-key which has to be specified in the URL. This wasn’t the case back in 2010, when I published these articles.
    You can get an API key from Google for free and you would then have to change the Calculated Field URL_link to include this key. Have a look here:

    Google Maps Embed API

    Surprisingly the Google map seems to be still working fine on the dashboard published on Tableau Public in the article.

  11. Alexander Mou Avatar

    Could you get a key and save the effort for everyone else?

  12. Robert Avatar

    Alexander,
    I am sorry, no.
    Google says: “Your key […] ensures that Google can contact you about your website/application if necessary.”
    If you want to use Google maps in your workbooks, you should create your own key.

  13. Alexander Mou Avatar

    I see. Thanks!
    Is there a way to get a hold of the original data in csv or xsl?
    Thanks!

  14. Robert Avatar

    Alexander,
    download and open the workbook, go to a worksheet, remove the filter, right click somewhere in the visualization, select View Data, switch to the tab Underlying and click on Export All.

  15. Alexander Mou Avatar

    Unfortunately, not everyone has the luxury of a Tableau desktop.

  16. Robert Avatar

    It is exactly the same procedure with Tableau Public.

  17. Alexander Mou Avatar

    Tableau public can’t open your workbook.

  18. Robert Avatar

    Right click on the file and open it with Tableau Public.

Leave a Reply

Your email address will not be published. Required fields are marked *