计算画面中每一行的价格差异

Calculate price difference for each row in tableau

我有一份报告说 returns 数据像

项目 |价格 |差异

一个 | 1.00 | 0

乙 | 2.00 | 1.00

C | 0.50 | -0.50

我有一个允许 select 项的参数,我想知道如何编写基于用户 selection 重新计算的差异公式:即如果使用 select 项目 B 比所有价格应计算为当前行的价格与项目 B 的价格之间的差值。

如果用户 select 项目 B

的预期输出

一个 | 1.00 | -1.00

乙 | 2.00 | 0.00

C | 0.50 | -1.50

这是个混蛋,但我知道了:

假设您已经有了参数,我们需要的是一个具有以下逻辑的字段:

"If category is the same as what's selected in the parameter, then get the price"

这里F1是你的类别,F1参数是基于这个字段的参数

"Get parameter select price" = IF [F1 Parameter] = [F1] THEN [Price] END

然后我们想把这个字段的值放在每一行中(这样我们就可以得到它和类别价格之间的差异)

为此,我们使用了 FIXED 计算和 MAX。由于IF语句只输出参数选择行的价格,其余为null,使用MAX会把这个值放在每一行:

"Parameter Selected Value for all rows" =    {FIXED: MAX(IF [F1 Parameter] = [F1] THEN [Price] END)}

那么为了区别你可以做:

"Difference" = [Price] - [Parameter Selected Value for all rows]

这里有一个table来演示:

如果您需要,很乐意进一步解释


如果您希望所选类别的差异列中有空值,因为它始终为零,您可以将 "Difference" 更改为:

IF [F1 Parameter] = [F1] THEN NULL ELSE Price - [Parameter Selected Value for all rows] END

图片显示: