tableau-apitableau-desktopcalculated-field

How to create a calculated field in Tableau for Aggregated and Non-Aggregated Fields


I have a date field which is a calculated field ("SalesDate_current_month", this will give values True or False) from original date column (SalesDate). Trying to create a calculated field as below, but getting this error "Cannot mix aggregate and non-aggregate comparisons or results in 'If' expressions".

IF[SalesDate_current_month] = True then SUM([Sales])/SUM([Employees_Per_Region]) END

Can anyone help me in this?


Solution

  • Either use ATTR to make SalesDate_current_month an aggregate or wrap the sum around the numerator and denominator.

    IF ATTR([SalesDate_current_month]) = True then SUM([Sales])/SUM([Employees_Per_Region]) END
    

    or

    SUM(IF [SalesDate_current_month] = True then [Sales] end) /
    SUM(IF [SalesDate_current_month] = True then [Employees_Per_Region] end)