Update: In version 2021.4, Tableau allows you to add map layers from multiple spatial data sources with very little hassle. The update means you no longer have to combine disparate data sources as suggested in this post. If you have 2021.4+, see Rowan’s post here. If you have a version between 2020.4 and 2021.3 or below, use the guidance below.
The advent of map layers in Tableau was a giant leap forward in Tableau’s mapping capabilities. There are many great examples out on how to use them. Here are a few blogs that come to mind:
- Exploring the New Map Layers Feature in Tableau 2020.4 | InterWorks
- Tableau Map Layers | Dataviz.blog
- Build Custom Maps the Easy way with Multiple Map Layers in Tableau | Tableau
I want to use this post to clarify a common point of confusion I have seen with the basic process of adding multiple, separate spatial files as separate layers in the same map. When adding multiple spatial files to a single map, they must be combined into the same data source first.
This concept is very aligned with how Tableau functions, but it can be confusing to those coming from the GIS and mapping world, as GIS tools do not have this same requirement. Because the purpose of spatial data is often just concerned with locations on a map, there’s often no inherent relationship between spatial files. Sometimes, you just want to plot a bunch of layers onto a map without worrying about what key fields might relate them together.
For example, say you want to map the locations of historical tornado touchdowns, locations of population centers and locations of drive-in movie theaters. There is no immediate relationship between the three features, though you may find it interesting to overlay the three features on the same map:
Bringing Spatial Files Together
As one of Tableau’s main strengths is combining disparate data sources, it is no surprise that there are MANY ways to accomplish combining multiple shapefiles into one data source. Joins, spatial joins, unions, spatial unions, and relationships are all methods at your disposal. It can sometimes be overwhelming to know which method to use and unfortunately the answer is, “It depends!”.
Below, I outline common scenarios you may have and the method for combining the spatial files:
- Join or Relate: When you have key fields to join or relate the spatial files together
- Spatial Union: When the files have close or identical fields/columns and are of the same spatial unit (i.e. all points or all polygons)
- Outer Join on 1=2 or Outer Spatial Join: When there’s no clear relationship between the data and you just want to throw your spatial data on the same map
Do you have key fields that tie spatial files together?
Let’s say all three of your shapefiles have zip codes as a column, and it makes sense for your analysis to combine the three spatial files based on zip codes … Great! You can relate the shapefiles together using Tableau’s relationship model.
Generally, I recommend using the relationship model in this case instead of explicitly joining the tables using the physical model. For mapping purposes, this will allow you more flexibility in how you use the data source:
Do your spatial files have the same or similar fields and all the same spatial unit?
If so, a spatial union may make sense. Spatial unions are useful when combining spatial data to extend the analysis across either time or space.
For example, maybe you have point locations of UFO sightings in separate spatial files for the years 2018, 2019 and 2020 (i.e. across time). For each of these files, the geometry is represented as ‘points’ of the same phenomena (UFO sightings). Because the three files are showing the same phenomena, the columns in each file are likely the same as well, making a spatial union possible:
In the example above, we are using a spatial union to combine individual shapefiles that represent Census Tracts for Washington, Oregon and Idaho (i.e. across space). Because the files are essentially the same feature, just a different state, the table schema is identical and will work well in a union.
With this union, I have combined the three shapefiles into a single data source, which then allows them to be viewed and analyzed easily together on the same map.
Do your spatial files lack any reasonable key fields to relate them together?
Are you just trying to add layers to a map and do not know or have key fields that relate the files together? That is okay! This is common with spatial data as you often just want to display on a map, not necessarily join or relate it to other data.
For example, I have three spatial files:
- Portland Neighborhood boundaries (as polygons)
- Locations of grocery stores (points)
- Locations of farmers markets (points)
I simply want to display these three layers on a map in Tableau. But, because there is no relationship between these three spatial files, I cannot just join the files on key fields using a typical join or relate. Fortunately, there are still a couple of options for us:
Option 1: Combine Using a Full Outer Join on 1=2
The trick here, which I first encountered on Marc Reid’s dataviz.blog, is calculating two fields that will not join between the two tables – in this case, calculating a join on 1=2. This forces all records to appear in the results without needlessly creating duplicates and, most importantly, you will have access to all the geographic columns and objects, giving you full flexibility to map the objects. The result is essentially the same as a union, except this method guarantees all input columns will remain unique. For example, if each of my spatial files had a column named ‘geometry’, each of those will remain as separate columns with this approach. Marc Reid’s post describes this method well and also shows a fix for a filtering issue you may encounter with this method:
To make this join, use the Full Outer join type and add a new join clause, calculating 1 on the left and 2 on the right:
If you have more than two spatial files, you can join in the subsequent files using the same method but different values, e.g. 2 and 3.
Alternatively, you can also accomplish this same method using the new relationship model, using a custom calculation in the same fashion.
Option 2: Combine using an Outer Spatial Join (Intersects + Full Outer Join)
The final option is to use the new intersect join type (i.e. a spatial join) as a full outer join. To know more about the intersect option, see my introduction here.
With this method, Tableau will join records together from the three spatial files where they intersect in space. With the setup above, Tableau will join the records of Farmers_Markets and Grocery_Stores to Neighborhood_Boundaries, where those Farmers Markets and Grocery Store points intersect. Additionally, with the Full Outer join, records that do not intersect will also be brought in. Additionally, we are able to distinguish those features that do intersect from those features that do not intersect, which may be useful for analysis:
In the spatial join, use the ‘geometry’ columns, or whichever column holds the spatial information for file. The ‘intersect’ join option is the spatial join. The Full Outer ensures all records come through, even those that do not intersect:
Above shows my resulting map using the full outer spatial join to combine the three disparate spatial files and the new map layers feature to show them together in the same map.
Using a full outer spatial join has a major advantage over just the typical join.
The Advantage of a Full Outer Spatial Join
The spatial join allows further analysis capabilities because after a spatial join, Tableau knows the spatial relationship of the three files relative to each other in space, allowing you to do spatial analysis. For example, in my use case with grocery stores, farmers markets and neighborhood boundaries, I can count the number of grocery stores that exist within the boundary of a given neighborhood, a task I would not be able to accomplish with other join methods:
As you can see, as I hover over the neighborhoods, the tooltip shows the count of farmers markets and grocery stores within each boundary, made possible by use of the spatial join. This type of analysis would not be possible using a simple join or a relate.
One caveat: spatial joins only have analytically useful results with polygons and points that are ‘spatial joined’ together. For example, if you had three spatial files with points, the spatial join implementation here makes less sense as you cannot count the occurrence of one point inside of another point. You will notice that in my example, I am intentionally joining the point shapefiles (grocery_stores and farmers_markets) to the neighborhood boundary polygons to take full advantage of the spatial join.
This guide was intended to show the ways you can combine multiple spatial files into the same data source, especially if those spatial files have no notable relationship to each other. Perhaps in future iterations of Tableau, we’ll be able to add map layers from separate data sources. Until then, the methods described above should work!