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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
alzhf
Frequent Visitor

Calculating occupancy at specific times

I've got a table with occupancy data and I need to calculate the occupancy at specific times. The table looks something like this

 

inout
01.01.2020 01:00           01.01.2020 23:00       
01.01.2020 02:0003.01.2020 12:00
02.01.2020 07:0002.01.2020 23:00

 

What I now want is a new table where I can see the occupancy at every day of the year, e.g.

 

dateocc 06:00occ 22:00
01.01.2020          2                                   

2                                   

02.01.20201

2

03.01.20201

1

I most likely need a date-table for this, but I don't really know how to approach this problem in detail, so every help or idea is greatly appreciated.

2 REPLIES 2
ImkeF
Community Champion
Community Champion

Hi @alzhf ,
you should create one large table with one row for each interval you want to observe. 
Then create a calendar table and a time table separately. So make sure that your large table has date and time in different columns.
Your desired result table would then be a matrix with a simple count-measure in it (COUNTROWS(FactTable)).
If you need help building this large table, please let me know if you need anything else than 6:00 and 22:00 occupancies.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

alzhf
Frequent Visitor

Hey Imke,

I solved it by using a simple COUNTROWS, just as you suggested. 
I created a Date Table with a custom date format and inserted a calcuated column with this code.

 

occupancy = COUNTROWS(
    FILTER(
        TABLE,AND(
            TABLE[IN DATE]<=DATETABLE[DATE],
            TABLE[OUT DATE]>=DATETABLE[DATE])))

 

This works as intended.

 

Do you know a way to extract the IDs for the correlating time stamps? Right now, I can only count the specific IDs/lines from the table, but I don't have access to the specific IDs.

Maybe by unpivoting the Date table and listing the specific IDs under the dates? 


I'm currently trying to do this with this code:

CM eff Midnight = 
var _rangeStart= MIN('Date Slicer'[Date])
var _rangeEnd= MAX('Date Slicer'[Date])
return
CALCULATE( 
    SUMX(Table2,Table2[Value]),((Table1[DATE in]) >= _rangeStart),((Table1[DATE out]) <= _rangeEnd))

but I'm not getting the correct result. Do you see my mistake here?

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.