Forum Discussion

phil91's avatar
phil91
Frequent Visitor
4 years ago
Solved

DAX - count between 2 dates

Hi,   Hoping somebody could help me with the below problem.   I have a HR table of employees 'Merged Starters and Leavers' containing a start date and leaving date. I have a slicer on the report...
  • BA_Pete's avatar
    4 years ago

    Hi phil91 ,

     

    In terms of the active relationship, I guess this would be personal preference to some degree. If your visuals most-frequently utilise metrics based on [start date], then make this one active and vice-versa. If there's no difference, then I tend to make them all inactive to avoid confusion later on.

     

    Regarding number employed during the period, you'll need a value-over-time measure, something like this:

    _noofEmployed = 
    VAR date_to_examine =
    MAX(calendar[date])
    VAR noofEmployed =
    CALCULATE(
        CALCULATE(
            DISTINCTCOUNT( yourTable[employeeCode]),
            KEEPFILTERS( date_to_examine >= yourTable[start date]),
            KEEPFILTERS( date_to_examine <= yourTable[leave date])
        ),
        CROSSFILTER(calendar[date], yourTable[relatedDateFieldIfUsed], None)
    )
    RETURN
        IF (ISBLANK(noofEmployed ), BLANK(), noofEmployed )

     

    You'll notice that I've removed the crossfilter in this example as this works only when unrelated. If you make both of your relationships inactive, then you can remove the first CALCULATE and the CROSSFILTER line.

     

    Pete