title: Resolve aggregate and Non-aggregate mix error for IF results in Tableau
body: <p>I am trying to do the following in Tableau.</p>
  
  <p>If the chosen key figure is sales or volume (non-aggregate fields) then call calculation1 else calculation 2.</p>
  
  <pre><code>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 
  </code></pre>
  
  <p>But I am getting the following error:
  "Cannot mix aggregate and non-aggregate comparisons or results in 'IF' expressions."</p>
  
  <p>Periodic Calculation</p>
  
  <pre><code>IF [Choose Vol/NNS]="NNS"
  AND [Choose Period]=-1
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;2
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;0
  then [(Actuals+Planned) NNS]
  ELSEIF [Choose Vol/NNS]="Volume"
  AND    [Choose Period]=-1
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;2
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;0
  THEN [(Actuals + Planned) Volume]
  ELSEIF [Choose Vol/NNS]="NNS"
  AND [Choose Period]=-3
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;4
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;0
  then [(Actuals+Planned) NNS]
  ELSEIF [Choose Vol/NNS]="Volume"
  AND [Choose Period]=-3
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;4
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;0
  THEN [(Actuals + Planned) Volume]
  ELSEIF [Choose Vol/NNS]="NNS"
  AND [Choose Period]=-6
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;7
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;0
  then [(Actuals+Planned) NNS]
  ELSEIF [Choose Vol/NNS]="Volume"
  AND     [Choose Period]=-6
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;7
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;0
  THEN [(Actuals + Planned) Volume]
  ELSEIF [Choose Vol/NNS]="NNS"
  AND     [Choose Period]=1
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;0
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;-2
  THEN [(Actuals+Planned) NNS]
  ELSEIF [Choose Vol/NNS]="Volume"
  AND     [Choose Period]=1
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;0
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;-2
  THEN [(Actuals + Planned) Volume]
  ELSEIF [Choose Vol/NNS]="NNS"
  AND     [Choose Period]=3
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;0
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;-4
  THEN [(Actuals+Planned) NNS]
  ELSEIF [Choose Vol/NNS]="Volume"
  AND     [Choose Period]=3
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;0
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;-4
  THEN [(Actuals + Planned) Volume]
  ELSEIF [Choose Vol/NNS]="NNS"
  AND     [Choose Period]=6
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;0
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;-7
  THEN [(Actuals+Planned) NNS]
  ELSEIF [Choose Vol/NNS]="Volume"
  AND     [Choose Period]=6
  AND DATEDIFF("month",[Dates], [Choose Current Month])&lt;0
  AND
  DATEDIFF("month",[Dates],[Choose Current Month] )&gt;-7
  THEN [(Actuals + Planned) Volume]
  </code></pre>
  
  <p>END</p>
  
  <p>Periodic Calculation 2</p>
  
  <pre><code>IF [Choose IGM$/NNSP]="NNSP"
  AND [Choose Period]=-1
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;2
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;0
  THEN [(Actuals + Planned) NNSP]
  ELSEIF [Choose IGM$/NNSP]="IGM$"
  AND [Choose Period]=-1
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;2
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;0
  THEN [(Actuals + Planned) IGM$]
  ELSEIF [Choose IGM$/NNSP]="NNSP"
  AND [Choose Period]=-3
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;4
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;0
  THEN [(Actuals + Planned) NNSP]
  ELSEIF [Choose IGM$/NNSP]="IGM$"
  AND [Choose Period]=-3
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;4
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;0
  THEN [(Actuals + Planned) IGM$]
  ELSEIF [Choose IGM$/NNSP]="NNSP"
  AND [Choose Period]=-6
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;7
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;0
  THEN [(Actuals + Planned) NNSP]
  ELSEIF [Choose IGM$/NNSP]="IGM$"
  AND [Choose Period]=-6
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;7
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;0
  THEN [(Actuals + Planned) IGM$]
  ELSEIF [Choose IGM$/NNSP]="NNSP"
  AND [Choose Period]=1
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;0
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;-2
  THEN [(Actuals + Planned) NNSP]
  ELSEIF [Choose IGM$/NNSP]="IGM$"
  AND [Choose Period]=1
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;0
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;-2
  THEN [(Actuals + Planned) IGM$]
  ELSEIF [Choose IGM$/NNSP]="NNSP"
  AND [Choose Period]=3
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;0
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;-4
  THEN [(Actuals + Planned) NNSP]
  ELSEIF [Choose IGM$/NNSP]="IGM$"
  AND [Choose Period]=3
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;0
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;-4
  THEN [(Actuals + Planned) IGM$]
  ELSEIF [Choose IGM$/NNSP]="NNSP"
  AND [Choose Period]=6
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;0
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;-7
  THEN [(Actuals + Planned) NNSP]
  ELSEIF [Choose IGM$/NNSP]="IGM$"
  AND [Choose Period]=6
  AND ATTR(DATEDIFF("month",[Dates], [Choose Current Month]))&lt;0
  and ATTR(DATEDIFF("month",[Dates],[Choose Current Month] ))&gt;-7
  THEN [(Actuals + Planned) IGM$]
  END
  </code></pre>
  
  <p>Can anyone help with how to work around this error?</p>
  
  <p>Thanks.</p>