tableau-desktop

CountD of employees who were active only during date range specified?


I have a workbook where I am trying to countD a 1 or a 0 when a workers hire/term (if they termed) date fall within a start/end date parameter range.

I filtered to an employee who was hired on 6/1/21 and didn't terminate. Based on the date parameters on the right, it should be a 0 counted, but it's a 1? The date range is 2020, so the employee wasn't hired yet.

enter image description here

COUNTD(
IF 
    Year([Hire Date])>=Year([Start Date]) and ISNULL([Termination Date (Blank for Currently Active)])
    OR  [Hire Date]<[Start Date] and ISNULL([Termination Date (Blank for Currently Active)])
    OR [Hire Date]>=[Start Date] and [Termination Date (Blank for Currently Active)]<=[End Date]
THEN
    [Employee ID]
END
)

Solution

  • Because you are using OR evaluators, this will always return as True and give an Employee ID whether the [Hire Date] is greater or less than the [Start Date].

    Since we are just checking hire date, we do not need Termination Date to compare against. I'd rewrite this to look something like:

    IF [Hire Date]>= [Start Date] AND [Hire Date]<= [End Date]
    THEN [Employee ID]
    END
    

    This checks if the [Hire Date] is in your date range, if it is, it returns [Employee ID], if it isn't, it does nothing. Take the COUNTD of this and you should be good to go.

    You could also use the same logic to return a 1 or 0 and then just take the SUM of this calculated field. It works almost the same:

    IF [Hire Date]>= [Start Date] AND [Hire Date]<= [End Date]
    THEN 1
    ELSE 0
    END
    

    Or equivalently and even shorter

    INT([Hire Date]>= [Start Date] AND [Hire Date]<= [End Date])