Topics: 8 min readtime

Boost your mapping analyses with spatial functions in Tableau

Written by Jochem Brinckman
Tuesday 4 February, 2020

Introduction

As you all probably know doing analyses using mapping in Tableau is relatively simple. Lately, Tableau added some new functionalities called 'Spatial Functions' by which you can really improve your mapping analyses. And most importantly, they’re still easy to use!

These functions were initially introduced in version 2019.2 with the MAKEPOINT and MAKELINE functions and were expanded in 2019.3 with the DISTANCE function. For the upcoming version 2020.1 a new function will be released which I already tested out at the end of this blog post 😎. 

These functions can be found when you create a calculated field and select 'Spatial' in the drop-down menu. The MAKEPOINT function creates a point that is based on the latitude and longitude of a location. These points, also called spatial objects, can be used for the MAKELINE and the DISTANCE function afterwards. These two functions need two points as an input, a starting and an ending point.


Capture7

Spatial functions in a calculated field

 

When dragged into the view, the spatial fields created by MAKEPOINT and MAKELINE are automatically aggregated with the COLLECT function. This function can be used with spatial fields only. You can always disaggregate these fields such as shown in the first example below.

 

One thing to note is that Tableau doesn't allow the measures Latitude (generated) and Longitude (generated) to be used in a calculated field because these are no real data fields (they are generated on the fly by Tableau). In order to use latitudes and longitudes with a spatial function they have to be present in the data set.

In this blog post, two use cases will be given for the use of spatial functions. The first one will cover the different clients that are served by different warehouses in the port of Antwerp. Here, the use of the MAKEPOINT function will be shown. The second example will cover the top 5 domestic flight routes in the USA and will mainly focus on the MAKELINE and the DISTANCE function.

 

Example 1: Warehouses and their client locations 🚢

 

Capture

Warehouses and their clients in the port of Antwerp

 

In this example, we have a data source with the latitude and longitude of two warehouses in the port of Antwerp (second largest seaport in Europe in terms of freight volumes, based in Belgium). Each of these warehouses provides the same materials to their five clients in the area. From these clients the latitude and longitude are also in the data source. Like mentioned previously we need these latitudes and longitudes from the database itself and not the generated ones from Tableau. Here are the steps:

  1. Download the client data source.
  2. The first thing that needs to be done is to convert the coordinates from the warehouses and the clients into spatial objects with the MAKEPOINT function. Two calculated measures are created here, POINT clients and POINT warehouses.
    POINT clientsPOINT warehouse
  3. Once these functions have been created they can be dragged into the view. First, you drag one of the two calculated measures (let's take POINT clients) into the view and you'll see that Tableau automatically puts it on a map. You will also see that Longitude (generated) and Latitude (generated) are automatically put on the columns and rows bar.
  4. Go to the top menu, select 'Analysis' and make sure 'Aggregate Measures' is not selected.
  5. The next step is to make sure your columns and rows look like the image below. You can become this by just dragging Longitude (generated)  from the measures into the columns bar. Or by selecting the Longitude (generated)  from the columns bar while pressing the Ctrl-button and dragging it next to it in the bar.
                    Rows and columns
  6. In the Marks area there are two views now. Currently, they both have the same view with the calculated measure  you dragged into the view in step 2 (in my case it is POINT clients). Now, you replace one of these with the other calculated measure (POINT warehouses).
  7. In order to combine the spatial fields from both the warehouses and the clients in one map, a dual axis is needed. You achieve this by right clicking the Longitude (generated) pill on the right in the column bar and select 'dual axis'.
  8. The last step is making the view more clean by adding colors and changing the size of the dots. Drag the dimension warehouse ID on colors and make the size of the POINT warehouse measure bigger. 
Demo

Here you can see all the steps in one animation

 

Small conclusion: maybe the warehouse that is delivering client 4 should be reconsidered... 🤔 
A downloadable version of this workbook can be found here.

 

Example 2: Domestic flight routes USA ✈️

gifke2Top 5 airports in the USA by amount of flight routes
 

In this example we have a database that consists of all the origin airports with all its connected destination airports in the world. Each row in the database represents a flight route from one airport to another. The latitude and longitude from both origin and destination airports are included in the database. Here are the steps:

  1. Download the flight routes data source.
  2. The beginning of this example is the same as the previous one with the creation of the following calculated fields. For each origin and destination airport, a point is created.
    POINT origin airportPOINT destination airport
  3. These points will be used in the MAKELINE function. With this function geodesic lines are created between each origin airport and all its possible destination airports. A geodesic line appears with a curve when generated over a long distance because of the earth's curves. But over a short distance it will appear as a straight line.

    Line
  4. Putting these lines (or flight routes) in the view is very easy. You can just drag the calculated measure into an empty view. But because there are a lot of flight routes all over the world some filtering is needed to get rid of the chaos in the view. That is why I will only select the top 5 airports in the USA by the amount of domestic flight routes. Put the dimension Country Destination Airports and Country Origin Airports in the filter section and select 'Unites States' for both and add them both to context. And finally, put Name Origin Airports in the filter section and select the top 5 by Amount of Flight Routes.
  5. The last function that will be used is the DISTANCE function. Just like the MAKELINE function this function needs two points (an origin and destination) as an input. Don't forget to add the measurement unit at the end.
    Distance
  6. Next, this field can be dragged into a tooltip so when you hover on the flight route you can see its distance.

Captue 9Tooltip with the Distance function

 

A downloadable version of this workbook can be found here.

Conclusion

Using these spatial functions is actually very straightforward once you have a data source with the location coordinates in it. But having these data sources with the correct coordinates from all your clients, suppliers, etc will be the biggest challenge.

giphy

*Amazed by the spatial functions*

 

Coming soon...

In Tableau Desktop 2020.1 a new spatial function will be released named the BUFFER function. This will generate a buffer around a given distance from a point. Check it out here. I already tried it with the beta version to see which clients fall within a buffer of 5 km from each warehouse. 

 

Capture6

Buffer of 5km from each warehouse

 


Kopie van Jochem - Make Your own Table Calculations with Python in Tableau Prep Builder-1

Jochem Brinckman | Analytics consultant