Tableau function Number

Tableau SQUARE to SQL

SQUARE(number) returns the number times itself: SQUARE(5) is 25, and a negative becomes positive. DuckDB and BigQuery have no SQUARE function, so write x * x or pow(x, 2); Snowflake has SQUARE(). It often sits inside aggregates (variance, root mean square), where AVG(SQUARE(x)) and SQUARE(AVG(x)) are different numbers.

Updated · Checked on the xmetric engine

What is the syntax?

SQUARE(number)
number
a number or a numeric expression.

Returns a number: the input times itself. Null if the input is null.

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 each value squared?

Tableau

SQUARE([Value])

SQL (DuckDB)

SELECT Value * Value AS result
FROM input
ORDER BY id

2.5 and -2.5 both square to 6.25; 16 squares to 256.

ValueResult
2.56.25
−2.56.25
0.130.02
−0.40.16
3.7514.06
null
00
16256

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

What is the average squared value per region?

Tableau

AVG(SQUARE([Value]))

SQL (DuckDB)

SELECT Region, avg(Value * Value) AS result
FROM input
GROUP BY Region
ORDER BY Region

East averages 6.25, 6.25 and 0.0156: 4.17.

RegionResult
East4.17
North128
West7.11

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

Where does a straight translation go wrong?

Common mistake

Why is the square of the average smaller than the average of the squares?

Squaring the average lets East's 2.5 and -2.5 cancel before squaring, giving 0.0017 instead of 4.17. The difference between the two is the variance, so keep SQUARE inside AVG as Tableau has it.

Tableau

AVG(SQUARE([Value]))

Tempting SQL

SELECT Region, pow(avg(Value), 2) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, avg(Value * Value) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East4.1704.17
North12864128
West7.112.817.11

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 SQUARE(Value) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT POW(Value, 2) AS result
FROM input
ORDER BY id

BigQuery documentation

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

Is SQUARE the inverse of SQRT?

For non-negative numbers, yes. SQRT(SQUARE(-3)) is 3, not -3.

How do I get the root mean square?

SQRT(AVG(SQUARE([Value]))); in SQL, sqrt(avg(Value * Value)).

What does SQUARE return for a null?

Null. Row 6 has no value.

Where is this documented?