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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Qotsa
Helper V
Helper V

Time difference between first and last time

Hi,

 

Can't get what I'm looking for exactly from other posts.

 

Need to calculate the time difference in hours between the 'Time of Last Call' & the 'Time of First Call' for each 'carer_id'

Qotsa_0-1643715325098.png

 

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

Here is a measure expression (not a column) that gives your expected results.

mahoneypat_0-1643720260698.png

TimeDiff =
VAR thisdate =
    MIN ( data[Date] )
VAR thisfirst =
    MIN ( data[Time of First Call] )
VAR prevdate =
    CALCULATE (
        MAX ( data[Date] ),
        ALL ( data ),
        VALUES ( data[carer_id] ),
        data[Date] < thisdate
    )
VAR prevlast =
    CALCULATE (
        MAX ( data[Time of Last Call] ),
        ALL ( data ),
        VALUES ( data[carer_id] ),
        data[Date] = prevdate
    )
RETURN
    IF (
        NOT ( ISBLANK ( prevdate ) ),
        24
            FIXED ( ( thisdate + thisfirst ) - ( prevdate + prevlast )3 )
    )

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

6 REPLIES 6
mahoneypat
Microsoft Employee
Microsoft Employee

Here is a measure expression (not a column) that gives your expected results.

mahoneypat_0-1643720260698.png

TimeDiff =
VAR thisdate =
    MIN ( data[Date] )
VAR thisfirst =
    MIN ( data[Time of First Call] )
VAR prevdate =
    CALCULATE (
        MAX ( data[Date] ),
        ALL ( data ),
        VALUES ( data[carer_id] ),
        data[Date] < thisdate
    )
VAR prevlast =
    CALCULATE (
        MAX ( data[Time of Last Call] ),
        ALL ( data ),
        VALUES ( data[carer_id] ),
        data[Date] = prevdate
    )
RETURN
    IF (
        NOT ( ISBLANK ( prevdate ) ),
        24
            FIXED ( ( thisdate + thisfirst ) - ( prevdate + prevlast )3 )
    )

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat That gives me correct result Pat. TY

Qotsa
Helper V
Helper V

Still not giving desired result.

 

Here is my file: https://www.dropbox.com/s/b0uhe3z8ne4unit/sample%20time%20diff.pbix?dl=0

 

freginier
Super User
Super User

Use datediff function 

Column = DATEDIFF('Table'[First],'Table'[Last],HOUR)

@freginier Thks for reply.

Not getting desired result - 

Qotsa_0-1643717188959.png

 

don't summurize your time difference column

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.