简体   繁体   中英

IF part in tableau calculated field is not getting executed

I want to create a calculated field in tableau , where its value should be dependent on the value of the other field. Below is the calculation the calculated field [D]

IF [A]=NULL
THEN [B]
ELSE [C]
END

This calculated field always assigns the value in the else part ( [D] is populated with the value of [c]). Note : [A],[B], and [C] are the existing fields in the tableau ( all are of string types)

Try this:

IF ISNULL([A])
THEN [B]
ELSE [C]
END

A Null value in a field means there is nothing in the field and it is not storing a value NULL .

If you really want to compare NULL in a field then use as IF ISNULL([A]) . With file based data sources such as Excel or CSV, you can also check for an empty string with IF [A]='' — since those data sources don't distinguish between nulls and empty strings.

As others have pointed out, you can use ISNULL() to test whether a field has a null value. Testing for the presence or absence of a value using ISNULL() is different than testing a value with equality

This is not exactly the case you asked about, but sometimes, you may have a default value for a field that you always want to use in cases when there is no value specified, in that case, the IFNULL() function is useful. It returns the value of its first argument if there is a non-null value, otherwise it returns the value of the second argument.

Let's say you have a field called Approver, and if no Approver is specified in the data, then the default value is "Fred". A reliable approach is to first:

  • Rename the Approver field in Tableau to, say, Approver-Original
  • Define a new Approver field as ifnull(Approver-Original, "Fred")
  • Hide the field Approver-Original

Then you can safely use Approver anywhere you wish knowing that it always has a value, and the information about applying defaults is in one place.

There is also a function called ZN() for numeric field that returns zero if the argument is null. Useful in those cases where zero is the correct default.

All this makes sense in cases, where it makes sense to have a default value to replace null.

There are other cases when you really want to leave a value null to represent the absence of data, say a field called Spouse that will be null for single people. Assigning a default spouse in that case would be wrong, and possibly not appreciated. ISNULL() is useful in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM