Tableau function Number

Tableau HEXBINY to SQL

HEXBINY(x, y) maps a point to the y-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?

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

Returns a number: the y-coordinate of the nearest hexagonal bin centre, which xmetric returns to 3 decimals.

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 y of the hexagon each point falls in?

Tableau

HEXBINY([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 round(ay, 3)
    ELSE round(b_y, 3)
    END AS result
FROM h
ORDER BY id

Row 3 (0.125, 1) is nearest the centre (0, 1.732); row 8 (16, -0.8) is nearest (16.5, -0.866).

ValueRatioResult
2.50.50
−2.5−0.50
0.1311.73
−0.4−1−1.73
3.7500
null
01.51.73
16−0.8−0.87

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

How do I bin into smaller hexagons?

Tableau

HEXBINY([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 round(ay, 3) / 2
    ELSE round(b_y, 3) / 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.50.43
−2.5−0.5−0.43
0.1310.87
−0.4−1−0.87
3.7500.43
null
01.51.73
16−0.8−0.43

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 y on its own ignores the hexagon rows, which sit √3/2 apart. Row 3 (0.125, 1) rounds to 1, but its hexagon centre is at 1.732.

Tableau

HEXBINY([Value], [Ratio])

Tempting SQL

SELECT round(Ratio) 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 round(ay, 3)
    ELSE round(b_y, 3)
    END AS result
FROM h
ORDER BY id
ValueRatioTableauTempting SQLSQL that matches
2.50.5010
−2.5−0.50−10
0.1311.7311.73
−0.4−1−1.73−1−1.73
3.750000
nullnullnull
01.51.7321.73
16−0.8−0.87−1−0.87

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 ROUND(ay, 3)
    ELSE ROUND(b_y, 3)
    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 ROUND(ay, 3)
    ELSE ROUND(b_y, 3)
    END AS result
FROM h
ORDER BY id

BigQuery documentation

What does your own HEXBINY 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 HEXBINY?

How do I make the hexagons bigger or smaller?

Scale the inputs and divide the result back: HEXBINY([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?