If statement 解决表格中IF结果的骨料和非骨料混合错误

If statement 解决表格中IF结果的骨料和非骨料混合错误,if-statement,error-handling,aggregate-functions,tableau-api,If Statement,Error Handling,Aggregate Functions,Tableau Api,我试着在Tableau中做下面的事情 如果选择的关键数字是销售额或销售量(非聚合字段),则调用calculation1 else calculation 2 IF ATTR([Choose Key Figure])= "Sales" THEN [Periodic Calculation] ELSEIF ATTR([Choose Key Figure])= "Volume" THEN [Periodic Calculation] ELSEIF ATTR([Choose Key Figure])="

我试着在Tableau中做下面的事情

如果选择的关键数字是销售额或销售量(非聚合字段),则调用calculation1 else calculation 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]
如果[选择音量/NNS]=“NNS”
和[选择时段]=-1
和DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际值+计划值)NNS]
ELSEIF[选择音量/NNS]=“音量”
和[选择时段]=-1
和DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际+计划)体积]
ELSEIF[选择音量/NNS]=“NNS”
和[选择时段]=-3
和DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际值+计划值)NNS]
ELSEIF[选择音量/NNS]=“音量”
和[选择时段]=-3
和DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际+计划)体积]
ELSEIF[选择音量/NNS]=“NNS”
和[选择时段]=-6
和DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际值+计划值)NNS]
ELSEIF[选择音量/NNS]=“音量”
和[选择时段]=-6
和DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际+计划)体积]
ELSEIF[选择音量/NNS]=“NNS”
并且[选择时段]=1
和DATEDIFF(“月”,“日期],[选择当前月])-2
然后[(实际值+计划值)NNS]
ELSEIF[选择音量/NNS]=“音量”
并且[选择时段]=1
和DATEDIFF(“月”,“日期],[选择当前月])-2
然后[(实际+计划)体积]
ELSEIF[选择音量/NNS]=“NNS”
和[选择时段]=3
和DATEDIFF(“月”,“日期],[选择当前月])-4
然后[(实际值+计划值)NNS]
ELSEIF[选择音量/NNS]=“音量”
和[选择时段]=3
和DATEDIFF(“月”,“日期],[选择当前月])-4
然后[(实际+计划)体积]
ELSEIF[选择音量/NNS]=“NNS”
和[选择时段]=6
和DATEDIFF(“月”,[日期],[选择当前月])-7
然后[(实际值+计划值)NNS]
ELSEIF[选择音量/NNS]=“音量”
和[选择时段]=6
和DATEDIFF(“月”,[日期],[选择当前月])-7
然后[(实际+计划)体积]
结束

定期计算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
如果[选择IGM$/NNSP]=“NNSP”
和[选择时段]=-1
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际值+计划值)NNSP]
ELSEIF[选择IGM$/NNSP]=“IGM$”
和[选择时段]=-1
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际值+计划值)IGM$]
ELSEIF[选择IGM$/NNSP]=“NNSP”
和[选择时段]=-3
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际值+计划值)NNSP]
ELSEIF[选择IGM$/NNSP]=“IGM$”
和[选择时段]=-3
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际值+计划值)IGM$]
ELSEIF[选择IGM$/NNSP]=“NNSP”
和[选择时段]=-6
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际值+计划值)NNSP]
ELSEIF[选择IGM$/NNSP]=“IGM$”
和[选择时段]=-6
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])0
然后[(实际值+计划值)IGM$]
ELSEIF[选择IGM$/NNSP]=“NNSP”
并且[选择时段]=1
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])-2
然后[(实际值+计划值)NNSP]
ELSEIF[选择IGM$/NNSP]=“IGM$”
并且[选择时段]=1
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])-2
然后[(实际值+计划值)IGM$]
ELSEIF[选择IGM$/NNSP]=“NNSP”
和[选择时段]=3
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])-4
然后[(实际值+计划值)NNSP]
ELSEIF[选择IGM$/NNSP]=“IGM$”
和[选择时段]=3
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])-4
然后[(实际值+计划值)IGM$]
ELSEIF[选择IGM$/NNSP]=“NNSP”
和[选择时段]=6
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])-7
然后[(实际值+计划值)NNSP]
ELSEIF[选择IGM$/NNSP]=“IGM$”
和[选择时段]=6
和ATTR(DATEDIFF(“月”,“日期],[选择当前月])-7
然后[(实际值+计划值)IGM$]
结束
有人能帮助解决这个错误吗


谢谢。

如果[Choose Key Figure]是一个参数,[Periodic Calculation]和[Periodic Calculation 2]是行级(非聚合)计算,那么您可以:

  • 删除计算字段中对ATTR()的所有调用 或
  • 删除对ATTR()的所有调用,并在对[Periodic Calculation]或[Periodic Calculation 2]的每次引用上调用聚合函数,如SUM() 或
  • 保留对ATTR()的调用,并在每次引用[Periodic Calculation]或[Periodic Calculation 2]时调用聚合函数,如SUM()
  • 1和2之间的区别在于是在计算中还是在使用它的Tableau工作簿工具架中为计算字段指定聚合函数

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