Tableau function Number

Tableau SIN to SQL

SIN(number) returns the sine of an angle given in radians: SIN(PI()/4) is 0.7071. DuckDB, Snowflake and BigQuery spell it sin() too. If your angles are in degrees, convert first with RADIANS(). Watch where the aggregate sits: AVG(SIN(x)) and SIN(AVG(x)) are different numbers.

Updated · Checked on the xmetric engine

What is the syntax?

SIN(number)
number
an angle in radians. Use RADIANS([Degrees]) for angles in degrees.

Returns a number from -1 to 1. 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 sine of each angle?

Tableau

SIN([Value])

SQL (DuckDB)

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

Value is read as radians: 0 gives 0, 16 radians gives -0.288.

ValueResult
2.50.6
−2.5−0.6
0.130.12
−0.4−0.39
3.75−0.57
null
00
16−0.29

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

What is the average sine per region?

Tableau

AVG(SIN([Value]))

SQL (DuckDB)

SELECT Region, avg(sin(Value)) AS result
FROM input
GROUP BY Region
ORDER BY Region

Each row's sine is taken first, then averaged per region; the null row is skipped.

RegionResult
East0.04
North−0.14
West−0.48

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

Where does a straight translation go wrong?

Common mistake

Why is sin(avg(x)) not the same as avg(sin(x))?

Averaging the angles first gives the sine of one middle angle. Tableau's formula takes the sine of each row and then averages, so North is -0.14 in Tableau and 0.99 in the SQL. Keep the order of the formula.

Tableau

AVG(SIN([Value]))

Tempting SQL

SELECT Region, sin(avg(Value)) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, avg(sin(Value)) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East0.040.040.04
North−0.140.99−0.14
West−0.480.99−0.48

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

Does SIN take degrees or radians?

Radians. For degrees, write SIN(RADIANS([Angle])); in SQL, sin(radians(Angle)).

Why average the sine and cosine of angles?

Angles wrap around: the plain average of 350 and 10 degrees is 180, the opposite direction. The mean direction is ATAN2(AVG(SIN(a)), AVG(COS(a))), which needs the trigonometry inside the average.

What does SIN return for a null?

Null. Row 6 has no value.

Where is this documented?