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)
SELECTCASEWHEN Value >0THENln(Value) ENDAS result
FROM input
ORDERBY id
LN(16) is 2.77 and LN(0.125) is -2.08. Zero and the negatives return null.
Value
Result
2.5
0.92
−2.5
null
0.13
−2.08
−0.4
null
3.75
1.32
null
0
null
16
2.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
SELECTCASEWHEN Value >0THENlog(Value) ENDAS result
FROM input
ORDERBY id
SQL that matches
SELECTCASEWHEN Value >0THENln(Value) ENDAS result
FROM input
ORDERBY id
Value
Tableau
Tempting SQL
SQL that matches
2.5
0.92
0.4
0.92
−2.5
null
null
null
0.13
−2.08
−0.9
−2.08
−0.4
null
null
null
3.75
1.32
0.57
1.32
null
null
null
0
null
null
null
16
2.77
1.2
2.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
SELECTCASEWHEN Value >0THENLN(Value) ENDAS result
FROM input
ORDERBY id