Tableau: last value calculation in calculated field

时间:2015-07-28 17:13:50

标签: tableau calculated-field

my dataset is like this

KPI                    VALUE    TYPE     DATE  
coffee break duration   11        0      30/06/2015  
coffee break duration   12        0      31/07/2015  
coffee break duration   10        0      30/11/2014  
coffee break duration   10        0      31/12/2014  
coffee expense          20        1      31/07/2015  
coffee expense          20        1      31/12/2014  
coffee consumers        15       -1      31/07/2015  
coffee consumers        17       -1      31/12/2014  

for Type, 0 means minutes, 1 means dollars and -1 means people

I want to get a table like this

KPI                     Year(date)    YTD  
coffee break duration    2015         11,5  
coffee break duration    2014         10  
....

YTD calculation is:

if sum([TYPE]) = 0 then avg([VALUE])
elseif sum([TYPE]) > 0 then sum([VALUE])
elseif sum([TYPE]) < 0 then [last value for the considered year]
end

By [Last value for the considered year] I mean the last entry available, in a year if my table is set to Year, otherwise it has to change dynamically based on what Timespan I want to show.

What can I do to have [last value for the considered year] as a calc field ready to use in my YTD calc?

Many thanks,
Stefania

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,那么你可以在IF声明中使用LOD

if sum(type) = 0 then avg([value])
elseif sum([type]) > 0 then sum(value)
elseif sum([type]) < 0 then max(if [date] = { INCLUDE kpi: max(date)} then [value] end)
end

如果在考虑的年份的最后一天有多个值,则需要最大值

我稍微修改了您的数据,以显示结果正常运行 enter image description here