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
pedanticpad
Helper II
Helper II

Count occurrences between 2 dates on a calendar table

Hi,

 

I have a table with StartDate and EndDate.  I need to count the number of occurances between the StartDate and EndDate on a calendar table along the lines of the below.  Any help would be greatly appreciated.

 

StartDateEndDate CalendarDateCount
01/12/201903/12/2019 01/12/20193
01/12/201904/12/2019 02/12/20194
01/12/201904/12/2019 03/12/20193
02/12/201908/12/2019 04/12/20191
   05/12/20191
   06/12/20191
   07/12/20191
   08/12/20190

 

Thanks

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @pedanticpad ,

Try this:

Count Column = 
CALCULATE (
    COUNTROWS ( 'Table' ) + 0,
    FILTER (
        'Table',
        'Table'[StartDate] <= EARLIER ( 'Calendar'[Date] )
            && 'Table'[EndDate] > EARLIER ( 'Calendar'[Date] )
    )
)

oc.PNG

 

Best Regards,

Icey

 

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

 

View solution in original post

9 REPLIES 9
Icey
Community Support
Community Support

Hi @pedanticpad ,

Try this:

Count Column = 
CALCULATE (
    COUNTROWS ( 'Table' ) + 0,
    FILTER (
        'Table',
        'Table'[StartDate] <= EARLIER ( 'Calendar'[Date] )
            && 'Table'[EndDate] > EARLIER ( 'Calendar'[Date] )
    )
)

oc.PNG

 

Best Regards,

Icey

 

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

 

Thanks so much! That works perfectly

Icey
Community Support
Community Support

Hi @pedanticpad ,

Try this:

1. Create relationships between the two tables.

count1.PNGcount2.PNG

2. Create a measure.

Count = 
COUNT ( 'Table'[StartDate] )
    + CALCULATE (
        COUNT ( 'Table'[EndDate] ),
        USERELATIONSHIP ( 'Calendar'[Date], 'Table'[EndDate] )
    ) + 0

count.PNG

 

Best Regards,

Icey

 

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

Thanks Icey, Is there any way to use that to create a table that I can add calculated columns to rather than a visual?

 

Also, is it possible to ammend your formula to pull in criteria?

Tahreem24
Super User
Super User

Or you can use below DAX:
Column = DATEDIFF(Table1[Dates1], Table1[Dates2], DAY)
Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard

Thanks for the reply. Those 2 solutions work to give me the number of days between the two dates, but not the number of instances between 2 dates. 

 

What I need is to be able to say on each day of the year we had x occupancy by using the StartDate and EndDate.

If it makes it any easier I was able to achieve it in MS Access using the below;

 

SELECT Import_CalendarDate.CalendarDate, Count(ID) AS TotalOccupancy
FROM Import_CalendarDate LEFT JOIN RoomOccupancy ON (Import_CalendarDate.CalendarDate>=RoomOccupancy.StartDate) AND (Import_CalendarDate.CalendarDate<RoomOccupancy.EndDate)
WHERE Import_CalendarDate.CalendarDate<date()
GROUP BY CalendarDate
ORDER BY Import_CalendarDate.CalendarDate;

What I got from your point is to count some occurances between 2 dates.

Measure =
CALCULATE (
SUM ( ValueTable[Value] ),
FILTER (
ValueTable,
ValueTable[Date] >= MIN( PeriodTable[StartDate] )
&& ValueTable[Date] <= MAX ( PeriodTable[EndDate] )
)
)

Please give Kudos and accept this as a solution if it helps you.
Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard

Hi,
&nbsp;
Tahreem,&nbsp; I can't get your formula to work at all.&nbsp; Icey, your formula is pulling in the wrong values.
&nbsp;
&nbsp;
Just to calrify the aim;
&nbsp;
Think of it as a hotel (field name, etc changes to make more sense with a hotel example).&nbsp; I have a list of Admission Dates, Discharge Dates and Room Numbers.&nbsp; I need to see the nightly occupancy for each day, so that if 5 rooms were occupied last night (regardless of their admission date) I would see an occupancy of 5.&nbsp; To add to that not all rooms will be occupied overnight (admit and discharge date the same) so I only need to count it if the admission date and discharge date are not the same.
&nbsp;
Thanks.
Tahreem24
Super User
Super User

Give a try using below calculated column :
Days Column =
SWITCH (
TRUE (),
'Table'[start_date] < 'Table'[end_date], DATEDIFF ( 'Table'[start_date], 'Table'[end_date], DAY ),
'Table'[start_date] > 'Table'[end_date], DATEDIFF ( 'Table'[end_date], 'Table'[start_date], DAY ) * -1,
0
)

Don't forget to hit Thumbs up and mark it as a solution if it helps you.
Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard

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.