Forum Discussion
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 | Property | KennelNumber |
| Dog Trust | 1 Dog Street | DS_1 |
| Dog Trust | 1 Dog Street | DS_2 |
| Happy Canine | 4 Bone Drive | BD_1 |
| Happy Canine | 4 Bone Drive | BD_2 |
| Happy Canine | 4 Bone Drive | BD_3 |
| PAWS | 22 Sniff Avenue | SA_1 |
| PAWS | 22 Sniff Avenue | SA_2 |
| PAWS | 22 Sniff Avenue | SA_3 |
Table2
| Dog_Number | KennelNumber | Tenancy_Start | Tenancy_End | Days_Empty | |
| Max1 | DS_1 | 01/04/2024 |
| 4 | |
| Rex1 | DS_1 | 15/05/2024 | |||
| Piper1 | DS_1 | 25/04/2024 | 30/04/2024 | 15 | |
| Bob1 | DS_2 | 02/05/2024 | 01/09/2024 | 62 | |
| Tilly1 | DS_2 | 01/11/2024 | 07/11/2024 | 0 | |
| Biff1 | DS_2 | 11/11/2024 | |||
| Tank1 | DS_2 | 08/11/2024 | 10/11/2024 | 0 | |
| Alba1 | DS_3 | 01/06/2024 | 20/06/2024 | 5 | |
| Mollie | DS_3 | 25/06/2024 | |||
| Frank1 | SA1 | 14/03/2024 | 01/08/2024 | ||
| Dave1 | SA1 | 01/03/2024 | 10/03/2024 | 4 |
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
- Khushidesai0109Skilled Sharer
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
- If a kennel is still occupied (no Tenancy_End), ignore it for now.
- DataNinja777Super User
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,
- lbendlinSuper User
Consider using COUNTROWS(INTERSECT(Calendar1,Calendar2)) instead. Much simpler than DATEDIFF.
- RichOBPost 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
- lbendlinSuper User
My code takes care of empty end dates. You would have to explain how empty start dates should be handled.
- RichOBPost 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
- AnonymousNot 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 lbendlin, Khushidesai0109 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 TeamIf this post helps then please mark it as a solution, so that other members find it more quickly.
Thank you.
- AnonymousNot 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.- AnonymousNot 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.