解决Tableau中IF结果的聚合和非聚合混合错误

时间:2015-09-27 06:14:30

标签: if-statement error-handling aggregate-functions tableau

我正在尝试在Tableau中执行以下操作。

如果选择的关键指标是销售额或交易量(非聚合字段),则调用calculate1,否则计算2。

IF ATTR([Choose Key Figure])= "Sales"
THEN [Periodic Calculation]
ELSEIF ATTR([Choose Key Figure])= "Volume"
THEN [Periodic Calculation]
ELSEIF ATTR([Choose Key Figure])="profit"
THEN [Periodic Calculation 2]
ELSEIF ATTR([Choose Key Figure])="price per unit"
THEN [Periodic Calculation 2]
END 

但是我收到以下错误: "不能混合聚合和非聚合比较或结果' IF'表达式"

定期计算

IF [Choose Vol/NNS]="NNS"
AND [Choose Period]=-1
AND DATEDIFF("month",[Dates], [Choose Current Month])<2
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>0
then [(Actuals+Planned) NNS]
ELSEIF [Choose Vol/NNS]="Volume"
AND    [Choose Period]=-1
AND DATEDIFF("month",[Dates], [Choose Current Month])<2
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>0
THEN [(Actuals + Planned) Volume]
ELSEIF [Choose Vol/NNS]="NNS"
AND [Choose Period]=-3
AND DATEDIFF("month",[Dates], [Choose Current Month])<4
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>0
then [(Actuals+Planned) NNS]
ELSEIF [Choose Vol/NNS]="Volume"
AND [Choose Period]=-3
AND DATEDIFF("month",[Dates], [Choose Current Month])<4
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>0
THEN [(Actuals + Planned) Volume]
ELSEIF [Choose Vol/NNS]="NNS"
AND [Choose Period]=-6
AND DATEDIFF("month",[Dates], [Choose Current Month])<7
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>0
then [(Actuals+Planned) NNS]
ELSEIF [Choose Vol/NNS]="Volume"
AND     [Choose Period]=-6
AND DATEDIFF("month",[Dates], [Choose Current Month])<7
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>0
THEN [(Actuals + Planned) Volume]
ELSEIF [Choose Vol/NNS]="NNS"
AND     [Choose Period]=1
AND DATEDIFF("month",[Dates], [Choose Current Month])<0
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>-2
THEN [(Actuals+Planned) NNS]
ELSEIF [Choose Vol/NNS]="Volume"
AND     [Choose Period]=1
AND DATEDIFF("month",[Dates], [Choose Current Month])<0
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>-2
THEN [(Actuals + Planned) Volume]
ELSEIF [Choose Vol/NNS]="NNS"
AND     [Choose Period]=3
AND DATEDIFF("month",[Dates], [Choose Current Month])<0
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>-4
THEN [(Actuals+Planned) NNS]
ELSEIF [Choose Vol/NNS]="Volume"
AND     [Choose Period]=3
AND DATEDIFF("month",[Dates], [Choose Current Month])<0
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>-4
THEN [(Actuals + Planned) Volume]
ELSEIF [Choose Vol/NNS]="NNS"
AND     [Choose Period]=6
AND DATEDIFF("month",[Dates], [Choose Current Month])<0
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>-7
THEN [(Actuals+Planned) NNS]
ELSEIF [Choose Vol/NNS]="Volume"
AND     [Choose Period]=6
AND DATEDIFF("month",[Dates], [Choose Current Month])<0
AND
DATEDIFF("month",[Dates],[Choose Current Month] )>-7
THEN [(Actuals + Planned) Volume]

END

定期计算2

IF [Choose IGM$/NNSP]="NNSP"
AND [Choose Period]=-1
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<2
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>0
THEN [(Actuals + Planned) NNSP]
ELSEIF [Choose IGM$/NNSP]="IGM$"
AND [Choose Period]=-1
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<2
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>0
THEN [(Actuals + Planned) IGM$]
ELSEIF [Choose IGM$/NNSP]="NNSP"
AND [Choose Period]=-3
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<4
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>0
THEN [(Actuals + Planned) NNSP]
ELSEIF [Choose IGM$/NNSP]="IGM$"
AND [Choose Period]=-3
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<4
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>0
THEN [(Actuals + Planned) IGM$]
ELSEIF [Choose IGM$/NNSP]="NNSP"
AND [Choose Period]=-6
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<7
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>0
THEN [(Actuals + Planned) NNSP]
ELSEIF [Choose IGM$/NNSP]="IGM$"
AND [Choose Period]=-6
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<7
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>0
THEN [(Actuals + Planned) IGM$]
ELSEIF [Choose IGM$/NNSP]="NNSP"
AND [Choose Period]=1
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<0
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>-2
THEN [(Actuals + Planned) NNSP]
ELSEIF [Choose IGM$/NNSP]="IGM$"
AND [Choose Period]=1
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<0
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>-2
THEN [(Actuals + Planned) IGM$]
ELSEIF [Choose IGM$/NNSP]="NNSP"
AND [Choose Period]=3
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<0
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>-4
THEN [(Actuals + Planned) NNSP]
ELSEIF [Choose IGM$/NNSP]="IGM$"
AND [Choose Period]=3
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<0
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>-4
THEN [(Actuals + Planned) IGM$]
ELSEIF [Choose IGM$/NNSP]="NNSP"
AND [Choose Period]=6
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<0
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>-7
THEN [(Actuals + Planned) NNSP]
ELSEIF [Choose IGM$/NNSP]="IGM$"
AND [Choose Period]=6
AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))<0
and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))>-7
THEN [(Actuals + Planned) IGM$]
END

任何人都可以帮忙解决此错误吗?

感谢。

1 个答案:

答案 0 :(得分:1)

如果[选择关键指标]是参数,[周期计算]和[周期计算2]是行级(非聚合)计算,则可以:

  1. 删除计算字段中对ATTR()的所有调用 或
  2. 删除对ATTR()的所有调用,并在每个对[Periodic Calculation]或[Periodic Calculation 2]的引用上调用SUM()等聚合函数 或
  3. 保持对ATTR()的调用,并在[Periodic Calculation]或[Periodic Calculation 2]
  4. 的每个引用上调用SUM()等聚合函数

    1和2之间的区别在于是否为计算中的计算字段或使用它的Tableau工作簿中指定聚合函数。

    在这种情况下没有真正的理由选择第三种方法,因为参数一次只能有一个值,所以ATTR()调用没用。