如何在 Tableau Desktop 中转换字符串,就像我们在 MS Excel 中使用“=Text()”函数一样?

How to convert strings in Tableau Desktop, like we use "=Text()" function in MS Excel?

我正在使用 Tableau Desktop 10.x,并且有来自 MS Excel 文件的实时连接。

我有一列数字如下:

123
1234
12345

但是,我想更改或创建另一列以查看如下结果:

ID000123
ID001234
ID012345

我知道 Excel 中有一个函数可以将字符串转换为特定格式 --> "=Text()" ,例如下面的 excel 公式将字符串转换为特定格式:

=Text(A1,"ID000000")

请帮助我,因为我需要 Tableau 数据中的这个 trick/results。

这应该有效。假设 [Sales] 是您的数字衡量标准。

if len(str([Sales])) = 3 then "ID000"+str([Sales]) 
elseif len(str([Sales])) = 4 then "ID00"+str([Sales])
elseif len(str([Sales])) = 5 then "ID0"+str([Sales])
end 

Bernardo 的回答是我最熟悉的一个。最近,我发现了另一种不用 if 语句的方法。假设您需要像 ID000000003948 这样的东西,您可以将 Bernardo 的公式整合到这个不依赖于 if 语句的动态公式中。

"ID" + REPLACE(SPACE(12-LEN(Str([Sales]))), " ", "0") + Str([Sales])

公式中的数字 12 是填充字符串所需的总长度,包括零。