Skip navigation

While it has long been possible to use calculated fields and parameters in Tableau to calculate distance from specific locations in Tableau, the experience wasn’t great for keeping a user in the analytic flow.  The process was largely driven by clicking on a huge parameter drop down and hunting around for the right origin location.

 

Using a drop down, like the one below, to select locations on the map is not an awesome, intuitive user experience. 
Why can’t I just click on the point of origin on the map??
With set actions, you can now build rich analytic distance calculations with a simple click on the map.
How many of the points on the map are within a distance of this point right here?
2018-11-13_10-41-30.gif

 

 

While this post explores set actions for dynamic distance calculations there are so many cool uses to explore…for instance:

 

One of these days I'm going to look into combining Rody and Jeffrey's work with my example in this post to do the distance select set action a dual axis map with both the radial circle for visual demonstration of distance AND the corresponding set to show in/out of the distance buffer.  It's going to be awesome. 


 

How does the magic of dynamic distance calculations with set actions work?

There are a few steps to setting this up.  I’ll walk through them here, and you can download a demo workbook from Tableau Public to explore in more detail.

 

1. Make a set to define the origin of your distance calculations
We need a set that tells us which point on the map is our origin.  This set will only have one location and once it’s created, we’ll use a set action to update it dynamically.  This is most of the magic behind the scenes.

So, step 1 – just make a set.  Select one feature to start with a pre-selected location. In the example below, my selected facility is being defined by the ID, and #9589 is selected.


 

2. Assign the latitude and longitude of the origin location (from the Set in step #1) to every row in your dataset.

 

In order to calculate distance between the selected origin and the other locations in your dataset, we need to make sure that the location of the origin is available to use in calculated fields. We can do this with two quick calculated fields – one for latitude and one for longitude.  Here is the example calculation for latitude:

 

{ MAX(if [1) Selected Facility] THEN [Latitude] END) }

 

This just uses a level of detail (LOD) calculation to get the maximum value for latitude for all items in the Selected Facility set (created in Step #1) and then assign it to every row in the table.

 

The calculation for longitude will look pretty much just the same…just update the measure that is assigned in the calculation from Latitude to Longitude.

 

 

3. Calculate distance from selected origin location

Now that every row has data for latitude and longitude for both the row-level location AND the selected origin location, we can do a quick distance calculation between the two for every location in our dataset.

 

     IIF([Distance Unit] == "Miles", 3959, 6371) * ACOS(

SIN(RADIANS([2) Latitude of Selected])) * SIN(RADIANS(([Latitude]))) +

COS(RADIANS([2) Latitude of Selected])) * COS(RADIANS(([Latitude]))) *

COS(RADIANS(([Longitude]))-RADIANS([2) Longitude of Selected]))

)

 

This is a basic spherical distance calculation with a tiny bit of calculations bling added in just for fun – it adds in a parameter-driven toggle ([Distance Unit]) that allows the user to set distance calculations to be either in Miles or Kilometers.  That’s the IIF at the start – if the parameter is set to ‘Miles’ use the earth radius in Miles (3959 miles), and if not, use the earth radius in Kilometers (6371 kilometers) when calculating the distance between locations.  This is totally un-necessary, but it’s nice if you’re making a visualization for people in locations that use different measurement systems.

 

4. Set up a set action to change origin on click!

Now that every location in the dataset has a calculation for how far it is from the selected origin, we just need to make it easy to change which location is used as the origin. Set actions to the rescue!

We start by setting up a new Action on our worksheet
          

In the Actions dialog, add an action to Change Set Values:

Give it a good name (I used ‘Update origin location’)


Pick the worksheet(s) that you want to use to drive changes to the selected origin (I’m just using my Location Map worksheet)

 

Pick the Target set data source (I only have one datasource, so select that one…)

 

Pick the set that will be updated based on the selection (We want to update the set from Step #1 – the Selected Facility)

 

And…when clearing the selection, let’s ‘Keep set values’ – that makes it so that the origin location will persist even when the user interacts with other parts of the viz.

 

The basics are now all set up!  If you interact with the map, the origin location will change and the distance calculations will update accordingly.  BUT, there isn’t yet any visual indicator that something has changed (unless you have the calculated distance field visible on the tooltip and are paying close attention).

 

In the two images below, I show the result from clicking on two different locations on the map...but with the tooltip showing for the same location (not one of the two selected points.  The only way that you can tell the difference between the two is that the distance updated on the tooltip (both show the distance from the selected point to Facility ID 15875) - the difference in distance is because I clicked on a different location on the map each time (which updated the origin location for our distance-based set action):

 

In the next steps, we’ll update the symbolization and add a ‘within distance’ check to indicate whether each location is inside or outside our preferred search radius.

 

 

 

 

5. Symbolize – origin vs. not origin

We’ll start with a simple symbolization to show selected origin location vs. everything else.  I use a calculated field to give me a quick True / False for whether a point is the selected origin.

 

[1) Selected Facility] = TRUE

 

If I drop this on the color and/or size shelf I can adjust the symbolization so that the selected origin stands out.  I can emphasize even more if I use the same field on Color, Size, and/or Shape to customize even more:

 

 

Color encoding only (selected is red)

Color (red), size (selected is larger), and shape (selected as star)

 

 

6. Create a calculated field for points inside and outside a specified search distance


While the distance calculation by itself is useful, sometimes the question isn’t just how far away is it, but what is nearby?  In order to assess what is near vs. far, we need to compare our distance calculation to the threshold for nearby.

 

You can do this by hard-coding the distance value of interest, or set a parameter so that the calculation updates automatically as the distance threshold is changed.


Before we make our new set, we’ll start with a calculated field to identify whether each point is inside or outside the distance threshold.  This uses a pre-made numeric parameter, but could just be a number (e.g., if [3) Distance from selected] <= 1 ]

 

IF [3) Distance from selected] <= [Max distance]

THEN 1

ELSE 0

END

 

 

We can use this calculated field for all sorts of symbolization or filtering (e.g., just show the locations within the specified distance, or encode so that the locations within the distance are a different color than the other locations farther away).

 

Or, using this calculated field, we can quickly create a new set that will allow for quick filtering to the top N values (e.g., if you want to narrow down to the 10 closest points)

 

 

 

 

7. Build up an awesome dashboard to explore your data.

 

Using the steps above, I built a quick dashboard to explore store locations in Manhattan.  I wanted to run off some analytics for all stores within a specified distance of the selected store.

 

This dashboard shows all stores within 1 mile (in yellow) of the selected store (red star).  The selection then drives a few bar charts that show characteristics of the top 5 stores by employee count and by distance.

 

 

But, this is just one example – the sky is the limit with set actions!