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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Pelas68
Frequent Visitor

How to calculate duration between connect time and disconnect time when they are in different rows

Hi,

 

Could someone to help me. I have table of multible logins per users with connect timestamp and disconnect timestamp. The event_data (colum 3) is allways unique with one connect and disconnect session pair. I would like to calculate duration between one connect and disconnect session pair per user. I know basically how to calculate duration between two times but this time the timestamps are in different rows so I am a little confused how to solve this issue...

 

Pelas68_0-1679304333202.png

 

 

 

2 ACCEPTED SOLUTIONS
KeyurPatel14
Resolver IV
Resolver IV

Hi @Pelas68 ,

Hope you are doing well.
Please try the below M Code and let me know if ther eis any queries:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7Py0tNLlHSUVIwMtc3NNQ3MjAyUjAysjI1tzIxBQobAgGQCjVUitXBp9zCytgIKGwEBCDlRmDlLpnFyTh1WFoZmGBYgFuHsZWBgZUBmh2xAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [event = _t, time = _t, data = _t, user = _t]),
#"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"time", type datetimezone}}, "en-IN"),
#"Added Index" = Table.AddIndexColumn(#"Changed Type with Locale", "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each Number.From([time])),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "time1", each try #"Added Index" [time] {[Index] + 1} otherwise null),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index", "Custom"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"event", "time", "time1", "data", "user"}),
#"Filtered Rows" = Table.SelectRows(#"Reordered Columns", each ([time1] <> null)),
#"Added Custom2" = Table.AddColumn(#"Filtered Rows", "Custom", each [time1] - [time])
in
#"Added Custom2"

I hope this will solve your problem and if this helps then give it a kudos and mark it as a solution.
Thank you.

Below is the image of expected result:

KeyurPatel14_0-1679307809809.png

 

View solution in original post

KeyurPatel14
Resolver IV
Resolver IV

If you want Disconnect in your event column then kindly paste below M Code in your Advanced Editor:

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7Py0tNLlHSUVIwMtc3NNQ3MjAyUjAysjI1tzIxBQobAgGQCjVUitXBp9zCytgIKGwEBCDlRmDlLpnFyTh1WFoZmGBYgFuHsZWBgZUBmh2xAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [event = _t, time = _t, data = _t, user = _t]),
#"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"time", type datetimezone}}, "en-IN"),
#"Added Index" = Table.AddIndexColumn(#"Changed Type with Locale", "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each Number.From([time])),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "time1", each try #"Added Index" [time] {[Index] + 1} otherwise null),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index", "Custom"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"event", "time", "time1", "data", "user"}),
#"Added Custom2" = Table.AddColumn(#"Reordered Columns", "Time Difference", each [time1] - [time])
in
#"Added Custom2"

Hope this will solve your problem and if it helps then please give it a kudos and accept it as a solution.
Thank you.

View solution in original post

2 REPLIES 2
KeyurPatel14
Resolver IV
Resolver IV

If you want Disconnect in your event column then kindly paste below M Code in your Advanced Editor:

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7Py0tNLlHSUVIwMtc3NNQ3MjAyUjAysjI1tzIxBQobAgGQCjVUitXBp9zCytgIKGwEBCDlRmDlLpnFyTh1WFoZmGBYgFuHsZWBgZUBmh2xAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [event = _t, time = _t, data = _t, user = _t]),
#"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"time", type datetimezone}}, "en-IN"),
#"Added Index" = Table.AddIndexColumn(#"Changed Type with Locale", "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each Number.From([time])),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "time1", each try #"Added Index" [time] {[Index] + 1} otherwise null),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index", "Custom"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"event", "time", "time1", "data", "user"}),
#"Added Custom2" = Table.AddColumn(#"Reordered Columns", "Time Difference", each [time1] - [time])
in
#"Added Custom2"

Hope this will solve your problem and if it helps then please give it a kudos and accept it as a solution.
Thank you.

KeyurPatel14
Resolver IV
Resolver IV

Hi @Pelas68 ,

Hope you are doing well.
Please try the below M Code and let me know if ther eis any queries:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7Py0tNLlHSUVIwMtc3NNQ3MjAyUjAysjI1tzIxBQobAgGQCjVUitXBp9zCytgIKGwEBCDlRmDlLpnFyTh1WFoZmGBYgFuHsZWBgZUBmh2xAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [event = _t, time = _t, data = _t, user = _t]),
#"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"time", type datetimezone}}, "en-IN"),
#"Added Index" = Table.AddIndexColumn(#"Changed Type with Locale", "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each Number.From([time])),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "time1", each try #"Added Index" [time] {[Index] + 1} otherwise null),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index", "Custom"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"event", "time", "time1", "data", "user"}),
#"Filtered Rows" = Table.SelectRows(#"Reordered Columns", each ([time1] <> null)),
#"Added Custom2" = Table.AddColumn(#"Filtered Rows", "Custom", each [time1] - [time])
in
#"Added Custom2"

I hope this will solve your problem and if this helps then give it a kudos and mark it as a solution.
Thank you.

Below is the image of expected result:

KeyurPatel14_0-1679307809809.png

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.