Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
arad33
Frequent Visitor

Find End Date from Same Column and Count Records

I'm working on an absence report with the below data set and would like to find the below

 

First and Last Date of the sickness period so in the first set it would be First date = 30/03/2022 Last Date = 08/04/2022 and the Days in that period would be 8.

 

I'm guessing once I can set the dates the counts would be a pretty simple DATEDIFF but I just can't figure how to extract the Start & End Date based on the change in Status?

 

Screenshot 2023-02-22 131316.png

4 REPLIES 4
arad33
Frequent Visitor

Thank you for the response once again and its now even closer its now just that it is returning the count on the first and last day of the sickness period so in the first part of your example I am getting a count of 8 on 03/30/2022 and on 04/08/2022.  Ideally I just want one return on the last date (04/08/2022) in this instance.

v-yiruan-msft
Community Support
Community Support

Hi @arad33 ,

If I understand correctly, you would like to get the number of sick date per period. If yes, you can create a measure as below to get it:

Count of sick days =
VAR _seldate =
    SELECTEDVALUE ( 'Table'[Date] )
VAR _empid =
    SELECTEDVALUE ( 'Table'[Employee ID] )
VAR _status =
    SELECTEDVALUE ( 'Table'[Status] )
VAR _preworkdate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Status] = "Worked"
                && 'Table'[Date] < _seldate
        )
    )
VAR _nextworkdate =
    CALCULATE (
        MIN ( 'Table'[Date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Status] = "Worked"
                && 'Table'[Date] > _seldate
        )
    )
VAR _minsickdate =
    CALCULATE (
        MIN ( 'Table'[Date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Status] = "Sick"
                && 'Table'[Date] <= _nextworkdate
                && 'Table'[Date] > _preworkdate
        )
    )
VAR _maxsickdate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Status] = "Sick"
                && 'Table'[Date] <= _nextworkdate
                && 'Table'[Date] > _preworkdate
        )
    )
RETURN
    IF (
        _status = "Sick",
        CALCULATE (
            DISTINCTCOUNT ( 'Table'[Date] ),
            FILTER (
                ALLSELECTED ( 'Table' ),
                'Table'[Date] >= _minsickdate
                    && 'Table'[Date] <= _maxsickdate
            )
        ),
        BLANK ()
    )

yingyinr_0-1677227308930.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi 

 

Thank you for your reply this is certainly as close as I have ever been and it is returning the data but I now can't get it to return a single line for each period. I have the date linked to a date table but when I use any of those it doesn't work and in the raw table its returning the value multiple times.  For example this user has 44 days total but obviously its returned 33, 33 times, 2 twice and 4, 4 times.  

 

Screenshot 2023-02-24 Multi return.png

If I do earliest date I get this

If I link to month in the date table I get thisEarliest.pngMonth.png

 

Ideally if I could get first date or last date of sickness with the number count that would be great, If I could get first and last with the count that would be dream world.

Hi  @arad33 ,

I updated my sample pbix file(see the attachment), please check if that is what you want. You can update the formula of measure as below to get it:

Count of sick days =
VAR _seldate =
    SELECTEDVALUE ( 'Table'[Date] )
VAR _empid =
    SELECTEDVALUE ( 'Table'[Employee ID] )
VAR _status =
    SELECTEDVALUE ( 'Table'[Status] )
VAR _preworkdate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Status] = "Worked"
                && 'Table'[Date] < _seldate
        )
    )
VAR _nextworkdate =
    CALCULATE (
        MIN ( 'Table'[Date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Status] = "Worked"
                && 'Table'[Date] > _seldate
        )
    )
VAR _minsickdate =
    CALCULATE (
        MIN ( 'Table'[Date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Status] = "Sick"
                && 'Table'[Date] <= _nextworkdate
                && 'Table'[Date] > _preworkdate
        )
    )
VAR _maxsickdate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Status] = "Sick"
                && 'Table'[Date] <= _nextworkdate
                && 'Table'[Date] > _preworkdate
        )
    )
RETURN
    IF (
        _status = "Sick"
            && _seldate IN { _minsickdate, _maxsickdate },
        CALCULATE (
            DISTINCTCOUNT ( 'Table'[Date] ),
            FILTER (
                ALLSELECTED ( 'Table' ),
                'Table'[Date] >= _minsickdate
                    && 'Table'[Date] <= _maxsickdate
            )
        ),
        BLANK ()
    )

yingyinr_0-1677742139342.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.