Data Studio Case 语句 - 如何 return 原始值?
Data Studio Case Statement - How to return original value?
我正在尝试创建一个计算字段。这是我写的Case语句。
case Rank
when 0 then '-'
end
这按预期工作,但 returns 对于其他值无效:
我想显示非 0 值的原始值(在这种情况下,应该是 1 而不是 null)。我试过这个:
case Rank
when 0 then '-'
else Rank
end
但出现此错误:
Invalid formula - THEN/ELSE arguments have incompatible types: TEXT/ NUMBER. All THEN/ELSE arguments must return the same type.
我找不到任何示例。有什么办法吗?
显示第二个CASE语句中的错误是因为语义类型(从这里开始称为类型 ) Rank
字段是 Number 而第 2 行的 THEN
部分(破折号 -
)是 Text 类型:
Invalid formula - THEN/ELSE arguments have incompatible types: TEXT/
NUMBER. All THEN/ELSE arguments must return the same type.
下面的解决方案涉及确保所有输出都是单一的 Type,(在本例中为 Text);有几种方法(使用下面的 EITHER #1 或 #2):
1) 将 Rank
的类型更改为计算字段中的文本
一种方法是合并 CAST
function to change the Field Type to Text; the entire process can be done in a single Calculated Field using the REGEXP_REPLACE
函数:
REGEXP_REPLACE(CAST(Rank AS TEXT ), "^(0)$", "-" )
2) 将 Rank
的类型更改为数据源中的文本
第二种方法是在数据源中更改 Rank
的字段 Type 并使用第二个 CASE 语句进行小的编辑(包装0 在引号中,因此 0
变为 "0"
):
CASE Rank
WHEN "0" THEN '-'
ELSE Rank
END
Editable Google Data Studio Report 和一张 GIF 来详细说明:
我正在尝试创建一个计算字段。这是我写的Case语句。
case Rank
when 0 then '-'
end
这按预期工作,但 returns 对于其他值无效:
我想显示非 0 值的原始值(在这种情况下,应该是 1 而不是 null)。我试过这个:
case Rank
when 0 then '-'
else Rank
end
但出现此错误:
Invalid formula - THEN/ELSE arguments have incompatible types: TEXT/ NUMBER. All THEN/ELSE arguments must return the same type.
我找不到任何示例。有什么办法吗?
显示第二个CASE语句中的错误是因为语义类型(从这里开始称为类型 ) Rank
字段是 Number 而第 2 行的 THEN
部分(破折号 -
)是 Text 类型:
Invalid formula - THEN/ELSE arguments have incompatible types: TEXT/ NUMBER. All THEN/ELSE arguments must return the same type.
下面的解决方案涉及确保所有输出都是单一的 Type,(在本例中为 Text);有几种方法(使用下面的 EITHER #1 或 #2):
1) 将 Rank
的类型更改为计算字段中的文本
一种方法是合并 CAST
function to change the Field Type to Text; the entire process can be done in a single Calculated Field using the REGEXP_REPLACE
函数:
REGEXP_REPLACE(CAST(Rank AS TEXT ), "^(0)$", "-" )
2) 将 Rank
的类型更改为数据源中的文本
第二种方法是在数据源中更改 Rank
的字段 Type 并使用第二个 CASE 语句进行小的编辑(包装0 在引号中,因此 0
变为 "0"
):
CASE Rank
WHEN "0" THEN '-'
ELSE Rank
END
Editable Google Data Studio Report 和一张 GIF 来详细说明: