Tableau function Number

Tableau LN to SQL

LN(number) returns the natural logarithm (base e): LN(50) is 3.912. For zero or a negative number Tableau returns null, while DuckDB's ln() raises an error, so the SQL is CASE WHEN x > 0 THEN ln(x) END. Don't write log(): in DuckDB that is base 10, even though BigQuery's one-argument LOG(x) is natural.

Updated · Checked on the xmetric engine

What is the syntax?

LN(number)
number
a positive number.

Returns a number: the natural logarithm. Null if the input is null, zero or negative.

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 natural log of each value?

Tableau

LN([Value])

SQL (DuckDB)

SELECT CASE WHEN Value > 0 THEN ln(Value) END AS result
FROM input
ORDER BY id

LN(16) is 2.77 and LN(0.125) is -2.08. Zero and the negatives return null.

ValueResult
2.50.92
−2.5null
0.13−2.08
−0.4null
3.751.32
null
0null
162.77

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

Where does a straight translation go wrong?

Common mistake

Why does log() give smaller numbers than LN?

In DuckDB log(x) is the base-10 logarithm, not the natural one (BigQuery and many languages use log for ln, which is where the habit comes from). LN(16) is 2.77; log(16) is 1.20.

Tableau

LN([Value])

Tempting SQL

SELECT CASE WHEN Value > 0 THEN log(Value) END AS result
FROM input
ORDER BY id

SQL that matches

SELECT CASE WHEN Value > 0 THEN ln(Value) END AS result
FROM input
ORDER BY id
ValueTableauTempting SQLSQL that matches
2.50.920.40.92
−2.5nullnullnull
0.13−2.08−0.9−2.08
−0.4nullnullnull
3.751.320.571.32
nullnullnull
0nullnullnull
162.771.22.77

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 Value > 0 THEN LN(Value) END AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT SAFE.LN(Value) AS result
FROM input
ORDER BY id

BigQuery documentation

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

What does LN return for zero or a negative?

Null: rows 2, 4 and 7 have -2.5, -0.4 and 0. DuckDB raises "cannot take logarithm" on those rows, so keep the CASE WHEN Value > 0 guard.

How do I get a log in another base?

LOG([Value], base), or LN([Value]) / LN(base).

Is LN the inverse of EXP?

Yes: LN(EXP(x)) is x.

Where is this documented?