知識庫

Error "Can't compare boolean and string values" When


發佈時間 : 14 Nov 2016
上次修改日期 : 25 Oct 2022

問題

Error in calculation:
 
Can't compare boolean and string values.

環境

Tableau Desktop

解決方法

The steps below correctly format the boolean as a value TRUE or FALSE:

Step 1: Create boolean expression

  1. Create new calculated field called "boolean"
  2. Use the following formula: [Segment] - "Consumer"

Step 2: Create Adjusted amount

  1. Create new calculated field called "Adjusted amount"
  2. Use the following formula:

    If [boolean] = True

    THEN [Profit]

    ELSE [Sales]

    END

  3. Note: do not use quotes around "True" - see the calculated field called "Adjusted amount Error" and click on the error at the bottom. This replicates the error in the case subject. 

Step 3: Create example calculation using Adjusted amount

  1. Create a new calculated field called Adjusted amount divided
  2. Use the following formula: SUM([Adjusted amount])/SUM([Quantity])
  3. This calculation, finally, does the math of dividing the true field by a third measure.

原因

Using quotes around "True" or "False" when using a boolean in a calculation will create the error "Can't compare boolean and string values" because the text in quotes is considered a string, rather than a boolean value.
這篇文章是否解決了問題?