Tableau function Number

Tableau ATAN2 to SQL

ATAN2(y, x) returns the angle, in radians from -π to π, between the positive x axis and the point (x, y). The y coordinate comes first. DuckDB, Snowflake and BigQuery take the same order, atan2(y, x), but Excel and some libraries take x first, so copying the arguments from a spreadsheet swaps them silently.

Updated · Checked on the xmetric engine

What is the syntax?

ATAN2(y_number, x_number)
y_number
the vertical coordinate (first).
x_number
the horizontal coordinate (second).

Returns a number of radians from -π to π. Null if either argument is null.

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 angle of each point?

Tableau

ATAN2([Value], [Other])

SQL (DuckDB)

SELECT atan2(Value, Other) AS result
FROM input
ORDER BY id

Value is y and Other is x. Row 8 (16, 16) is π/4; row 2 (-2.5 over -3) is in the third quadrant, -2.45.

ValueOtherResult
2.540.56
−2.5−3−2.45
0.13null
−0.4−1−2.76
3.7580.44
2null
00.50
16160.79

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

What is that angle in degrees?

Tableau

DEGREES(ATAN2([Value], [Other]))

SQL (DuckDB)

SELECT degrees(atan2(Value, Other)) AS result
FROM input
ORDER BY id

Row 8 is 45 degrees; row 2 is about -140.

ValueOtherResult
2.5432.01
−2.5−3−14
0.13null
−0.4−1−158
3.75825.11
2null
00.50
161645

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

Where does a straight translation go wrong?

Common mistake

Why do the angles change when the arguments are swapped?

Tableau reads the first argument as y. Written x first (the Excel order), the SQL measures the angle from the other axis: row 1 becomes 1.01 instead of 0.56. Keep y first.

Tableau

ATAN2([Value], [Other])

Tempting SQL

SELECT atan2(Other, Value) AS result
FROM input
ORDER BY id

SQL that matches

SELECT atan2(Value, Other) AS result
FROM input
ORDER BY id
ValueOtherTableauTempting SQLSQL that matches
2.540.561.010.56
−2.5−3−2.45−2.27−2.45
0.13nullnullnull
−0.4−1−2.76−1.95−2.76
3.7580.441.130.44
2nullnullnull
00.501.570
16160.790.790.79

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 ATAN2(Value, Other) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT ATAN2(Value, Other) AS result
FROM input
ORDER BY id

BigQuery documentation

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

Which argument comes first?

y. ATAN2(2, 1) is 1.107 (a steep angle), ATAN2(1, 2) is 0.464. DuckDB, Snowflake and BigQuery follow the same order; Excel's ATAN2 is (x, y).

How do I get a 0 to 360 degree bearing?

Take DEGREES(ATAN2([y], [x])), which gives -180 to 180, and add 360 to the negative results: in SQL, (degrees(atan2(y, x)) + 360) % 360.

What does ATAN2 return when an argument is null?

Null. Row 3 has no Other and row 6 no Value, so both return null.

Where is this documented?