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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
GScott32
New Member

Calculating the time difference between rows based on a boolean value for occupancy sensors

Thank in advance for any help, quite an interesting problem I have.

 

I have occupancy sensors in my office that return a true or false state based on when the space is occupied or not. 

 

If the space becomes occupied then the sensor will add a true row to the dataset and when that space becomes free or un-occupied it will add a false row. See data Table.

 

GScott32_0-1619021727981.png

 
 
 
 
 
 
 
 
 
 

I need to calculate the time differnce between the true entry and the next false entry for that same sensor to tell me the total time occupied. I have seen other solutions where you calculate the differnece to the next row but wont always be the next entry for that sensor as the sensor may return enviromental data in the meantime. 

 

Here you can see the data for one sensor:

 

GScott32_1-1619022415995.png

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @GScott32,

You can calculate the difference between the current row content and the previous row, but power bi not support to use DAX expressions to direct add new records to current table. (you need to create a calculated table to manually add new records)

You can also try to use following formula if helps:

Measure =
VAR currDate =
    MAX ( Table[Date/Time] )
VAR currStatus =
    SELECTEDVALUE ( Table[Activity] )
VAR prevDate =
    CALCULATE (
        MAX ( Table[Date/Time] ),
        FILTER ( ALLSELECTED ( Table ), [Date/Time] < currDate && [Activity] = "TRUE" ),
        VALUES ( Table[DeviceName] )
    )
RETURN
    IF ( currStatus = "FALSE", DATEDIFF ( prevDate, curDate, SECOND ) )

Regards,

Xiaoxin Sheng

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @GScott32,

You can calculate the difference between the current row content and the previous row, but power bi not support to use DAX expressions to direct add new records to current table. (you need to create a calculated table to manually add new records)

You can also try to use following formula if helps:

Measure =
VAR currDate =
    MAX ( Table[Date/Time] )
VAR currStatus =
    SELECTEDVALUE ( Table[Activity] )
VAR prevDate =
    CALCULATE (
        MAX ( Table[Date/Time] ),
        FILTER ( ALLSELECTED ( Table ), [Date/Time] < currDate && [Activity] = "TRUE" ),
        VALUES ( Table[DeviceName] )
    )
RETURN
    IF ( currStatus = "FALSE", DATEDIFF ( prevDate, curDate, SECOND ) )

Regards,

Xiaoxin Sheng

amitchandak
Super User
Super User

@GScott32 , Try a new column like

 


new column =
var _next = minx(filter(Table, [devicename] = earlier([devicename]) && [Date/time] >earlier([Date/time]) && [Activity] =false()),[Date/time])
return
if([Activity] - true(), datediff([Date/time],_next, minute))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hello, 

Thank you for the potential solution. It is very close to working.

I am having one issue with it where not all of the occupied times line up: 

GScott32_1-1619082438564.png

 

 

GScott32_2-1619082447997.png

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.

Top Solution Authors