Last Modified Date: 23 Oct 2017
Option 1: Tableau Desktop 10.0 and newer versionsBeginning with Tableau Desktop 10.0, you can use the radial tool to measure distance in a map view. For more information, see Measure Distances Between Data Points and Locations in a Map.
Option 2: Use Latitude, Longitude and Custom SQL
Step 1: Configure the Custom SQL
- Download the attached workbook, distance_example.twbx, and open it in Tableau.
- Click OK on the Import Data Source alert and Yes on the Custom SQL alert.
- The example data file is named distance.xlsx. It contains a worksheet called Sheet1, which lists the latitude and longitude for Seattle WA, San Mateo CA, and Bend OR.
- In the Data pane, right-click Sheet1 (distance.xslx) and select Edit Data Source.
- Click the down arrow on TableauSQL and select Edit Custom SQL Query.
Note: When using your own data, you save a step by selecting Custom SQL when you connect to the data source. For more information, see Connect to a Custom SQL Query.
- The example data source has three fields: Location, Lat, and Long. Create an inner join on a second instance of the table, where the locations from the two instances are not equal.
In the Edit Custom SQL dialog box, type the following query (or copy it from here and paste it), then click OK:
[Sheet1$].[Location] AS [Location],
[Sheet1$].[Long] AS [Long],
[Sheet1$].[Lat] AS [Lat],
[Sheet1b$].[Location] AS [Location2],
[Sheet1b$].[Long] AS [Long2],
[Sheet1b$].[Lat] AS [Lat2]
FROM [Sheet1$] INNER JOIN [Sheet1$] [Sheet1b$]
ON ([Sheet1$].[Location] <> [Sheet1b$].[Location])
Step 2: Create the View
- Select Worksheet > New Worksheet
- In the Measures pane, right-click Long and select Geographic Role > Longitude.
- Do the same for Long2.
- Drag Long to Columns and Lat to Rows.
- Place Location on Detail on the Marks card.
- Select Analysis > Create Calculated Field.
The below calculation uses the Great Circle distance formula to balance complexity with accuracy, and uses the average radius of the Earth.
- Name the field Distance. Enter the following formula, then click OK:
3959 * ACOS
SIN(RADIANS([Lat])) * SIN(RADIANS([Lat2])) +
COS(RADIANS([Lat])) * COS(RADIANS([Lat2])) * COS(RADIANS([Long2]) - RADIANS([Long]))
- On the Marks card, select Line.
- Place Distance on Color
- Place Distance on Label.
Step 3: Position the Labels
- On the Marks card, on the drop-down menu for Distance, select Dimension.
Now the labels appear in two locations, one on top of the other.
- Right-click the Seattle number and select Mark Label > Never Show. Do the same for the San Mateo number.
Now only three distance numbers remain: one at San Mateo and two at Bend.
- Select the number at San Mateo and drag it to the midpoint of the line between San Mateo and Seattle.
- Select the larger number at Bend and drag it to the midpoint of the line between Bend and San Mateo.
- Select the remaining number at Bend and drag it to the midpoint of the line between Bend and Seattle.
- Right-click the number, select Format, and in the Format pane, select formatting options to make the numbers more apparent on the map.