Tableau function Spatial

Tableau MAKELINE to SQL

MAKELINE(MAKEPOINT([Lat], [Lon]), MAKEPOINT([DestLat], [DestLon])) draws a straight line from each depot to its destination: a two-point LineString. In SQL it is ST_MakeLine(ST_Point(Lon, Lat), ST_Point(DestLon, DestLat)), longitude first. If either point is missing there is no line. The runnable SQL builds the same GeoJSON the engine returns with json_object, because this page's DuckDB has no spatial extension. With the extension loaded, the DuckDB call is shown in the FAQ.

Updated · Checked on the xmetric engine

What is the syntax?

MAKELINE(start_point, end_point)
start_point, end_point
spatial points, usually MAKEPOINT(lat, lon) calls.

Returns a spatial line from the first point to the second. Null when either point is null.

What does it return, and what is the SQL?

Each example runs on the same 4 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

What does each delivery route look like?

Tableau

MAKELINE(MAKEPOINT([Lat], [Lon]), MAKEPOINT([DestLat], [DestLon]))

SQL (DuckDB)

SELECT
  CASE
    WHEN Lat IS NULL OR Lon IS NULL OR DestLat IS NULL OR DestLon IS NULL THEN NULL
    ELSE json_object('type', 'LineString', 'coordinates', [[Lon, Lat], [DestLon, DestLat] ])
    END AS result
FROM input
ORDER BY id

Each line runs from the depot to the destination. Hue has no depot longitude, so it has no line.

LatLonDestLatDestLonResult
10.7810710.05106[object Object]
21.0310620.84107[object Object]
16.0510816.46108[object Object]
16.4615.88108null

The Tableau formula and the SQL return the same 4 rows.

What is the return trip?

Tableau

MAKELINE(MAKEPOINT([DestLat], [DestLon]), MAKEPOINT([Lat], [Lon]))

SQL (DuckDB)

SELECT
  CASE
    WHEN Lat IS NULL OR Lon IS NULL OR DestLat IS NULL OR DestLon IS NULL THEN NULL
    ELSE json_object('type', 'LineString', 'coordinates', [[DestLon, DestLat], [Lon, Lat] ])
    END AS result
FROM input
ORDER BY id

The same line, drawn the other way: the first argument is where it starts.

LatLonDestLatDestLonResult
10.7810710.05106[object Object]
21.0310620.84107[object Object]
16.0510816.46108[object Object]
16.4615.88108null

The Tableau formula and the SQL return the same 4 rows.

Where does a straight translation go wrong?

Common mistake

Why does a route appear for a depot with no longitude?

MAKELINE needs both points; with one missing it returns null. A hand-built LineString still comes back as an object with a null coordinate inside. Return null when any coordinate is missing.

Tableau

ISNULL(MAKELINE(MAKEPOINT([Lat], [Lon]), MAKEPOINT([DestLat], [DestLon])))

Tempting SQL

SELECT
  json_object('type', 'LineString', 'coordinates', [[Lon, Lat], [DestLon, DestLat] ]) IS NULL AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  Lat IS NULL OR Lon IS NULL OR DestLat IS NULL OR DestLon IS NULL AS result
FROM input
ORDER BY id
LatLonDestLatDestLonTableauTempting SQLSQL that matches
10.7810710.05106falsefalsefalse
21.0310620.84107falsefalsefalse
16.0510816.46108falsefalsefalse
16.4615.88108truefalsetrue

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

How do I write it in Snowflake or BigQuery?

The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.

Snowflake

SELECT
  ST_MAKELINE(ST_MAKEPOINT(Lon, Lat), ST_MAKEPOINT(DestLon, DestLat)) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT
  ST_MAKELINE(ST_GEOGPOINT(Lon, Lat), ST_GEOGPOINT(DestLon, DestLat)) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own MAKELINE formula return?

Edit the formula and run it on the 4 sample rows. Fields: [id], [Depot], [Lat], [Lon], [DestLat], [DestLon], [Wkt], [Route].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about MAKELINE?

Which comes first, latitude or longitude?

Tableau's MAKEPOINT takes latitude first. SQL spatial functions and WKT take x first, which is longitude: DuckDB ST_Point(Lon, Lat), Snowflake ST_MAKEPOINT(Lon, Lat), BigQuery ST_GEOGPOINT(Lon, Lat). Swapping them puts Saigon (10.78 N, 106.70 E) off the map, since 106.70 is not a valid latitude.

Does the line follow the curve of the earth?

The line stores just two points. How it is drawn between them depends on the tool: a geography type in Snowflake or BigQuery treats it as the shortest path on the sphere; a planar GEOMETRY treats it as a straight line on the coordinate plane.

Where is this documented?