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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Davebryant1
Frequent Visitor

Reporting occupancy numbers on a monthly basis

Hi,

Im looking for some help reporting tenancy occupancy numbers on the last day of the month for specific accommodation. This will include tenancys that have closed and are still open. Below is a sample of the data

 

Table 1

Accommodation TypeStart DateEnd Date
Accommodation type 101/04/202215/04/2022
Accommodation type 116/04/2022 
Accommodation type 130/04/2022 
Accommodation type 115/05/202220/05/2022
Accommodation type 101/06/2022 
Accommodation type 129/06/202204/07/2022
Accommodation type 101/09/202229/09/2022
Accommodation type 202/09/2022 
Accommodation type 201/04/2022 
Accommodation type 231/03/202204/04/2022

 

I have a calendar table as follows

 

DateMonth
01-Apr-22Apr
02-Apr-22Apr
03-Apr-22Apr
04-Apr-22Apr
05-Apr-22Apr
06-Apr-22Apr
07-Apr-22Apr
08-Apr-22Apr
09-Apr-22Apr
10-Apr-22Apr

 

This is what im trying to output

 

 AprilMayJuneJulyAugustSeptember
Number of occupants at the end of the month in Accommondation Type 1224333

 

I've tried various code but cant seem to crack it. Any advice or pointers would be much appreciated.

 

Thanks

Dave

1 ACCEPTED SOLUTION

You can add another filter into the CALCULATE

Occupancy1 =
VAR ReferenceDate =
    MAX ( 'dimDate (start Date)'[Endofmonthdate] )
VAR Result =
    CALCULATE (
        COUNTROWS ( Tenancies ),
        REMOVEFILTERS ( 'dimDate (start Date)' ),
        'Tenancies'[Tenancy Start Date] <= ReferenceDate
            && (
                ISBLANK ( Tenancies[Tenancy End Date] )
                    || Tenancies[Tenancy End Date] > ReferenceDate
            ),
        'Tenancies'[Accomodation type] = "Accomodation type 1"
    )
RETURN
    Result

View solution in original post

15 REPLIES 15
Davebryant1
Frequent Visitor

Thanks for the speedy response John. 

I've had a go but not getting the results im expecting, this is your code with my real table names

 

Occupancy =
VAR ReferenceDate =
    MAX ( 'dimDate (start Date)'[Date] )
VAR Result =
    CALCULATE (
        COUNTROWS ( Tenancies ),
        'Tenancies'[Tenancy Start Date] <= ReferenceDate
            && (
                ISBLANK ( Tenancies [Tenancy End Date] )
                    || Tenancies[Tenancy End Date] > ReferenceDate
            )
    )
RETURN
    Result
 
These are the results im gettng but, i should be getting results in the region of 500 each month. 
Davebryant1_0-1666783937124.png
My calendar table (dimDate (start Date)) is joined to the Tenancies table on the tenancy start date field. Could this be causing issues?
 
Many Thanks

Yes, the filter on date will be affecting the results. Try

Occupancy =
VAR ReferenceDate =
    MAX ( 'dimDate (start Date)'[Date] )
VAR Result =
    CALCULATE (
        COUNTROWS ( Tenancies ),
        REMOVEFILTERS ( 'dimDate (start Date)' ),
        'Tenancies'[Tenancy Start Date] <= ReferenceDate
            && (
                ISBLANK ( Tenancies[Tenancy End Date] )
                    || Tenancies[Tenancy End Date] > ReferenceDate
            )
    )
RETURN
    Result

Thanks John, i think i've cracked it.

Davebryant1_0-1666795019699.png

 

I had to create a new collumn in the date table that shows the last day of the month and amend your code to

 

Occupancy1 =
VAR ReferenceDate =
    MAX ( 'dimDate (start Date)'[Endofmonthdate] )
VAR Result =
    CALCULATE (
        COUNTROWS ( Tenancies ),
        REMOVEFILTERS ( 'dimDate (start Date)' ),
        'Tenancies'[Tenancy Start Date] <= ReferenceDate
            && (
                ISBLANK ( Tenancies[Tenancy End Date] )
                    || Tenancies[Tenancy End Date] > ReferenceDate
            )
    )
RETURN
    Result

 

My Final question now is how do i add a filter into it? As per my origional post i only want to count those rows that are "Accommodation type 1"

 

Many Thanks

You can add another filter into the CALCULATE

Occupancy1 =
VAR ReferenceDate =
    MAX ( 'dimDate (start Date)'[Endofmonthdate] )
VAR Result =
    CALCULATE (
        COUNTROWS ( Tenancies ),
        REMOVEFILTERS ( 'dimDate (start Date)' ),
        'Tenancies'[Tenancy Start Date] <= ReferenceDate
            && (
                ISBLANK ( Tenancies[Tenancy End Date] )
                    || Tenancies[Tenancy End Date] > ReferenceDate
            ),
        'Tenancies'[Accomodation type] = "Accomodation type 1"
    )
RETURN
    Result

Hi @johnt75 ,

Im after some more help with adapting the code you've already provided if possible? If you want me to post as a new thread let me know...

 

I now need to report on the same paramaters as before but i need to know as at the end of each month how many tenancys had been active for 4 weeks or more.

 

I have had an inital go as below but the number of days in tenancy is calculating the overall number and not as at the end of each month. 

 

Tenancy Over 4 weeks = 
VAR ReferenceDate =
    MAX ( 'dimDate (start Date)'[MonthDate] )
VAR Result =
    CALCULATE (
        COUNTROWS (Tenancies ),
        REMOVEFILTERS ( 'dimDate (start Date)' ),
        Tenancies[Tenancy Start Date] <= ReferenceDate
            && (
                ISBLANK ( Tenancies[Tenancy End Date] )
                    || Tenancies[Tenancy End Date] > ReferenceDate
            ),
        Properties[property type] = "Type1",
        Tenancies[Number of days in tenancy] >28
        
    )
RETURN
    Result

 

Thanks

 

 

what's the code for the Number of days in tenancy?

That collumn is a system generated field that produces the number of days the Tenancy is/was active. It perorms 2 calulations end date - start date and if end date = blank, Todays date-Start date.

That's where the problem is. You don't want to calculate it as at today, you want to calculate it as at the end of the month in question.

Try

Tenancy Over 4 weeks =
VAR ReferenceDate =
    MAX ( 'dimDate (start Date)'[MonthDate] )
VAR Result =
    CALCULATE (
        COUNTROWS (
            KEEPFILTERS (
                FILTER (
                    Tenancies,
                    DATEDIFF (
                        Tenancies[Tenancy Start Date],
                        COALESCE ( Tenancies[Tenancy End Date], ReferenceDate ),
                        DAY
                    ) > 28
                )
            )
        ),
        REMOVEFILTERS ( 'dimDate (start Date)' ),
        Tenancies[Tenancy Start Date] <= ReferenceDate
            && (
                ISBLANK ( Tenancies[Tenancy End Date] )
                    || Tenancies[Tenancy End Date] > ReferenceDate
            ),
        Properties[property type] = "Type1"
    )
RETURN
    Result

Hi John, 

 

I've tried to get the code working but still strugggling. I think its failing on those tenancies that have an end date. Its calculating those with an end date as end date - start date at each date point rather than weekly date - start date. E.g

 

 

Case IDTenancy Start DateTenancy End DateAs at 22/08/2022Days Tenancy Open as at 22/8
22783019/08/2022 22/08/20223
23782318/08/202217/10/202222/08/20224
21128517/06/2022 22/08/202266


based on the above data as at 22/8 it should only count ID211285 but its also counting ID237823.

 

Davebryant1_0-1667255053832.png

 

 

Here's my latest code, i've changed it to a weekly view from monthly now.

 

 

Tenancy Over 4 weeks = 
VAR ReferenceDate =
    MAX ( 'dimDate (start Date)'[WeekDate])
VAR Result =
    CALCULATE (
        COUNTROWS (
            FILTER (
                KEEPFILTERS ( Tenancies ),
                DATEDIFF (
                   Tenancies[Tenancy Start Date],
                    COALESCE (Tenancies[Tenancy End Date], ReferenceDate ),
                    DAY
                ) > 28
            )
        ),
        REMOVEFILTERS ( 'dimDate (start Date)' ),
        Tenancies[Tenancy Start Date]<= ReferenceDate
            && (
                ISBLANK (Tenancies[Tenancy End Date])
                    || Tenancies[Tenancy End Date] > ReferenceDate
            ),
        Properties[property type] = "Type1"
        
    )
RETURN
    Result

 

 

Any ideas?

Try

Tenancy Over 4 weeks = 
VAR ReferenceDate =
    MAX ( 'dimDate (start Date)'[WeekDate])
VAR Result =
    CALCULATE (
        COUNTROWS (
            FILTER (
                KEEPFILTERS ( Tenancies ),
                DATEDIFF (
                   Tenancies[Tenancy Start Date],
                    MINX( {Tenancies[Tenancy End Date], ReferenceDate}, [Value] ),
                    DAY
                ) > 28
            )
        ),
        REMOVEFILTERS ( 'dimDate (start Date)' ),
        Tenancies[Tenancy Start Date]<= ReferenceDate
            && (
                ISBLANK (Tenancies[Tenancy End Date])
                    || Tenancies[Tenancy End Date] > ReferenceDate
            ),
        Properties[property type] = "Type1"
        
    )
RETURN
    Result

Thanks John, tried that code but am getting the error below:

 

KEEPFILTERS function can only be used as a top level filter argument of CALCULATE and CALCULATETABLE or with a table argument of a function performing a table scan.

I always get them the wrong way round. Try

Tenancy Over 4 weeks =
VAR ReferenceDate =
    MAX ( 'dimDate (start Date)'[MonthDate] )
VAR Result =
    CALCULATE (
        COUNTROWS (
            FILTER (
                KEEPFILTERS ( Tenancies ),
                DATEDIFF (
                    Tenancies[Tenancy Start Date],
                    COALESCE ( Tenancies[Tenancy End Date], ReferenceDate ),
                    DAY
                ) > 28
            )
        ),
        REMOVEFILTERS ( 'dimDate (start Date)' ),
        Tenancies[Tenancy Start Date] <= ReferenceDate
            && (
                ISBLANK ( Tenancies[Tenancy End Date] )
                    || Tenancies[Tenancy End Date] > ReferenceDate
            ),
        Properties[property type] = "Type1"
    )
RETURN
    Result

Thanks John, its returning figures not but not what i am expecting. I need to investigate more to understand whats going on.

 

Thanks for your assistance

Thanks John, just what i needed.

johnt75
Super User
Super User

You could create a measure like

Occupancy =
VAR ReferenceDate =
    MAX ( 'Date'[Date] )
VAR Result =
    CALCULATE (
        COUNTROWS ( 'Table 1' ),
        'Table 1'[Start date] <= ReferenceDate
            && (
                ISBLANK ( 'Table 1'[End date] )
                    || 'Table 1'[End date] > ReferenceDate
            )
    )
RETURN
    Result

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.