Forum Discussion

deanbland's avatar
deanbland
Helper III
5 years ago
Solved

Calculating Datediff between status changes

Hi, I have a somewhat complicated request...    I export a dataset each week that shows whether employees have clients or not (Benched = no client, Not Benched = has a client).    The state of ut...
  • Anonymous's avatar
    Anonymous
    5 years ago

    deanbland 

     

    You want a DAX calculated column or DAX measure or M? Here is a DAX column

     

     

    Column =
    VAR CurID = 'Table'[Employee ID]
    VAR CurBenched = 'Table'[Benched?]
    VAR CurDate = 'Table'[Date]
    VAR MaxDate =
        MAXX (
            FILTER (
                'Table',
                'Table'[Employee ID] = CurID
                    && 'Table'[Date] < CurDate
                    && 'Table'[Benched?] = "Not Benched"
            ),
            'Table'[Date]
        )
    VAR MinDate =
        IF (
            MaxDate = BLANK (),
            MINX ( FILTER ( 'Table', 'Table'[Employee ID] = CurID ), 'Table'[Date] ),
            MINX (
                FILTER ( 'Table', 'Table'[Employee ID] = CurID && 'Table'[Date] > MaxDate ),
                'Table'[Date]
            )
        )
    RETURN
        IF ( CurBenched = "Not Benched", BLANK (), DATEDIFF ( MinDate, CurDate, DAY ) )

     

  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi deanbland ,

    You can also create a measure as below base on Anonymous ' provided calculated column:

     

    Measurea =
    VAR _curemp =   SELECTEDVALUE ( 'Table'[Employee ID] )
    VAR _curdate =  SELECTEDVALUE ( 'Table'[Date] )
    VAR _nbenchdate =
        CALCULATE (
            MAX ( 'Table'[Date] ),
            FILTER (
                ALLSELECTED ( 'Table' ),
                'Table'[Employee ID] = _curemp
                    && 'Table'[Date] < _curdate
                    && 'Table'[Benched?] = "Not Benched"
            )
        )
    VAR _mindate =
        CALCULATE (
            MIN ( 'Table'[Date] ),
            FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Employee ID] = _curemp )
        )
    VAR _mindate2 =
        CALCULATE (
            MIN ( 'Table'[Date] ),
            FILTER (
                ALLSELECTED ( 'Table' ),
                'Table'[Employee ID] = _curemp
                    && 'Table'[Date] > _nbenchdate
            )
        )
    RETURN
        IF (
            ISBLANK ( _nbenchdate ),
            DATEDIFF ( _mindate, _curdate, DAY ),
            DATEDIFF ( _mindate2, _curdate, DAY )
        )

     

    If the above one is not working in your scenario, please provide the correct result with some example and calculation logic. Thank you.

    Best Regards