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
Anonymous
Not applicable

Calculating occupied beds during month in slicer

Hi everyone,

 

I was hoping someone would be able to help me with something that I'm stuck on, please.

 

We're a care home provider and one of the KPIs we would like to track is the number of days residents stayed in the home (let's call it ResDays) but I'm new to PowerBI and at a bit of a loss with how to calculate it. 

 

The report I'm building has 3 tables that I think are relevant to this question - DateTable, cv_BI_Occupancy and cv_facilities. They are laid out as below:

 

cv_facilities:

 

nameAddress
Care Home A123 XYZ
Care Home B456 ABC
Care Home C789 DEF

 

DateTable (dates run from 1 Jan 2020 to 31 Dec 2021):

DateMonthYearMonthYearNoFinancial Year
01 January 2020Jan 20202020012020
02 January 2020Jan 20202020012020
03 January 2020Jan 20202020012020
04 January 2020Jan 20202020012020
05 January 2020Jan 20202020012020

 

cv_BI_Occupancy (the system generates a leave date of 31 December 2999 for any rooms that are currently occupied):

SiteRoomNoTypeOfStayServiceArrivalLeaveDate
Care Home A114PermanentAL21 September 201201 February 2020
Care Home A216PermanentDEM16 December 201918 January 2020
Care Home A117PermanentAL23 March 201902 January 2020
Care Home A214PermanentDEM13 October 201916 March 2020
Care Home AG01PermanentAL16 April 201531 December 2999
Care Home A114PermanentAL29 September 202031 December 2999
Care Home A207PermanentAL30 December 201931 December 2999
Care Home B215PermanentAL30 January 202025 August 2020
Care Home B111PermanentDEM13 October 201408 October 2020
Care Home B228PermanentAL19 June 202017 December 2020
Care Home B130PermanentDEM03 May 201330 November 2020
Care Home B304PermanentAL17 January 201931 December 2999
Care Home B404PermanentAL06 December 201831 December 2999
Care Home B302PermanentAL13 February 202031 December 2999
Care Home C118PermanentAL08 December 201906 November 2020
Care Home C112PermanentAL18 September 201309 May 2020
Care Home C120PermanentAL18 March 202025 December 2020
Care Home C113PermanentAL15 February 201208 January 2020
Care Home C003PermanentAL01 September 202031 December 2999
Care Home C310PermanentDEM22 October 202031 December 2999
Care Home C227PermanentAL15 June 202031 December 2999

 

The relationships set up are as follows:

DateTable[Date] to cv_BI_Occupancy[Leave Date] - 1 to many

cv_facilities[name] to cv_BI_Occupancy[Site] - 1 to many

 

The report I am trying to create will be for one care home at any given time, and for one period at any given time so there are two slicers synced to all pages, those being DateTable[MonthYear] and cv_facilities[name]. It will show financial and non-financial information so this ResDays calculation only forms a small but important part of the overall report.

 

I've replicated in Excel what I would expect my outcomes to be for two random periods, as well as how that would be made up from the table above:

 

 Resident Days in JulyResident Days in November
Care Home A6290
Care Home B217150
Care Home C93126

 

SiteRoomNoTypeOfStayServiceArrivalLeaveDateJul 2020Nov 2020
Care Home A114PermanentAL21 September 201201 February 202000
Care Home A216PermanentDEM16 December 201918 January 202000
Care Home A117PermanentAL23 March 201902 January 202000
Care Home A214PermanentDEM13 October 201916 March 202000
Care Home AG01PermanentAL16 April 201531 December 29993130
Care Home A116PermanentAL29 September 202031 December 2999030
Care Home A207PermanentAL30 December 201931 December 29993130
Care Home B215PermanentAL30 January 202025 August 2020310
Care Home B111PermanentDEM13 October 201408 October 2020310
Care Home B228PermanentAL19 June 202017 December 20203130
Care Home B130PermanentDEM03 May 201330 November 20203130
Care Home B304PermanentAL17 January 201931 December 29993130
Care Home B404PermanentAL06 December 201831 December 29993130
Care Home B302PermanentAL13 February 202031 December 29993130
Care Home C118PermanentAL08 December 201906 November 2020316
Care Home C112PermanentAL18 September 201309 May 202000
Care Home C120PermanentAL18 March 202025 December 20203130
Care Home C113PermanentAL15 February 201208 January 202000
Care Home C003PermanentAL01 September 202031 December 2999030
Care Home C310PermanentDEM22 October 202031 December 2999030
Care Home C227PermanentAL15 June 202031 December 29993130

 

I can create a column in the cv_BI_Occupancy table with a calculation that gives me the right figures if I hard code in a start and end date like this:

RESIDENT DAYS =
VAR STARTDATE = DATE(2020,07,01)
VAR ENDDATE = DATE(2020,07,31)
RETURN
IF(cv_BI_Occupancy[Arrival].[Date] > ENDDATE, 0,
IF(cv_BI_Occupancy[LeaveDate] < STARTDATE, 0,
VALUE(DATEDIFF( MAX( STARTDATE, cv_BI_Occupancy[Arrival].[Date]), MIN( cv_BI_Occupancy[LeaveDate], ENDDATE),DAY))))
 
But this becomes obsolete as soon as the period changes in the slicer so I'd like to reference a start and end date using the slicer if at all possible? I'm also sure that creating a column is not the best way to do it so is there a more efficient method? 
 
Any help at all would be gratefully received, thank you!
1 ACCEPTED SOLUTION
v-eqin-msft
Community Support
Community Support

Hi @Anonymous ,

 

To my knowledge, you could delete the relationhip between Date table and cv_BI_Occupancy table so that you could use the DateTable[Date] for Slicer. 

 

Then use the following formula to creata measure:

1. in cv_BI_Occupancy table

 

RESIDENT DAYS =
VAR _STARTDATE =
    MIN ( 'Date'[Date] )
VAR _ENDDATE =
    MAX ( 'Date'[Date] )
RETURN
    IF (
        MAX ( 'cv_BI_Occupancy'[Arrival] ) > _ENDDATE,
        0,
        IF (
            MAX ( 'cv_BI_Occupancy'[LeaveDate] ) < _STARTDATE,
            0,
            VALUE (
                DATEDIFF (
                    MAX ( MAX ( 'cv_BI_Occupancy'[Arrival] ), _STARTDATE ),
                    MIN ( MAX ( cv_BI_Occupancy[LeaveDate] ), _ENDDATE ),
                    DAY
                )
            )
        )
    )

 

2. in cv_facilities table

 

Measure =
SUMX ( 'cv_BI_Occupancy', [RESIDENT DAYS] )

 

The final output is shown below:

1.4.4.1.gif

Please take a look at the pbix file here.

 

Best Regards,
Eyelyn Qin
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

10 REPLIES 10
Anonymous
Not applicable

Hi all,

Thank you for the help and the example files! I'll have a look over the next couple of days and come back to confirm it's working as expected. 

Happy new year!

v-eqin-msft
Community Support
Community Support

Hi @Anonymous ,

 

To my knowledge, you could delete the relationhip between Date table and cv_BI_Occupancy table so that you could use the DateTable[Date] for Slicer. 

 

Then use the following formula to creata measure:

1. in cv_BI_Occupancy table

 

RESIDENT DAYS =
VAR _STARTDATE =
    MIN ( 'Date'[Date] )
VAR _ENDDATE =
    MAX ( 'Date'[Date] )
RETURN
    IF (
        MAX ( 'cv_BI_Occupancy'[Arrival] ) > _ENDDATE,
        0,
        IF (
            MAX ( 'cv_BI_Occupancy'[LeaveDate] ) < _STARTDATE,
            0,
            VALUE (
                DATEDIFF (
                    MAX ( MAX ( 'cv_BI_Occupancy'[Arrival] ), _STARTDATE ),
                    MIN ( MAX ( cv_BI_Occupancy[LeaveDate] ), _ENDDATE ),
                    DAY
                )
            )
        )
    )

 

2. in cv_facilities table

 

Measure =
SUMX ( 'cv_BI_Occupancy', [RESIDENT DAYS] )

 

The final output is shown below:

1.4.4.1.gif

Please take a look at the pbix file here.

 

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

Anonymous
Not applicable

Hi @v-eqin-msft I hope you don't mind me asking a follow up question here.

 

If I was to try and use the same method to calculate unit days instead of resident days I would need to omit some duplicate rows. Imagine 2 people sharing a room for the full length of a 30 day period, the measure at the moment would calculate 60 days occupied as each person would have their own entry in the system, which would be perfect for my original query, however if I wanted to know how many days that specific room was occupied, I would want the solution to only be 30 days.

 

Here's an example:

 

SiteRoomNoTypeOfStayServiceArrivalLeaveDateJul 2020Nov 2020
Care Home A114PermanentAL21 September 201901 October 2020310
Care Home A114PermanentAL21 September 201901 January 2021030

 

I was thinking I'd be able to copy the measures and just adjust a couple of things, so is this an easy amendment to make or is there a completely new solution that's needed?

Also, if it's easier I'm more than happy to create a new post rather than reopen an old and solved problem.

 

Thanks in advance!

Anonymous
Not applicable

Thanks Evelyn - that works perfectly. I was clearly being too stubborn with trying to keep the slicer format! 

Exactly the way I'm doing. And as I know, maybe it's the only way.

Hi @Anonymous , can u send me the PBIX file, I could give you some help. Because I got the same problems before.

Ashish_Mathur
Super User
Super User

Hi,

Would you want to analyse data only as far as a month?  i.e. would you ever want to get an answer to a particular date level such as occupancy as on July 26, 2020?  Also, would you be OK if in the back-end i treat 31 December 2999 as today's date? 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Thanks both.

 

Ashish, for this KPI it would only be for the full month and only ever for one month at a time. 

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Johanno
Responsive Resident
Responsive Resident

Hi, this is probably not a final solution but maybe some help on the way - a measure that counts the days that are within the selected month of the slicer:

 

Skärmklipp.PNG

Resident days selected month = 
VAR StartSelectedMonth = DATE(2020, SELECTEDVALUE(Dates[Month]), 1)
VAR EndSelectedMonth = DATE(2020, SELECTEDVALUE(Dates[Month])+1, 1) - 1
VAR Arrival = CALCULATE( MAX(cv_BI_Occupancy[Arrival]), ALL(Dates[Date])) //ignore the slicer
VAR Leave = CALCULATE( MAX(cv_BI_Occupancy[LeaveDate]), ALL(Dates[Date])) //ignore the slicer
VAR FilteredDateTable = FILTER(VALUES(Dates[Date]), 
                            Dates[Date] >= StartSelectedMonth && 
                            Dates[Date] <= EndSelectedMonth)
VAR Days = SUMX( FilteredDateTable, IF( Dates[Date] >= Arrival && Dates[Date] <= Leave, 1, 0))
RETURN Days

  

In general you should use a measure instead of calculated column whenever possible since it's calculated when needed instead of using memory. If it's a small model and if it solves your problem it won't be an issue.

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.