Calculate price difference for each row in tableau

↘锁芯ラ 提交于 2019-12-24 08:08:04

问题


I have a report that returns the data like

Item | Price | Difference

A | 1.00 | 0

B | 2.00 | 1.00

C | 0.50 | -0.50

I have a parameter that allows to select Item, I wonder how can I write up formula for the Difference that would recalculate based on user selection: i.e if use would select item B than all the price should be calculated as difference between the price for current row and the price for item B.

Expected output if user would select item B

A | 1.00 | -1.00

B | 2.00 | 0.00

C | 0.50 | -1.50


回答1:


This was a bastard to figure out, but I have it:

Assuming you've already got your parameter, what we need is a field that has the logic:

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

Here F1 is your category and F1 parameter is the parameter based off of this field

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

We then want to place the value for this field in every row (so we can take the difference between it and the category's price)

To do this we used the FIXED calculation and MAX. Since the IF statement only outputs the price for the parameter selected row, with the rest being null, using MAX will put this value in every row:

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

Then for the difference you can do:

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

Here is a table to demonstrate:

Happy to explain further if you need


If you want to have a null value in the difference column for the selected category, since it'll always be zero, you can change "Difference" to:

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

Picture to show this:



来源:https://stackoverflow.com/questions/52705801/calculate-price-difference-for-each-row-in-tableau

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!