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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
drad2211
Frequent Visitor

dax time difference help

Hi

 

I have a somewhat similar question. I have a table that has the following columns (ID, Time, Action) and I want to add a new column (I believe by making a DAX equation) that can show the time difference (in mins) for specific ID and the 'In' 'Out' Action. I would like the time difference to be shown only on the 'Out' row. The data file is titled 'Data' and the table looks like: 

 

ID                                       Time                             Action

1                            08:00:00 05/05/2015                   In

1                            11:00:00 05/05/2015                  Out

2                            12:00:00 05/05/2015                   In

1                            13:00:00 05/05/2015                   In

1                            15:00:00 05/05/2015                  Out

2                            17:00:00 05/05/2015                  Out

.                                           .                                      .

.                                           .                                      .

 

And I would like the table to look like:

 

ID                                       Time                             Action                     Time Diff

1                            08:00:00 05/05/2015                   In

1                            11:00:00 05/05/2015                  Out                           180

2                            12:00:00 05/05/2015                   In

1                            13:00:00 05/05/2015                   In

1                            15:00:00 05/05/2015                  Out                           120

2                            17:00:00 05/05/2015                  Out                           420

.                                           .                                      .

.                                           .                                      .

 

 

Any advice would be helpful. 

Thank you

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

Hi @drad2211 

 

1. Add the index column in power query

2. Add the measure below:

Measure = var a = CALCULATE(MAX('Table (2)'[Time]),FILTER(ALL('Table (2)'),MAX('Table (2)'[Action])="Out"&&[Index]<MAX('Table (2)'[Index])),VALUES('Table (2)'[ID]))
Return
DATEDIFF(a,MAX('Table (2)'[Time]),MINUTE)

00.PNG 

 

Community Support Team _ Dina Ye
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

5 REPLIES 5
v-diye-msft
Community Support
Community Support

Hi @drad2211 

 

1. Add the index column in power query

2. Add the measure below:

Measure = var a = CALCULATE(MAX('Table (2)'[Time]),FILTER(ALL('Table (2)'),MAX('Table (2)'[Action])="Out"&&[Index]<MAX('Table (2)'[Index])),VALUES('Table (2)'[ID]))
Return
DATEDIFF(a,MAX('Table (2)'[Time]),MINUTE)

00.PNG 

 

Community Support Team _ Dina Ye
If this post helps, then please consider Accept it as the solution to help the other members find it more
quickly.
amitchandak
Super User
Super User

Refer to this example, if can also use lookupvalue

https://community.powerbi.com/t5/Desktop/DAX-LOOKUPVALUE-with-MAX/td-p/696076

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks.

My Recent Blog -
https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...
https://community.powerbi.com/t5/Community-Blog/Power-BI-Working-with-Non-Standard-Time-Periods/ba-p...
https://community.powerbi.com/t5/Community-Blog/Comparing-Data-Across-Date-Ranges/ba-p/823601

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
parry2k
Super User
Super User

@drad2211 how did you get to 420?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

sorry 300

Hi @drad2211 

 

Add a calculated column using the following DAX expression:

Time Diff = 
VAR RowID = Table1[ID]
VAR RowOutDate = Table1[Date]
VAR InDate = 
CALCULATE ( 
    MAX ( Table1[Date] ),
    FILTER ( 
        ALL ( Table1 ),
        Table1[ID] = RowID
            && Table1[Action] = "In"
            && Table1[Date] <= RowOutDate
    )
)
VAR Result =
IF ( 
    Table1[Action] = "Out",
    (RowOutDate - InDate) * 1440
)
RETURN Result

 

You'll need to replace any reference to 'Table1' with your actual table name.

 

Best regards,

Martyn

 

If I answered your question, please help others by accepting it as a solution.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.