LOG(number, [base]) returns the logarithm in the given base, or base 10 when the base is left out: LOG(16, 4) is 2. Zero and negatives give null. In DuckDB use log10(x) for the default and log(base, x) with two arguments: the base comes first in DuckDB and Snowflake but second in Tableau and BigQuery, and BigQuery's one-argument LOG(x) is the natural log.
Updated · Checked on the xmetric engine
What is the syntax?
LOG(number, [base])
number
a positive number.
base
optional, the base of the logarithm. 10 if omitted.
Returns a number: the logarithm of number in base (10 if omitted). Null if the number 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 base-10 log of each value?
Tableau
LOG([Value])
SQL (DuckDB)
SELECTCASEWHEN Value >0THENlog10(Value) ENDAS result
FROM input
ORDERBY id
LOG(16) is 1.204; zero and the negatives return null.
Value
Result
2.5
0.4
−2.5
null
0.13
−0.9
−0.4
null
3.75
0.57
null
0
null
16
1.2
✓ The Tableau formula and the SQL return the same 8 rows.
What is the base-2 log of each value?
Tableau
LOG([Value], 2)
SQL (DuckDB)
SELECTCASEWHEN Value >0THENlog(2, Value) ENDAS result
FROM input
ORDERBY id
16 is 2 to the 4th, so it returns 4; 0.125 returns -3.
Value
Result
2.5
1.32
−2.5
null
0.13
−3
−0.4
null
3.75
1.91
null
0
null
16
4
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why does ln() give bigger numbers than LOG?
Without a base, Tableau's LOG is base 10. Translating it to the natural log (BigQuery's LOG(x) habit) gives 2.77 for 16 instead of 1.20.
Tableau
LOG([Value])
Tempting SQL
SELECTCASEWHEN Value >0THENln(Value) ENDAS result
FROM input
ORDERBY id
SQL that matches
SELECTCASEWHEN Value >0THENlog10(Value) ENDAS result
FROM input
ORDERBY id
Value
Tableau
Tempting SQL
SQL that matches
2.5
0.4
0.92
0.4
−2.5
null
null
null
0.13
−0.9
−2.08
−0.9
−0.4
null
null
null
3.75
0.57
1.32
0.57
null
null
null
0
null
null
null
16
1.2
2.77
1.2
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
Common mistake
Why does copying the argument order give a different log?
DuckDB's two-argument log takes the base first. log(Value, 2) is the log of 2 in base Value: 0.25 for 16, where Tableau's LOG(16, 2) is 4.
Tableau
LOG([Value], 2)
Tempting SQL
SELECTCASEWHEN Value >0THENlog(Value, 2) ENDAS result
FROM input
ORDERBY id
SQL that matches
SELECTCASEWHEN Value >0THENlog(2, Value) ENDAS result
FROM input
ORDERBY id
Value
Tableau
Tempting SQL
SQL that matches
2.5
1.32
0.76
1.32
−2.5
null
null
null
0.13
−3
−0.33
−3
−0.4
null
null
null
3.75
1.91
0.52
1.91
null
null
null
0
null
null
null
16
4
0.25
4
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
What does LOG return with no base?
Leaving the base out is the same as base 10. Columns 1 to 8 are the sample rows, by id.
Case
Tableau, then SQL (DuckDB)
1
2
3
4
5
6
7
8
default base (10)
LOG([Value])CASE WHEN Value > 0 THEN log10(Value) END
0.4
null
−0.9
null
0.57
null
null
1.2
✓ On every row, each Tableau formula and its SQL return the same value.
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 >0THENLOG(10, Value) ENDAS result
FROM input
ORDERBY id