Tableau function Number

Tableau LOG to SQL

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)

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

LOG(16) is 1.204; zero and the negatives return null.

ValueResult
2.50.4
−2.5null
0.13−0.9
−0.4null
3.750.57
null
0null
161.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)

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

16 is 2 to the 4th, so it returns 4; 0.125 returns -3.

ValueResult
2.51.32
−2.5null
0.13−3
−0.4null
3.751.91
null
0null
164

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

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

SQL that matches

SELECT CASE WHEN Value > 0 THEN log10(Value) END AS result
FROM input
ORDER BY id
ValueTableauTempting SQLSQL that matches
2.50.40.920.4
−2.5nullnullnull
0.13−0.9−2.08−0.9
−0.4nullnullnull
3.750.571.320.57
nullnullnull
0nullnullnull
161.22.771.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

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

SQL that matches

SELECT CASE WHEN Value > 0 THEN log(2, Value) END AS result
FROM input
ORDER BY id
ValueTableauTempting SQLSQL that matches
2.51.320.761.32
−2.5nullnullnull
0.13−3−0.33−3
−0.4nullnullnull
3.751.910.521.91
nullnullnull
0nullnullnull
1640.254

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.

CaseTableau, then SQL (DuckDB)12345678
default base (10)LOG([Value])CASE WHEN Value > 0 THEN log10(Value) END0.4null−0.9null0.57nullnull1.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

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

Snowflake documentation

BigQuery

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

BigQuery documentation

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

What base does LOG use by default?

10. LOG(100) is 2. Watch BigQuery, where LOG(x) with one argument is the natural log; use LOG10(x) there.

What does LOG return for zero or a negative?

Null, like LN. DuckDB raises an error on those rows, so the SQL keeps a CASE WHEN Value > 0 guard.

Which argument is the base?

In Tableau the second: LOG([Value], 2). In DuckDB and Snowflake the first: log(2, Value).

Where is this documented?