简体   繁体   English

Tableau 8.2 计算字段与 If thens

[英]Tableau 8.2 calculated field with If thens

I am working on creating some calculated fields in Tableau 8.2.我正在 Tableau 8.2 中创建一些计算字段。

The data for the "test2" field is imported from Access. “test2”字段的数据是从 Access 导入的。 They can be numbers or "Null" in text.它们可以是文本中的数字或“空”。

I also have fields "test1" and "minimum" and "maximum".我也有字段“test1”和“minimum”和“maximum”。 "test1", "minimum" and "maximum" are only numbers. “test1”、“minimum”和“maximum”只是数字。

I would like to do an calculated field with an if statement.我想用 if 语句做一个计算字段。

The name of the calculated field is "answer".计算字段的名称是“answer”。

I would like to do the following:我想做以下事情:

1) If "test2" is not "Null" and "test2" < "minimum" then calculate "minimum" - "test2". 1) 如果“test2”不是“Null”且“test2”<“minimum”,则计算“minimum”-“test2”。

(I was using the syntax IIF and != for not equal, but it did not like it because "Null" is a string value) (我使用语法 IIF 和 != 表示不相等,但它不喜欢它,因为“Null”是一个字符串值)

2) else if "test2" is not "Null" and "test1" < "minimum" then calculate "minimum" - "test1" 2)否则如果“test2”不是“Null”并且“test1”<“minimum”然后计算“minimum”-“test1”

How would I go about doing this?我该怎么做呢? Please advise.请指教。

IF (NOT ISNULL([test2])) AND [test2] < [minimum]
THEN [minimum] - [test2]
ELSEIF (NOT ISNULL([test2])) AND [test1] < [minimum]
THEN [minimum] - [test1]
END

Not so hard if you know ISNULL function如果你知道 ISNULL 函数,那就没那么难了

Testing for null is redundant (unnecessary) in this case.在这种情况下,测试 null 是多余的(不必要的)。

An if condition that references a field with a null value evaluates to false.引用具有空值的字段的 if 条件的计算结果为 false。 This is different than, say, Java programming.这与 Java 编程不同。 You don't have to test fields for null values before referencing them in most cases.在大多数情况下,您不必在引用字段之前测试空值。 Helpfully, aggregation functions like min(), max(), sum(), count() etc ignore null values altogether.有用的是,像 min()、max()、sum()、count() 等聚合函数完全忽略空值。

Unnecessary null tests make formulas hard to read and can easily hide typos.不必要的空测试使公式难以阅读,并且很容易隐藏拼写错误。 Assuming you meant the test1 instead of test2 in your second test above, then your calculated field need only state:假设您在上面的第二个测试中指的是 test1 而不是 test2,那么您的计算字段只需要说明:

if test2 < minimum then
  minimum - test2
elseif test1 < minimum then
  minimum - test1
end

Inox is correct that when you do need to explicitly test for null values, the function to use is isnull(), or in some cases ifnull() or zn(). Inox 是正确的,当您确实需要显式测试空值时,要使用的函数是 isnull(),或者在某些情况下是 ifnull() 或 zn()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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