TAN(number) returns the tangent of an angle in radians: TAN(PI()/4) is 1. DuckDB, Snowflake and BigQuery all use tan(). Translate the aggregate exactly where Tableau puts it: TAN(AVG(x)) is the tangent of the average angle, AVG(TAN(x)) the average of the tangents, and the two can be far apart.
Updated · Checked on the xmetric engine
What is the syntax?
TAN(number)
number
an angle in radians.
Returns a number. Null if the input 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 tangent of each angle?
Tableau
TAN([Value])
SQL (DuckDB)
SELECTtan(Value) AS result
FROM input
ORDERBY id
TAN(0) is 0; 2.5 radians is in the second quadrant, so its tangent is negative (-0.747).
Value
Result
2.5
−0.75
−2.5
0.75
0.13
0.13
−0.4
−0.42
3.75
0.7
null
0
0
16
0.3
✓ The Tableau formula and the SQL return the same 8 rows.
What is the tangent of each region's average angle?
Tableau
TAN(AVG([Value]))
SQL (DuckDB)
SELECT Region, tan(avg(Value)) AS result
FROM input
GROUPBY Region
ORDERBY Region
West's average angle is 1.675 radians, just past π/2, so its tangent is -9.56.
Region
Result
East
0.04
North
−6.8
West
−9.56
✓ The Tableau formula and the SQL return the same 3 rows.
Where does a straight translation go wrong?
Common mistake
Why does averaging the tangents give a different answer?
Tableau averages the angles and then takes one tangent. Averaging each row's tangent instead gives 0.14 for West, against Tableau's -9.56.
Tableau
TAN(AVG([Value]))
Tempting SQL
SELECT Region, avg(tan(Value)) AS result
FROM input
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, tan(avg(Value)) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
0.04
0.04
0.04
North
−6.8
0.15
−6.8
West
−9.56
0.14
−9.56
✓ 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.
No, radians. For degrees, TAN(RADIANS([Angle])); in SQL tan(radians(Angle)).
What happens near 90 degrees?
The tangent grows without limit near π/2. Floating-point π/2 isn't exact, so TAN(PI()/2) returns a huge number rather than an error, in Tableau and in SQL.