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
Phill360
New Member

Refer to another row with multiple conditions

Hi All.

 

I have a set of events where each line is either an On or Off event and I need to put the time stamp of each event in own column on the same row. The conditions are for each row with ON Value to find the next OFF value event and add that to a new column. Then remove all rows with off values. 

 

Orginal Data

TimeTypeOD CommentFN nameEntityValueUsername
11:21:40 08-May-2024EventStraddle K7SYSTEMO18005510/DI0OFF (0) 
11:20:27 08-May-2024EventStraddle K7SYSTEMO18005510/DI0ON (1) 
11:20:26 08-May-2024EventStraddle K7SYSTEMO18005510/DI0OFF (0) 
11:20:25 08-May-2024EventStraddle K7SYSTEMO18005510/DI0ON (1) 
11:19:19 08-May-2024EventStraddle K7SYSTEMO18005510/DI0OFF (0) 
11:18:10 08-May-2024EventStraddle K7SYSTEMO18005510/DI0ON (1) 
11:21:40 08-May-2024EventStraddle K8SYSTEMO18005510/DI0OFF (0) 
11:20:27 08-May-2024EventStraddle K8SYSTEMO18005510/DI0ON (1) 
11:20:26 08-May-2024EventStraddle K8SYSTEMO18005510/DI0OFF (0) 
11:20:25 08-May-2024EventStraddle K8SYSTEMO18005510/DI0ON (1) 
11:19:19 08-May-2024EventStraddle K8SYSTEMO18005510/DI0OFF (0) 
11:18:10 08-May-2024EventStraddle K8SYSTEMO18005510/DI0ON (1) 

 

Result

TypeOD CommentFN nameEntityOn TimeOff TimeUsername
EventStraddle K7SYSTEMO18005510/DI011:20:27 08-May-202411:21:40 08-May-2024 
EventStraddle K7SYSTEMO18005510/DI011:20:25 08-May-202411:20:26 08-May-2024 
EventStraddle K7SYSTEMO18005510/DI011:18:10 08-May-202411:19:19 08-May-2024 
EventStraddle K8SYSTEMO18005510/DI011:20:27 08-May-202411:21:40 08-May-2024 
EventStraddle K8SYSTEMO18005510/DI011:20:25 08-May-202411:20:26 08-May-2024 
EventStraddle K8SYSTEMO18005510/DI011:18:10 08-May-202411:19:19 08-May-2024 

 

 

Thanks for your help 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjS0MjK0MjFQMLDQ9U2s1DUyMDJR0lFyLUvNKwHSwSVFiSkpOakK3uYgXmRwiKsvkOFvaGFgYGpqaKDv4mkA4ru5KWgYaAJZCkqxOhBTDayMzCk01U9BwxDDUDOaONWU2k41tAQiqjvV0MLKkNK4wgxVohKABU0SAH5TyUwA5DiVcAIg0anEJQBSnUpcAiDaqbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Time = _t, Type = _t, #"OD Comment" = _t, #"FN name" = _t, Entity = _t, Value = _t, Username = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time", type datetime}, {"Type", type text}, {"OD Comment", type text}, {"FN name", type text}, {"Entity", type text}, {"Value", type text}, {"Username", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Replaced Value" = Table.ReplaceValue(#"Added Index",each [Index],each Number.RoundDown([Index]/2),Replacer.ReplaceValue,{"Index"}),
    #"Pivoted Column" = Table.Pivot(#"Replaced Value", List.Distinct(#"Replaced Value"[Value]), "Value", "Time"),
    #"Removed Other Columns" = Table.SelectColumns(#"Pivoted Column",{"Type", "OD Comment", "FN name", "Entity", "Username", "OFF (0)", "ON (1)"})
in
    #"Removed Other Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

View solution in original post

3 REPLIES 3
Fthrs_Analytics
Frequent Visitor

You could use Text.End in Power Query to extract the last characters of the column to create an ON/OFF column

lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjS0MjK0MjFQMLDQ9U2s1DUyMDJR0lFyLUvNKwHSwSVFiSkpOakK3uYgXmRwiKsvkOFvaGFgYGpqaKDv4mkA4ru5KWgYaAJZCkqxOhBTDayMzCk01U9BwxDDUDOaONWU2k41tAQiqjvV0MLKkNK4wgxVohKABU0SAH5TyUwA5DiVcAIg0anEJQBSnUpcAiDaqbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Time = _t, Type = _t, #"OD Comment" = _t, #"FN name" = _t, Entity = _t, Value = _t, Username = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time", type datetime}, {"Type", type text}, {"OD Comment", type text}, {"FN name", type text}, {"Entity", type text}, {"Value", type text}, {"Username", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Replaced Value" = Table.ReplaceValue(#"Added Index",each [Index],each Number.RoundDown([Index]/2),Replacer.ReplaceValue,{"Index"}),
    #"Pivoted Column" = Table.Pivot(#"Replaced Value", List.Distinct(#"Replaced Value"[Value]), "Value", "Time"),
    #"Removed Other Columns" = Table.SelectColumns(#"Pivoted Column",{"Type", "OD Comment", "FN name", "Entity", "Username", "OFF (0)", "ON (1)"})
in
    #"Removed Other Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

Thanks for the response. This worked perfectly,  now I need to understand what you've provided. 

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

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.