Tableau error Type mismatch

The calculation contains errors

Expected type integer, found string.Result types from 'IF' expressions must match.

The branches of your IF return different types: a number after THEN, text after ELSE. Every THEN and ELSE in one IF must return the same type.

Updated · Checked on the xmetric engine

Which formula raises it, and what fixes it?

Raises the error
IF [Sales] > 0 THEN 1 ELSE "no" END
Tableau: Expected type integer, found string.Result types from 'IF' expressions must match.

The xmetric engine rejects it too.

Fixed
IF [Sales] > 0 THEN 1 ELSE 0 END

On each of the 3 example rows it returns:

RegionSalesResult
East201
West101
West−50

Why it works. Both branches now return numbers, so the field is numeric. If you want labels instead, make both branches text, for example "yes" and "no".

The 3 example rows
RegionSales
East20
West10
West-5

What else raises this message?

  1. Raises it

    IF [Sales] > 10 THEN 'High' ELSE [Sales] END

    Fixed

    IF [Sales] > 10 THEN 'High' ELSE STR([Sales]) END

    Every branch of an IF must return the same type; STR turns the number into text.

    Checked on the xmetric engine: the first fails, the fix runs.

  2. Raises it

    IF [Region] = 'East' THEN TRUE ELSE 'No' END

    Fixed

    IF [Region] = 'East' THEN TRUE ELSE FALSE END

    Pick one type per field: a boolean here, which also lets you write just [Region] = 'East'.

    Checked on the xmetric engine: the first fails, the fix runs.

Does my formula have this problem?

Paste your formula and run it on the example rows. It uses the fields [Region], [Sales]; rename yours to match.

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions does this involve?

What do people ask about this error?

How do I return a blank for some rows?

Leave out ELSE: rows that match no branch return NULL, and NULL fits any type. Writing ELSE '' turns a numeric field into text.

Does CASE have the same rule?

Yes. Every WHEN and the ELSE of a CASE must return the same type.

Where is this message documented?