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! Learn more

Reply
Anonymous
Not applicable

How to calculate time difference between concecutive rows efficiently

I'm using Power Query Online and I have about 60000 rows of data with columns session_user_email and session_start_time.

I need to calculate the time difference between concecutive rows for the same email between start_times. If the emails are different, value should be null. Data is already sorted.

I'm using the Index method but it is slow. This is my code:

 

#"Added index" = Table.AddIndexColumn(#"Renamed columns", "Index", 0, 1, Int64.Type),
  #"Added custom" =
Table.AddColumn(#"Added index", "session_end_time", each try
if 
#"Added index"{[Index]}[session_user_email] = 
#"Added index"{[Index] + 1}[session_user_email] then
#"Added index"{[Index] + 1}[session_start_time]
else
null
otherwise
null),

 How can I make this more efficient?

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

@Anonymous 

end_time = (tbl as table) as table =>
    Table.FromColumns(
        Table.ToColumns(tbl) & {List.RemoveFirstN(tbl[session_start_time], 1) & {null}},
        Table.ColumnNames(tbl) & {"session_end_time"}
    ),
g = Table.Group(#"Renamed columns", "session_user_email", {"upd", (x) => end_time(Table.Sort(x, "session_start_time"))}),
expand = Table.ExpandTableColumn(g, "upd", {"session_start_time", "session_end_time"})

View solution in original post

3 REPLIES 3
AlienSx
Super User
Super User

@Anonymous 

end_time = (tbl as table) as table =>
    Table.FromColumns(
        Table.ToColumns(tbl) & {List.RemoveFirstN(tbl[session_start_time], 1) & {null}},
        Table.ColumnNames(tbl) & {"session_end_time"}
    ),
g = Table.Group(#"Renamed columns", "session_user_email", {"upd", (x) => end_time(Table.Sort(x, "session_start_time"))}),
expand = Table.ExpandTableColumn(g, "upd", {"session_start_time", "session_end_time"})
Anonymous
Not applicable

This works very well, thank you!

Syndicate_Admin
Administrator
Administrator

Slap a Table.Buffer around your code.

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 Kudoed Authors