Tableau function Number

Tableau HEXBINX to SQL

HEXBINX(x, y) maps a point to the x-coordinate of the centre of the nearest hexagon, with hexagons of side 1. No SQL dialect has it built in. The centres form two offset grids, (3i, √3·j) and (3i + 1.5, √3·j + √3/2); the SQL finds the nearest centre on each grid and keeps the closer one. Rounding each axis on its own gives squares, not hexagons.

Updated · Checked on the xmetric engine

What is the syntax?

HEXBINX(x, y)
x
the horizontal coordinate, such as a scaled longitude.
y
the vertical coordinate, such as a scaled latitude.

Returns a number: the x-coordinate of the nearest hexagonal bin centre.

What does it return, and what is the SQL?

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

What is the x of the hexagon each point falls in?

Tableau

HEXBINX([Value], [Ratio])

SQL (DuckDB)

WITH c AS (
  SELECT id, Value AS x, Ratio AS y
  FROM input
), h AS (
  SELECT
    id,
    x,
    y,
    3 * floor(x / 3 + 0.5) AS ax,
    sqrt(3) * floor(y / sqrt(3) + 0.5) AS ay,
    3 * floor((x - 1.5) / 3 + 0.5) + 1.5 AS bx,
    sqrt(3)
      * floor((y - sqrt(3) / 2) / sqrt(3) + 0.5)
      + sqrt(3)
      / 2 AS b_y
  FROM c
)
SELECT
  CASE
    WHEN pow(x - ax, 2) + pow(y - ay, 2) <= pow(x - bx, 2) + pow(y - b_y, 2) THEN ax
    ELSE bx
    END AS result
FROM h
ORDER BY id

Row 8 (16, -0.8) is nearer the offset centre (16.5, -0.866) than the grid centre (15, 0), so its x is 16.5.

ValueRatioResult
2.50.53
−2.5−0.5−3
0.1310
−0.4−10
3.7503
null
01.50
16−0.816.5

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

How do I bin into smaller hexagons?

Tableau

HEXBINX([Value] * 2, [Ratio] * 2) / 2

SQL (DuckDB)

WITH c AS (
  SELECT id, Value * 2 AS x, Ratio * 2 AS y
  FROM input
), h AS (
  SELECT
    id,
    x,
    y,
    3 * floor(x / 3 + 0.5) AS ax,
    sqrt(3) * floor(y / sqrt(3) + 0.5) AS ay,
    3 * floor((x - 1.5) / 3 + 0.5) + 1.5 AS bx,
    sqrt(3)
      * floor((y - sqrt(3) / 2) / sqrt(3) + 0.5)
      + sqrt(3)
      / 2 AS b_y
  FROM c
)
SELECT
  CASE
    WHEN pow(x - ax, 2) + pow(y - ay, 2) <= pow(x - bx, 2) + pow(y - b_y, 2) THEN ax / 2
    ELSE bx / 2
    END AS result
FROM h
ORDER BY id

Doubling the inputs and halving the result gives hexagons of side 0.5.

ValueRatioResult
2.50.52.25
−2.5−0.5−2.25
0.1310
−0.4−10
3.7503.75
null
01.50
16−0.815.75

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

Where does a straight translation go wrong?

Common mistake

Why does rounding each axis give the wrong bin?

Rounding x on its own snaps points to a square grid. Row 5 (3.75, 0) rounds to 4, but the nearest hexagon centre is at 3.

Tableau

HEXBINX([Value], [Ratio])

Tempting SQL

SELECT round(Value) AS result
FROM input
ORDER BY id

SQL that matches

WITH c AS (
  SELECT id, Value AS x, Ratio AS y
  FROM input
), h AS (
  SELECT
    id,
    x,
    y,
    3 * floor(x / 3 + 0.5) AS ax,
    sqrt(3) * floor(y / sqrt(3) + 0.5) AS ay,
    3 * floor((x - 1.5) / 3 + 0.5) + 1.5 AS bx,
    sqrt(3)
      * floor((y - sqrt(3) / 2) / sqrt(3) + 0.5)
      + sqrt(3)
      / 2 AS b_y
  FROM c
)
SELECT
  CASE
    WHEN pow(x - ax, 2) + pow(y - ay, 2) <= pow(x - bx, 2) + pow(y - b_y, 2) THEN ax
    ELSE bx
    END AS result
FROM h
ORDER BY id
ValueRatioTableauTempting SQLSQL that matches
2.50.5333
−2.5−0.5−3−3−3
0.131000
−0.4−1000
3.750343
nullnullnull
01.5000
16−0.816.51616.5

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

WITH c AS (
  SELECT id, Value AS x, Ratio AS y
  FROM input
), h AS (
  SELECT
    id,
    x,
    y,
    3 * FLOOR(x / 3 + 0.5) AS ax,
    SQRT(3) * FLOOR(y / SQRT(3) + 0.5) AS ay,
    3 * FLOOR((x - 1.5) / 3 + 0.5) + 1.5 AS bx,
    SQRT(3)
      * FLOOR((y - SQRT(3) / 2) / SQRT(3) + 0.5)
      + SQRT(3)
      / 2 AS b_y
  FROM c
)
SELECT
  CASE
    WHEN POWER(x - ax, 2) + POWER(y - ay, 2) <= POWER(x - bx, 2) + POWER(y - b_y, 2) THEN ax
    ELSE bx
    END AS result
FROM h
ORDER BY id

Snowflake documentation

BigQuery

WITH c AS (
  SELECT id, Value AS x, Ratio AS y
  FROM input
), h AS (
  SELECT
    id,
    x,
    y,
    3 * FLOOR(x / 3 + 0.5) AS ax,
    SQRT(3) * FLOOR(y / SQRT(3) + 0.5) AS ay,
    3 * FLOOR((x - 1.5) / 3 + 0.5) + 1.5 AS bx,
    SQRT(3)
      * FLOOR((y - SQRT(3) / 2) / SQRT(3) + 0.5)
      + SQRT(3)
      / 2 AS b_y
  FROM c
)
SELECT
  CASE
    WHEN POW(x - ax, 2) + POW(y - ay, 2) <= POW(x - bx, 2) + POW(y - b_y, 2) THEN ax
    ELSE bx
    END AS result
FROM h
ORDER BY id

BigQuery documentation

What does your own HEXBINX formula return?

Edit the formula and run it on the 8 sample rows. Fields: [id], [Region], [Value], [Qty], [Ratio], [Other].

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

Which functions go with it?

What do people ask about HEXBINX?

How do I make the hexagons bigger or smaller?

Scale the inputs and divide the result back: HEXBINX([x] * 2, [y] * 2) / 2 uses hexagons of side 0.5. Tableau's help scales longitude and latitude by 2.5.

Do I need both HEXBINX and HEXBINY?

Yes. Put both on the view (usually as dimensions on Columns and Rows) to get one mark per hexagon; each alone only gives one coordinate of the centre.

What happens to a null?

Row 6 has no Value and no Ratio, and the bin is null.

Where is this documented?