How To Use Distance And Buffer Calculations In Tableau Using Tableau Mapping Functions to Find Barbecue in Kansas City You will learn how to (1) use the MAKEPOINT function to create geographic points from custom latitude and longitude pairs, (2) compute distances between points with the DISTANCE function, and (3) draw a parameterized buffer around a point with the BUFFER function. Related dataset: https://playfairdata.com/bbq Calculated field to create custom geographic points: MAKEPOINT([Latitude],[Longitude]) Calculated field to compute the distance between custom geographic points and a hardcoded geographic point: DISTANCE([Points],MAKEPOINT(39,-94.63),’miles’) Calculated field to compute a radial buffer around a hardcoded geographic point: BUFFER(MAKEPOINT(39,-94.63),5,’miles’)

How To Use Distance And Buffer Calculations In Tableau

Using Tableau Mapping Functions to Find Barbecue in Kansas City

You will learn how to (1) use the MAKEPOINT function to create geographic points from custom latitude and longitude pairs, (2) compute distances between points with the DISTANCE function, and (3) draw a parameterized buffer around a point with the BUFFER function.

Related dataset:
https://playfairdata.com/bbq

Calculated field to create custom geographic points:
MAKEPOINT([Latitude],[Longitude])

Calculated field to compute the distance between custom geographic points and a hardcoded geographic point:
DISTANCE([Points],MAKEPOINT(39,-94.63),’miles’)

Calculated field to compute a radial buffer around a hardcoded geographic point:
BUFFER(MAKEPOINT(39,-94.63),5,’miles’)

Hi, this is Ryan with Playfair Data TV. And in this video, I’m going to show you how to use the Distance and Buffer functions in Tableau Desktop.

For this example, we’re going to have some fun and look at the distance and I’ll show you later on, a buffer, between Playfair Data’s office in the suburbs of Kansas City, Missouri, and the location of my 10 favorite barbecue restaurants in the area.

So if you’d like to follow along, I’ll share a link to this dataset and the related content below the video. Here’s how it’s going to look over in Tableau Desktop if you’re following along.

The one big pitfall with both the Distance and the Buffer functions are, not necessarily a pitfall, but just something that you should know about is you will need your own Latitude and Longitude columns in the underlying data in order to use these.

That is because both the Distance and the Buffer functions need to use a third spatial function, which is called Makepoint. And that Makepoint function has to be used in a calculated field. Unfortunately, as generous as Tableau is with their generated Latitude and Longitude fields, they cannot be used in a calculated field, which is why you need your own Latitude and Longitude combinations in the underlying data.

So notice in my dataset, I’ve got both here. These generated versions will come over automatically as long as I have a field in my dataset that Tableau recognizes as being a geography, such as State, City, or Zip Code. That’s why I see those.

But these are the versions from my underlying data. Those are hardcoded measures that actually exist in the dataset. As long as you name those measures Latitude and Longitude, Tableau will recognize them and automatically apply the correct geographic role to them.

If you’re using an odd abbreviation or some other name that Tableau does not recognize, you can still use it. But there’s one extra step involved. You’d have to right-click on the Latitude or the Longitude field, hover over Geographic Role, and then assign it the Geographic Role of either Latitude or Longitude.

But mine was recognized automatically. Because I gave it a nice, intuitive name. And I’m ready to start using it. The first thing we need to do is create points out of every Latitude and Longitude combination.

I’ll show you one more time why you have to have Latitude and Longitude in your underlying data. Because if I were to go create a calculated field out of the generated version– so I right-clicked on Latitude, hover over Create, Calculated Field is not available to me as an option, only Parameter is.

But if I right-click on my version of it and hover over Create, I can make a calculated field. And I will call this Points. And the function that we’re starting with is called Makepoint. It’s one word. It’s spelled like that on the screen there. And then I’ll use an open parenthesis.

Anytime you see a function in Tableau that you have not used before or don’t recognize, you can click on it, and Tableau will look it up and this little data dictionary and give you an example of the syntax. So this says it returns a spatial object constructed from Latitude and Longitude.

So it’s going to take each pair and basically consolidate them into a single geographic point. And here’s the syntax– Makepoint, Latitude, comma, Longitude. So we’ve already got most of it.

I’ll go here after Latitude, type comma, and then type Longitude, close parenthesis. So those are all my points. That is the entire formula. I will click OK.

If you want to use these points, from here all you would have to do is drag that field to the Detail Marks Card. And Tableau will automatically create a map out of those points. It puts the Longitude field on the Columns Shelf, the Latitude field on the Row Shelf, and then it’s this Points field on the Detail Marks Card that tells Tableau where to plot those points, or which of those points should be plotted.

Before we get into the actual mapping, we’re going to start by using an example of the Distance function. And I’m going to make a bar chart that tries to figure out how close the Playfair Data office is located to my 10 favorite restaurants.

So I’m actually going to click Undo to clear the slate here. And I’m going to make a calculated field out of my Points calculated field by right-clicking on it, hovering over Create, and clicking Calculated Field. And I will call this one Distance.

And it starts with– you guessed it– a function called Distance. Again, let’s click on it and take a look at what Tableau’s doing. It says, this returns the distance between two points. And here’s the syntax. Distance, Makepoint– it has one location, comma, Makepoint– it has a second location, comma, and then the units.

So we’re trying to compute the distance in different units. Allowable units are meters, kilometers, miles, and feet. Let’s say for the purposes of this analysis, I want to compute the distance in miles.

To do that, I’m going to have my points, comma, a second location– this one I’m going to hardcode with the Latitude and Longitude of Playfair Data’s office, which is 39, -94.63, close parenthesis, comma.

Let me make this box a little bit wider so we can see what’s going on. After that second comma, I typed the units in. Miles happens to be the one anomaly in the group of allowable units in that you have to type out the whole name.

So I typed miles. It’s lowercase and then close the tick mark. For meters, kilometers, and feet, you can either type out the whole thing in lowercase lettering, or you can use an abbreviation such as that example syntax you see there on the right with just km.

But that is correct for miles. I’ll close the parentheses. And there’s my entire formula. I will also share this in the related content below the video.

One more nuance with a Distance calculation. It doesn’t matter which of your points comes first. So I could put the hardcoded Playfair Data coordinates before my set of points. Those are my 10 other points for all the different barbecue restaurants– doesn’t matter which one of those goes first. Either one’s going to work.

I will click OK to close the calculated field dialog and make my bar chart. I will start by putting the Distance calculated field onto the Columns Shelf. And then I will break that down by the addresses.

And I’ll also put Location on here just so we know which restaurant is associated with each of those addresses. I’ll fit the entire view so that we can see this a little bit better. And I will turn my labels on so that we can see what those Distance scores actually are.

One more thing I’ll do is change the format and knock out a couple of decimal places. We’ll round to two decimal places. And let’s take a look at this.

One interesting thing to me is this row for Playfair Data. That’s not really relevant. But this is a nice, quick quality assurance step where I can double-check that I’m getting an answer of 0.

Because after all, I’m comparing the distance between Playfair Data’s office and Playfair Data’s office. That should be 0. And it is. So I have a pretty good sense that this is working.

Now that I’m confident in the calculation, I’ll probably go ahead and exclude that row. I will also sort these in ascending order. Because it’s better if the restaurant is closer to us. And now I can look at this and see that the closest locations are both Jack Stack Barbecue.

They’re 3 and 1/2 miles away each. Char Bar is the next closest at 4.07, and so on until you get all the way to a third Jack Stack location, which is the furthest away at 6.77 miles away.

So this is a great, quick analysis. If I were getting hungry for barbecue for lunch, and I wanted to figure out what’s the closest possible restaurant, and I don’t want to spend a lot of gas money, I could throw this together and see that I can go to either the Jack Stack north of us or south of us. They’re both exactly 3 and 1/2 miles away.

Now we’re going to show a second example and use the Buffer field to draw a radius around Playfair Data’s office. For this example, let’s pretend that I’m trying to figure out how many restaurants are within a five-mile radius of Playfair Data.

To start this one, I’m going to go back to my Points calculation and put that onto the Detail Marks Card, which will generate a map for me. But to get some extra flexibility with this map, I’m going to change the mark type from Map to Circle, which will temporarily give me one data point until I put the Address field on the Detail Marks Card. That way Tableau knows to look at all 11 addresses that I have and plot them on the map.

But the reason I changed the Mark-type from Map to Circle is, it gives me a little bit more flexibility in how I encode those symbols. Now that the mark type is Circle, I can click on Size, for example, drag this to the right, and those circles will get larger.

And that’s the left side of my map. To create this buffer radius– so what I’m going to do is draw a circle around Playfair Data’s office, which is right here right in the middle. To do that, I’m going to use a dual axis map.

I show you how to make this map on a separate video. So I’m going to quickly fly through how I do it here. But I like to hold the Control key while I click Latitude on the Rows Shelf and drag it right next to itself.

At this point, we’ve got two rows showing the same thing. On the top row, it’s a symbol map, Circle’s the mark type showing all 11 locations. Then we’ve got the same exact thing on the bottom.

But what’s special about a dual axis map is, now that I’ve added a second geographic measure to the Rows Shelf, they each get their own set of Marks Cards that can be edited independently of each other. So I’ll leave the top row as is but navigate to the second row– the Marks Cards, which control the second row. And I’m going to make a couple of changes here.

First of all, we are going to change the mark type back to Map in order to use this special Buffer function. And I’m going to now type– I’m going to make another calculated field that creates the buffer for me. I’m going to right-click in any blank space on the Data pane and click Create Calculated Field. And I will call this one Buffer.

And the formula this time will be the Buffer function, open parenthesis. Let’s take a look at what this one does. It says, it returns a buffer of the given distance around a point– example, buffer, a hardcoded Makepoint, a value, and then a unit.

So for our example, we’re saying we want to see a buffer of five miles around Playfair Data’s office location. So I’ll type Makepoint and then type in that hardcoded Latitude and Longitude combination again, close parenthesis, comma, 5 is actually the same value that I want to use. So I’ll type 5, comma, tick mark and then again, you have to type out miles in lowercase lettering and then close parenthesis.

So there is the entire formula. I’ll share that in the Related Content, click OK. I’m going to drag that to the Detail Marks Card. And we don’t see it show up yet. Because we don’t need this other granularity.

In fact, we don’t have that granularity with the Buffer calculated field because we only have a single data point– Playfair Data’s location. So I’m going to drag the Points field off. And technically, I can also drag Address off. And it will also stay.

The only thing we need is that one Buffer calculated field. And I’ll change the transparency of that buffer to maybe 35% so that we can see the underlying marks. So at this point, on the top row– symbol map with all of our locations. On the bottom row, a Map mark type that’s drawing a 5-mile radius around Playfair Data’s office.

To combine these layers, click on the second pill on the Row Shelf and click Dual Axis. This will make the second map lay on top of the first map. If we did this correctly, we should see Playfair Data’s location right in the center of this circle.

That’s the center. And that lighter-blue buffer is going at a five-mile radius all the way around Playfair Data’s location. So a quick analysis is telling me that we’ve got 1, 2, 3, 4, 5, 6 barbecue restaurants within a five-mile radius of Playfair Data.

To make this a little bit more effective and user-friendly, I’m going to parameterize that radius value, which you can do by right-clicking in the blank space in the Data pane– click Create Parameter. I’ll call this my Distance Value.

I’ll leave it at Float but maybe click the Range radio button and put a range of values in here. So I’ll say that the user can pick between 1/2 a mile and maybe 6 miles. And I’ll click OK.

I will show that Parameter Control. Maybe bump this back up to– actually, let me edit that one more time and give this a Step Size. There’s a few too many options in there when I chose a data type of Float. So a Step Size of 1/2 a mile. Now let’s click OK and see how this looks– much better.

I’m going to move this Distance value to 5 so that it’s the same as it currently is. But then I’m going to edit my Buffer calculated field and replace my hardcoded 5 with the Distance Value parameter.

So this step is optional. But we are parameterizing that Distance value from a hardcoded 5 to a parameterized 0.5 all the way up to 6. If I did this correctly, we shouldn’t see anything on the view change. Because we’re essentially replacing the value of 5 that was already there with the value of 5 in our Distance Value parameter.

I’ll click OK to see if that’s right. The good thing is nothing changed. But now I can use this cursor on the Parameter Control to change the size of that radius all the way down starting at 1/2 a mile.

So if it’s a 1/2 a mile, there’s nothing even close to Playfair Data. But as I start to expand that circle, if we did this correctly, we should see the first two restaurants at a 3 and a 1/2 mile radius. Because remember in our Distance example, both of these Jack Stack locations were exactly 3.5 miles away.

If I look on the map, sure enough. It looks like this is also working. There’s a Jack Stack. Let me put the location on the tooltip so that we can make sure and quality-check our work.

So there’s a Jack Stack north of us and a Jack Stack south of us. And they’re exactly 3.5 miles away. If I bump this out to 4, we see that we’re almost to the third-place restaurant but not quite, which also seems correct.

Because I remember the Char Bar was 4.07 miles away. So our buffer right now is 4 miles, Char Bar is just on the outside of that buffer at 4.07. I can keep going all the way up to 6. Because those are the values that I allowed in that parameter that I created.

And that captures 9 out of 10 of my favorite barbecue restaurants. There’s one outlier. That’s the Jack Stack. That’s far north of us.

And if I look on my Distance example, sure enough, that Jack Stack is 6.77 miles away. This has been the Distance and Buffer functions in Tableau.

I’m Ryan with Playfair Data TV – thanks for watching!