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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Anonymous
Not applicable

Subtract different columns from previous row

I am new to Power BI and know very little M code, any and all help would be greatly appreciated.MDickey_0-1643836462110.png

I am trying to get the "Gap" time from when an assignment ended and the next assignment started.

So Logically I would take DT_TM_START (tran_num 54) - DT_TM_End (tran_num 53) or said another way 10:34:11-10:27:38

 

Thanks for all the help in advance

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @Anonymous ,

You can also achieve it by an easier DAX code, here's my solution.

1.Create a measure.

 

Time Gap =
VAR _NowStart =
    MAX ( 'Table'[DT_TM_START] )
VAR _LastEnd =
    MAXX (
        FILTER ( ALL ( 'Table' ), 'Table'[TRAN_NUM] = MAX ( 'Table'[TRAN_NUM] ) - 1 ),
        'Table'[DT_TM_END]
    )
RETURN
    CONVERT ( IF ( _LastEnd = BLANK (), 0, _NowStart - _LastEnd ), DATETIME )

 

 2.Change the format of the measure to hh:nn:ss

vkalyjmsft_0-1644459358370.png

Get the expected result.

vkalyjmsft_1-1644459424070.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

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

2 REPLIES 2
v-yanjiang-msft
Community Support
Community Support

Hi @Anonymous ,

You can also achieve it by an easier DAX code, here's my solution.

1.Create a measure.

 

Time Gap =
VAR _NowStart =
    MAX ( 'Table'[DT_TM_START] )
VAR _LastEnd =
    MAXX (
        FILTER ( ALL ( 'Table' ), 'Table'[TRAN_NUM] = MAX ( 'Table'[TRAN_NUM] ) - 1 ),
        'Table'[DT_TM_END]
    )
RETURN
    CONVERT ( IF ( _LastEnd = BLANK (), 0, _NowStart - _LastEnd ), DATETIME )

 

 2.Change the format of the measure to hh:nn:ss

vkalyjmsft_0-1644459358370.png

Get the expected result.

vkalyjmsft_1-1644459424070.png

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

SteveHailey
Solution Specialist
Solution Specialist

Hi @Anonymous. You can do this without needing to know any M code. It can be done through the GUI. I created a .pbix file linked here that you can download if it's helpful. Let me know if you get hung up on any of these steps and I'll be happy to walk you through it in more detail.

 

Here are the steps:

1. Add an Index column from 0  (Add column > Index column > From 0)

2. Add an Index column from 1 (Add column > Index column > From 1)

 

SteveHailey_0-1643847538500.png

3. Do a Merge Queries, joining the table with itself, from the Index 0 column to the Index 1 column. This will allow you to access the previous row. (Home > Merge Queries)

SteveHailey_1-1643847667822.png

 

4. Expand the column to bring in the "DT_TM_END" column from the prior row.

 

SteveHailey_2-1643847730535.png

5. Sort the TRAN_NUM column ascending. The sort will get messed up during the merge so this just puts it back in the original order.

6. Add a custom column to do the subtraction.

SteveHailey_3-1643847827130.png

7. Remove unneeded columns and rename them as you see fit.

 

SteveHailey_4-1643847924107.png

 

 

 

 

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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