Tableau function Number

Tableau COS to SQL

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

Updated · Checked on the xmetric engine

What is the syntax?

COS(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 cosine of each angle?

Tableau

COS([Value])

SQL (DuckDB)

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

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

ValueResult
2.5−0.8
−2.5−0.8
0.130.99
−0.40.92
3.75−0.82
null
01
16−0.96

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

What is the average cosine per region?

Tableau

AVG(COS([Value]))

SQL (DuckDB)

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

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

RegionResult
East−0.2
North0.02
West0.05

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

Where does a straight translation go wrong?

Common mistake

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

Averaging the angles first gives the cosine of one middle angle. Tableau's formula takes the cosine of each row and then averages, so East is -0.20 in Tableau and 1.00 in the SQL. Keep the order of the formula.

Tableau

AVG(COS([Value]))

Tempting SQL

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

SQL that matches

SELECT Region, avg(cos(Value)) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East−0.21−0.2
North0.02−0.150.02
West0.05−0.10.05

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

Does COS take degrees or radians?

Radians. For degrees, write COS(RADIANS([Angle])); in SQL, cos(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 COS return for a null?

Null. Row 6 has no value.

Where is this documented?