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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Substract from Different Rows on a Table

Hi Team. Is there a way to reproduce this in Powerbi.?

powerbi2.JPG

This is the table I have to, in theory, I would like to subtract the time or duration between 1:11 AM and 1:04AM the same way I did in my excel file. Any idea on how to achieve this?

powerbi.JPG

6 REPLIES 6
Anonymous
Not applicable

@v-alq-msft  Can you do the same but with the timestamp in a different order and different dates? I see that it works in yours but mine has different dates and times and does not show the same results.powerbi.JPG

Hi, @Anonymous 

 

I modified data to reproduce your scenario.

Table:

a1.png

 

You may create an index column in Power Query and then modify the measure or column as below.

Calculated column:

Result Column = 
var tab = 
ADDCOLUMNS(
    ALL('Table'),
    "Start",[Status Start Date]+[Start Time],
    "End",[Status End Date]+[End Time]
)
var totalseconds =  
DATEDIFF(
    MAXX(
        FILTER(
            tab,
            [Index]=EARLIER('Table'[Index])-1
        ),
        [End]
    ),[Status Start Date]+[Start Time],SECOND
)
var m = 
COALESCE(INT(DIVIDE(totalseconds,60)),0)
var s = COALESCE(MOD(totalseconds,60),0)
return
m&":"&s

 

Measure:

Result Measure = 
var tab = 
ADDCOLUMNS(
    ALL('Table'),
    "Start",[Status Start Date]+[Start Time],
    "End",[Status End Date]+[End Time]
)
var totalseconds =  
DATEDIFF(
    MAXX(
        FILTER(
            tab,
            [Index]=MAX('Table'[Index])-1
        ),
        [End]
    ),MAX([Status Start Date])+MAX([Start Time]),SECOND
)
var m = 
COALESCE(INT(DIVIDE(totalseconds,60)),0)
var s = COALESCE(MOD(totalseconds,60),0)
return
m&":"&s

 

Best Regards

Allan

 

If this post helps,then consider Accepting it as the solution to help other members find it faster.

Anonymous
Not applicable

First, thank you so much for the hand. Im still getting the following miscalculation. I think you are close but not quite getting the correct calculation or formatting. Your table is still organized even though it shows a gap between 5:17 and 6:31 pm. Can you try with the table having unsorted date times with 2 different users?

carlosdajer_0-1605731494951.png

Again, appreciate the time.

v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

Based on your descripton, I created data to reproduce your sceanrio. The pbix file is attached in the end.

Table:

a1.png

 

You may create an index column in Power Query and then create a measure or a column as below.

Measure:

Result Measure = 
var totalseconds =  
DATEDIFF(
    CALCULATE(
        MAX('Table'[Status End Datetime]),
        FILTER(
            ALL('Table'),
            [Index]=MAX('Table'[Index])-1
        )
    ),MAX('Table'[Status Start Datetime]),SECOND
)
var m = 
COALESCE(INT(DIVIDE(totalseconds,60)),0)
var s = COALESCE(MOD(totalseconds,60),0)
return
m&":"&s

 

Calculated column:

Result Column = 
var totalseconds =  
DATEDIFF(
    CALCULATE(
        MAX('Table'[Status End Datetime]),
        FILTER(
            ALL('Table'),
            [Index]=EARLIER('Table'[Index])-1
        )
    ),[Status Start Datetime],SECOND
)
var m = 
COALESCE(INT(DIVIDE(totalseconds,60)),0)
var s = COALESCE(MOD(totalseconds,60),0)
return
m&":"&s

 

Result:

a2.png

 

Best Regards

Allan

 

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

Anonymous
Not applicable

@Fowmy the Var only captures the Measures, is there a way to use the columns?

Fowmy
Super User
Super User

@Anonymous 

 

You need to first add an index column in Power Query then add the following DAX column to the table:

Time Diff = 

var __currtime = Table2[Start Time]
var __previoustime  =  MAXX( FILTER( Table2 , Table2[Index] =  EARLIER(Table2[Index]) - 1), Table2[End Time]) 
var _Diff = DATEDIFF(+__previoustime , __currtime, SECOND )
return
_Diff
    

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

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.