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
ABR002
Helper I
Helper I

Time Span between 2 rows with conditions

This honestly caused me a headache even summing it up to where I hope someone else can understand. 
I have a table that tracks the logs of a system, there are several tags that occur, some result in a status of down, some result in a status of up:

TimeTagSystem Status
8:45OnUp
8:46ReadyUp
8:49Shut Downdown
8:51Restartdown
8:53ActiveUp
8:57RunningUp
9:00Not ReadyDown
9:01Shut DownDown
9:02Restartdown
9:10OnUp
9:15ReadyUp
9:21OffDown

 

I need to calculate the time it spends in Up and Down status. Here is the desired result:

Time2nd TimeStatusTime in Status
8:458:50Up5
8:508:53Down3
8:539:00Up7
9:009:10Down10
9:109:21Up11

 

The issue I'm running into is that there are consecutive Up satuses and consecutive down statuses. I need to take the earliest time it was in an up status and compare it with the first instance of a down status, and from there the first instance of an up status, so the time it was triggerred as being down to the time it was triggerred as being up. I basically don't want the times that are crossed out, would like to just delete those rows. So for the first time it went down, 8:49, I would want to calculate the time to 8:53, the time it said it was down, I don't need 8:51, as the system was already down when that entry came up.

In searching this board I actually found a solution that pretty much got me to the five yard line:

I created 2 index columns, first one from 0, second one from 1. Then I merged the table with itself using those 2 index columns on a left outer merge. From there I made a conditional column to eliminate all rows where the 2 system status column values were the same. 

This would be a perfect solution if the logs simply went from Up, Down, Up, Down, Up, Down, Up~, but they don't, the consecutive instances cause this to happen:

TimeAdded Index1.Time System StatusTime in Status
8:458:46 Up1
8:498:51 down2
8:538:57 Up4
9:009:01 Down1
9:109:15 Up5

 

For the time of the status change, I get the most recent entry with the up or down tag. So using the bold rows as an example, the 8:49 time is correct, but the 8:51 is not, as it's a down status that came after a down status, so I would want 8:53 to be there, which is an up status

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Your sample data doesn't seem to match your explanation.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsrAyMVXSUfLPAxKhBUqxOmAhMyAvKDUxpRJF1BLIC84oLVFwyS8HqQdTEDlTQ7CO4pLEohI0GWMg3zG5JLMsFdkwU3OQhtK8vMy8dIS4pZWBAZDnl1+iALMebhRQzhCXA4ByRtgdYGllaIDmP6CQKab/LK2MQMb7p6XBtccCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Time = _t, Tag = _t, #"System Status" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time", type time}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"System Status"}, {{"Time", each List.Min([Time]), type nullable time}},GroupKind.Local)
in
    #"Grouped Rows"

 

Use Grouping with GroupKind.Local

 

lbendlin_0-1727298566828.png

 

View solution in original post

2 REPLIES 2
ABR002
Helper I
Helper I

Thanks, works great

lbendlin
Super User
Super User

Your sample data doesn't seem to match your explanation.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsrAyMVXSUfLPAxKhBUqxOmAhMyAvKDUxpRJF1BLIC84oLVFwyS8HqQdTEDlTQ7CO4pLEohI0GWMg3zG5JLMsFdkwU3OQhtK8vMy8dIS4pZWBAZDnl1+iALMebhRQzhCXA4ByRtgdYGllaIDmP6CQKab/LK2MQMb7p6XBtccCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Time = _t, Tag = _t, #"System Status" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time", type time}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"System Status"}, {{"Time", each List.Min([Time]), type nullable time}},GroupKind.Local)
in
    #"Grouped Rows"

 

Use Grouping with GroupKind.Local

 

lbendlin_0-1727298566828.png

 

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.

Top Solution Authors