Tableau function Number

Tableau TAN to SQL

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)

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

TAN(0) is 0; 2.5 radians is in the second quadrant, so its tangent is negative (-0.747).

ValueResult
2.5−0.75
−2.50.75
0.130.13
−0.4−0.42
3.750.7
null
00
160.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
GROUP BY Region
ORDER BY Region

West's average angle is 1.675 radians, just past π/2, so its tangent is -9.56.

RegionResult
East0.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
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, tan(avg(Value)) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East0.040.040.04
North−6.80.15−6.8
West−9.560.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.

Snowflake

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

Does TAN take degrees?

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.

What does TAN return for a null?

Null. Row 6 has no value.

Where is this documented?