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! Request now

Reply
seb_BMW
New Member

compute time difference between two lines in table

I have the following table:

 

Nr      position       time

1            in              12:23:10

2            in              12:25:19

1            out            12:28:59

3            in              13:00:00

2            out            13:02:10

 

and want to compute the time difference between in and out of each number, so that i have in this example:

 

Nr          diff

1            00:05:49

2            00:06:51

3            null     (or another text like "in")

 

how can i do this ? 

thank you very much

 

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

You're looking for the Pivot function.

 

  • Pivot on Position (Value is Time, and be sure not to aggregate)
  • Use M to subtract the resulting in/out column values
  • Convert datatype to the Duration type

Screenshots:

PBIDesktop_2017-07-14_09-33-46.png2017-07-14_09-35-14.pngPBIDesktop_2017-07-14_09-37-26.png

 

The line of M:

#"Added Difference" = Table.AddColumn(#"Pivoted Column", "Diff", each [out] - [in]),

View solution in original post

v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @seb_BMW,

 

Alternatively, you can achieve this in DAX.

 

Create a calculated table based on below formula.

new table =
SUMMARIZE (
    'time difference',
    'time difference'[Nr],
    "time diff", DATEDIFF (
        MIN ( 'time difference'[time] ),
        MAX ( 'time difference'[time] ),
        SECOND
    )
)

Add a calculated column in order to format the time difference.

Column =
IF (
    'new table'[time diff] = 0,
    BLANK (),
    INT ( 'new table'[time diff] / 3600 )
        & ":"
        & INT ( MOD ( 'new table'[time diff], 3600 ) / 60 )
        & ":"
        & MOD ( MOD ( 'new table'[time diff], 3600 )60 )
)
Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @seb_BMW,

 

Have you worked it out? If so, would you please mark the corresponding reply as an answer? If you still have any concern, please feel free to ask.

 

Regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @seb_BMW,

 

Alternatively, you can achieve this in DAX.

 

Create a calculated table based on below formula.

new table =
SUMMARIZE (
    'time difference',
    'time difference'[Nr],
    "time diff", DATEDIFF (
        MIN ( 'time difference'[time] ),
        MAX ( 'time difference'[time] ),
        SECOND
    )
)

Add a calculated column in order to format the time difference.

Column =
IF (
    'new table'[time diff] = 0,
    BLANK (),
    INT ( 'new table'[time diff] / 3600 )
        & ":"
        & INT ( MOD ( 'new table'[time diff], 3600 ) / 60 )
        & ":"
        & MOD ( MOD ( 'new table'[time diff], 3600 )60 )
)
Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

You're looking for the Pivot function.

 

  • Pivot on Position (Value is Time, and be sure not to aggregate)
  • Use M to subtract the resulting in/out column values
  • Convert datatype to the Duration type

Screenshots:

PBIDesktop_2017-07-14_09-33-46.png2017-07-14_09-35-14.pngPBIDesktop_2017-07-14_09-37-26.png

 

The line of M:

#"Added Difference" = Table.AddColumn(#"Pivoted Column", "Diff", each [out] - [in]),

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