Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by watching the DP-600 session on-demand now through April 28th.
Learn moreJoin the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now
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.
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:
Solved! Go to Solution.
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
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
@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))
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:
Check out the April 2026 Power BI update to learn about new features.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
| User | Count |
|---|---|
| 41 | |
| 38 | |
| 32 | |
| 21 | |
| 15 |
| User | Count |
|---|---|
| 64 | |
| 59 | |
| 31 | |
| 27 | |
| 25 |