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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Calculate time difference to nearest date by ID

 

hey peeps, 

 

How do I calculate the time duration by the same ID over difference dates? 

 

Transaction DateDate & TimeIDIn 1 / Out 0
9/5/20189/5/2018 5:18:20 PM229851
6/2/20176/2/2017 12:00:00 PM15721
7/2/20177/2/2017 4:00:00 PM15720
8/2/20178/2/2017 10:00:00 AM15721
11/2/201711/2/2017 12:00:00 PM15720

 

Expected Output as below

 

Example

1572 clocks in on 6/2 12pm, and clocks out on 7/2 4pm, = 24 + 4 = 28 hours 

1572 clocks in on 8/2 10am, and clocks out on 11/2 12pm, = 24 + 24 +24 + 4 = 78 hours

Total for 1572 = 28 + 78 = 106 hours

 

22985 clocks in once on 9/5, but there's no nearest clock out, hence = 0. 

 

FINIn CountOut CountTotal Duration
15722228 + 74 = 102 hours
22985100 cause no nearest out
1 ACCEPTED SOLUTION
v-yulgu-msft
Employee
Employee

Hi @Anonymous,

 

First new a calculated column:

Duration =
DATEDIFF (
    IF (
        Test2[In 1 / Out 0] = 0,
        CALCULATE (
            MAX ( Test2[Date & Time] ),
            FILTER (
                ALLEXCEPT ( Test2, Test2[ID] ),
                Test2[Date & Time] < EARLIER ( Test2[Date & Time] )
                    && Test2[In 1 / Out 0] = 1
            )
        )
    ),
    Test2[Date & Time],
    HOUR
)

1.PNG

 

Then, create below measures:

In Count = CALCULATE(COUNT(Test2[In 1 / Out 0]),FILTER(Test2,Test2[In 1 / Out 0]=1))+0

Out Count = CALCULATE(COUNT(Test2[In 1 / Out 0]),FILTER(Test2,Test2[In 1 / Out 0]=0))+0

Total Duration = SUM(Test2[Duration])+0

Use a Table visual o display data.

2.PNG

 

Best 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.

View solution in original post

2 REPLIES 2
v-yulgu-msft
Employee
Employee

Hi @Anonymous,

 

First new a calculated column:

Duration =
DATEDIFF (
    IF (
        Test2[In 1 / Out 0] = 0,
        CALCULATE (
            MAX ( Test2[Date & Time] ),
            FILTER (
                ALLEXCEPT ( Test2, Test2[ID] ),
                Test2[Date & Time] < EARLIER ( Test2[Date & Time] )
                    && Test2[In 1 / Out 0] = 1
            )
        )
    ),
    Test2[Date & Time],
    HOUR
)

1.PNG

 

Then, create below measures:

In Count = CALCULATE(COUNT(Test2[In 1 / Out 0]),FILTER(Test2,Test2[In 1 / Out 0]=1))+0

Out Count = CALCULATE(COUNT(Test2[In 1 / Out 0]),FILTER(Test2,Test2[In 1 / Out 0]=0))+0

Total Duration = SUM(Test2[Duration])+0

Use a Table visual o display data.

2.PNG

 

Best 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.
Anonymous
Not applicable

Brilliant!!!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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