Last Modified Date: 14 Mar 2023
Environment
Tableau DesktopAnswer
CLICK TO EXPAND STEPS
Step 1: Configure the Custom SQL
- Download the attached workbook, distance_example.twbx, and open it in Tableau. Please take note of the following information:
- You will need Tableau Desktop 2019.3.1 or a higher version to open the workbook.
- If you receive error "Database error 0x80040154: Class not registered", this means that the MSAS driver is not installed properly on your computer. Please follow the instructions detailed in the following documentation for further assistance: Error "Database error 0x80040154: Class not registered" Connecting to 64-bit Excel and also Tableau Driver Download.
- 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:
SELECT [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])
CLICK TO EXPAND STEPS
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.
- In the Measures pane, right-click Lat and select Geographic Role > Latitude.
- Do the same for Lat2.
- Drag Long to Columns and Lat to Rows.
- Place Location on Detail on the Marks card.
- Select Analysis > Create Calculated Field.
- 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])) )
- Note: The above calculation uses the Great Circle distance formula to balance complexity with accuracy, and uses the average radius of the Earth.
- On the Marks card, select Line.
- Place Distance on Color
- Place Distance on Label.
CLICK TO EXPAND STEPS
Step 3: Position the Labels
- On the Marks card, on the drop-down menu for Distance, select Dimension.
- Now, you will notice that the 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.
- (Optional) Right-click the number, select Format, and in the Format pane, select formatting options to make the numbers more apparent on the map.
Additional Information
To view the above steps in action, see the video below.Note: the video has no sound. To view the video in higher quality, click the YouTube icon below to watch it on YouTube directly.
Discuss this article... Feedback Forum
Measure Distances Between Data Points and Locations in a Map
Thank you for providing your feedback on the effectiveness of the article.
Open new Case
Continue Searching
Knowledge Base
Community
Product Help
Training and Tutorials