Tableau function Number

Tableau ACOS to SQL

ACOS(number) is the arccosine: the angle, in radians, whose cosine is the number, so ACOS(-1) is π. It only makes sense for numbers from -1 to 1. Outside that range Tableau gives null, while DuckDB's acos() stops the whole query with an error, so the SQL needs a guard: CASE WHEN x BETWEEN -1 AND 1 THEN acos(x) END.

Updated · Checked on the xmetric engine

What is the syntax?

ACOS(number)
number
a number from -1 to 1, usually a cosine or a ratio.

Returns a number of radians from 0 to π. Null if the input is null or outside -1 to 1.

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 angle has each ratio as its cosine?

Tableau

ACOS([Ratio])

SQL (DuckDB)

SELECT
  CASE WHEN Ratio BETWEEN - 1 AND 1 THEN acos(Ratio) END AS result
FROM input
ORDER BY id

-1 gives π (3.14159), 1 gives 0 and 0.5 gives π/3. Row 7 is 1.5, outside the range, so it returns null; row 6 has no ratio.

RatioResult
0.51.05
−0.52.09
10
−13.14
01.57
null
1.5null
−0.82.5

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

What is that angle in degrees?

Tableau

DEGREES(ACOS([Ratio]))

SQL (DuckDB)

SELECT
  CASE
    WHEN Ratio BETWEEN - 1 AND 1 THEN degrees(acos(Ratio))
    END AS result
FROM input
ORDER BY id

0.5 gives 60 degrees, -0.5 gives 120 and -1 gives 180.

RatioResult
0.560
−0.512
10
−1180
090
null
1.5null
−0.8143

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

Where does a straight translation go wrong?

Common mistake

Why does clamping the input give an angle where Tableau gives null?

The quick fix for DuckDB's out-of-range error is to clamp the input into -1 to 1. That turns row 7's 1.5 into 1 and returns 0, an angle Tableau never produced. Guard with CASE instead, so out-of-range values stay null.

Tableau

ACOS([Ratio])

Tempting SQL

SELECT acos(least(1, greatest(- 1, Ratio))) AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  CASE WHEN Ratio BETWEEN - 1 AND 1 THEN acos(Ratio) END AS result
FROM input
ORDER BY id
RatioTableauTempting SQLSQL that matches
0.51.051.051.05
−0.52.092.092.09
1000
−13.143.143.14
01.571.571.57
null3.14null
1.5null0null
−0.82.52.52.5

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
  CASE WHEN Ratio BETWEEN - 1 AND 1 THEN ACOS(Ratio) END AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT SAFE.ACOS(Ratio) AS result
FROM input
ORDER BY id

BigQuery documentation

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

What does ACOS return outside -1 to 1?

Null in Tableau: row 7 has a ratio of 1.5, and ACOS returns null there. DuckDB raises an out-of-range error instead, which is why every SQL on this page has a CASE WHEN ... BETWEEN -1 AND 1 guard.

How do I get degrees instead of radians?

Wrap it: DEGREES(ACOS([Ratio])), and in SQL degrees(acos(Ratio)). A ratio of 0.5 gives 60 degrees.

Is ACOS the inverse of COS?

Yes, within 0 to π: ACOS(COS(x)) gives back x only when x is between 0 and π. Outside that range you get the matching angle inside it.

Where is this documented?