Forum Discussion

RichOB's avatar
RichOB
Post Partisan
1 year ago

Measure for day count between 2 dates

Hi, I have a kennel company and need to find the number of days where the kennels are empty per month

 

I have a table for the Property information, a table with the date/dog details, and have made a relational join between the Kennel number to filter by Project and Property eventually. 

 

In my real-world table, the start and end dates are not chronological in a table, so I've added a photo with an arrow to show what I mean. I need a measure that calculates the days between when 1 dog leaves a kennel at the Tenancy_End date and when another one joins the same kennel immediately afterward (Tenancy_Start).

Table1

Project PropertyKennelNumber 
Dog Trust1 Dog StreetDS_1
Dog Trust1 Dog StreetDS_2
Happy Canine4 Bone Drive BD_1
Happy Canine4 Bone Drive BD_2
Happy Canine4 Bone Drive BD_3
PAWS22 Sniff Avenue SA_1
PAWS22 Sniff Avenue SA_2
PAWS22 Sniff Avenue SA_3


Table2

Dog_Number KennelNumber Tenancy_StartTenancy_EndDays_Empty
Max1DS_101/04/2024
20/04/2024
4
Rex1DS_115/05/2024  
Piper1DS_125/04/202430/04/202415
Bob1DS_202/05/202401/09/202462
Tilly1DS_201/11/202407/11/20240
Biff1DS_211/11/2024  
Tank1DS_208/11/202410/11/20240
Alba1DS_301/06/202420/06/20245
MollieDS_325/06/2024  
Frank1SA114/03/202401/08/2024 
Dave1SA101/03/202410/03/20244


Max left DS_1 on 20/04/2024, Piper was the next dog in DS_1 on 25/04/2024 so it was empty for 4 days.

Rex is still in DS_1 and does not have an end.




Thanks so much for your help

13 Replies

  • Hiii RichOB 

    • Identify when a kennel is empty:

      • Find the latest Tenancy_End for a kennel.
      • Find the next Tenancy_Start for the same kennel.
      • Calculate the difference (days empty).
    • Handle missing end dates:

      • If a kennel is still occupied (no Tenancy_End), ignore it for now.



        Days_Empty =
        VAR CurrentKennel = SELECTEDVALUE(Table2[KennelNumber])
        VAR TenancyEndDates =
        FILTER(
        Table2,
        Table2[KennelNumber] = CurrentKennel &&
        NOT(ISBLANK(Table2[Tenancy_End]))
        )

        VAR NextStartDates =
        FILTER(
        Table2,
        Table2[KennelNumber] = CurrentKennel &&
        NOT(ISBLANK(Table2[Tenancy_Start]))
        )

        VAR EmptyDays =
        SUMX(
        TenancyEndDates,
        VAR EndDate = Table2[Tenancy_End]
        VAR NextStartDate =
        MINX(
        FILTER(NextStartDates, Table2[Tenancy_Start] > EndDate),
        Table2[Tenancy_Start]
        )
        RETURN IF(NOT(ISBLANK(NextStartDate)), DATEDIFF(EndDate, NextStartDate, DAY), 0)
        )

        RETURN EmptyDays

     

     

  • Hi RichOB ,

     

    To calculate the number of empty days per kennel, the DAX measure needs to identify gaps between when one dog leaves and the next one arrives. The measure first determines the Tenancy_End date of a dog in a specific kennel. Then, it looks for the earliest Tenancy_Start date of another dog that arrives after the Tenancy_End date in the same kennel. If a new dog starts immediately, the empty days would be zero; otherwise, the difference in days is calculated using DATEDIFF. If no new dog arrives, the measure returns blank.

    Days_Empty_Measure = 
    VAR CurrentKennel = SELECTEDVALUE(Table2[KennelNumber])
    VAR CurrentEndDate = MAX(Table2[Tenancy_End])
    VAR NextStartDate =
        CALCULATE(
            MIN(Table2[Tenancy_Start]),
            Table2[KennelNumber] = CurrentKennel,
            Table2[Tenancy_Start] > CurrentEndDate
        )
    RETURN 
        IF(NOT(ISBLANK(NextStartDate)), DATEDIFF(CurrentEndDate, NextStartDate, DAY), BLANK())
    

    For example, in Kennel DS_1, Max1 left on April 20, 2024, and Piper1 was the next to arrive on April 25, 2024, meaning the kennel was empty for four days. In Kennel DS_2, Bob1 left on September 1, 2024, and Tilly1 started on November 1, 2024, resulting in a 61-day gap. If a kennel remains unoccupied after the last recorded Tenancy_End date, the measure does not assign a value.

     

    Best regards,

    • lbendlin's avatar
      lbendlin
      Super User

      Consider using COUNTROWS(INTERSECT(Calendar1,Calendar2)) instead.  Much simpler than DATEDIFF.

    • RichOB's avatar
      RichOB
      Post Partisan

      Hi DataNinja777 , thanks for the detail here, I'm getting the individual numbers in a table view next to the KennelNumber which is great. Would you expect a date filter to work with this measure? I added the Tenancy_End date to a dropdown selection, if i select January for example everything goes blank.

       

      Thanks

  • RichOB's avatar
    RichOB
    Post Partisan

    Hi lbendlin thanks for your help. I tried this but I get an error saying "The start date or end date in Calendar function can not be BLANK value. Is this because some of the end dates haven't happened yet so those cells are blank?

    • lbendlin's avatar
      lbendlin
      Super User

      My code takes care of empty end dates. You would have to explain how empty start dates should be handled.

      • RichOB's avatar
        RichOB
        Post Partisan

        In reality there should always be a start date included, this would be down to human error if they are ever empty. Would there be a way to exclude any rows from your previous measure when there is no start date please? I think it would be best to entirely remove them from the equation if possible. Thanks for your help

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi RichOB,

    Thanks for reaching out to the Microsoft fabric community forum.

    Just following up to your previous conversation, I'd like to confirm if you've successfully resolved this issue or if you need further help.

    If yes, you are welcome to share your workaround and mark it as a solution so that other users can benefit as well. If you find a reply particularly helpful to you, you can also mark it as a solution.

     

    I would also take a moment to thank lbendlinKhushidesai0109 and DataNinja777, for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.

     

    If I misunderstand your needs or you still have problems on it, please feel free to let us know.  

    Best Regards,
    Hammad.
    Community Support Team

     

    If this post helps then please mark it as a solution, so that other members find it more quickly.

    Thank you.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi RichOB,

      As we haven’t heard back from you, so just following up to our previous message. I'd like to confirm if you've successfully resolved this issue or if you need further help.

      If yes, you are welcome to share your workaround and mark it as a solution so that other users can benefit as well. If you find a reply particularly helpful to you, you can also mark it as a solution.


      If you still have any questions or need more support, please feel free to let us know. We are more than happy to continue to help you.
      Thank you for your patience and look forward to hearing from you.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi RichOB,

        I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution so that other community members can find it easily.


        Thank you.