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
qwertzuiop
Advocate III
Advocate III

Calculate time interval to next message

Hello dear PowerBI-Community

 

I hope you guys could help me with the following problem.

In the example (see table) there are several transports.

Each transport has an unique ID.

 

A transport can create 1 to n messages.

Actually my need is to measure the time interval to the next message on a transport.

For this reason i would like to create a calculated table in Power BI (see table - red column "timeGapToNextMessageOnTransportID")

 

transportIDmessageCreatedtimeGapToNextMessageOnTransportID
109.12.2019 10:157.25 h
109.12.2019 17:30x
210.12.2019 11:45x
312.12.2019 13:0013.0 h
313.12.2019 02:002.5 h
313.12.2019 04:300.5 h
313.12.2019 05:00x

 

I you have any questions, please ask.

I would be very thankful if one of you guys could help me.

 

 

Have a great time!

Cheers.

qwertzuiop

1 ACCEPTED SOLUTION

Hi @qwertzuiop ,

 

Measure = 
VAR temp_table =
    FILTER (
        SUMMARIZE (
            ALL ( 'Table'[transportID]; 'Table'[messageCreated] );
            'Table'[messageCreated];
            'Table'[transportID]
        );
        'Table'[messageCreated] > MAX ( 'Table'[messageCreated] )
            && 'Table'[transportID] = SELECTEDVALUE ( 'Table'[transportID] )
    )
VAR Current_Date =
    SELECTEDVALUE ( 'Table'[messageCreated] )
VAR Time_difference =
    DATEDIFF ( Current_Date; MINX( temp_table; 'Table'[messageCreated] ); MINUTE ) / 60 + 0
RETURN
    Time_difference

Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

7 REPLIES 7
MFelix
Super User
Super User

Hi @qwertzuiop ,

 

Create the following measure:

 

Measure =
VAR temp_table =
    FILTER (
        SUMMARIZE (
            ALL ( 'Table'[transportID]; 'Table'[messageCreated] );
            'Table'[messageCreated];
            'Table'[transportID]
        );
        'Table'[messageCreated] > MAX ( 'Table'[messageCreated] )
            && 'Table'[transportID] = SELECTEDVALUE ( 'Table'[transportID] )
    )
VAR Current_Date =
    SELECTEDVALUE ( 'Table'[messageCreated] )
VAR Time_difference =
    DATEDIFF ( Current_Date; MAXX ( temp_table; 'Table'[messageCreated] ); MINUTE ) / 60 + 0
RETURN
    IF ( Time_difference = 0; "-"; FORMAT ( Time_difference; ""##0.0# h" h" ) )

 

I'm assuming you don't want to have this calculated on total time level.

 

check PBIX file attach.


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hello @MFelix 

Thank you so much for you support and your well structured text and attached file.

It makes it very easy to follow your thoughts.

 

Nevertheless I have some questions:

  • How is your measure linked to the calculated column named "timeGapToNextMessageOnTransportID"?
    Because, if I focus on this column there is no stored formula (see figure 1)
  • If I try to create directly a calulated column with your formula (see figure 2) all I get is the value "-" for every line - why is this?
  • Is it possible to display the output as a decimal number instead of a string - so also without the letter "h" - excuse me

timegap.PNG

figure 1

 

figure 2

 

Thank you so much for your help.

Your contribution is very valuable.

 

Cheers!

qwertzuiop

 

I don't know why but figure 2 was missing.

sorry

 

calculatedColumn.PNG

figrue 2

Excuse me

Now I got it.

The calculated table is the one out of my example.

But then why is this error?

 

error.PNG

Hi @qwertzuiop ,

 

My bad when I was testing out the information placed a MAXX when should be a MINX it's giving the difference to 5AM and not 2AM redo the measure to:

Measure = 
VAR temp_table =
    FILTER (
        SUMMARIZE (
            ALL ( 'Table'[transportID]; 'Table'[messageCreated] );
            'Table'[messageCreated];
            'Table'[transportID]
        );
        'Table'[messageCreated] > MAX ( 'Table'[messageCreated] )
            && 'Table'[transportID] = SELECTEDVALUE ( 'Table'[transportID] )
    )
VAR Current_Date =
    SELECTEDVALUE ( 'Table'[messageCreated] )
VAR Time_difference =
    DATEDIFF ( Current_Date; MINX( temp_table; 'Table'[messageCreated] ); MINUTE ) / 60 + 0
RETURN
    IF ( Time_difference = 0; "-"; FORMAT ( Time_difference; "##0.0# h" ) )

 

Corrected file attach.

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hello @MFelix 

 

Thank you so much for your quick answer and your great support.

The error is fixed.

 

But may I ask you some more things:

  • How can I turn the output into a decimal number instead of a string like "7.25 h" -> What I would like is 7,25 - excuse my change
  • If I try this formula with a measure in my masterdocument with over 30'000 diffrent TransportID's with over 50'000 created messages it is still loading and nothing happens...(see figure). Is there a way to create a calulated column with this information instead of a measure - I think with this way I could solve the problem.

loading.PNGThank you so much.

You're a great member of the Community 🙂

 

Cheers!

qwertzuiop

Hi @qwertzuiop ,

 

Measure = 
VAR temp_table =
    FILTER (
        SUMMARIZE (
            ALL ( 'Table'[transportID]; 'Table'[messageCreated] );
            'Table'[messageCreated];
            'Table'[transportID]
        );
        'Table'[messageCreated] > MAX ( 'Table'[messageCreated] )
            && 'Table'[transportID] = SELECTEDVALUE ( 'Table'[transportID] )
    )
VAR Current_Date =
    SELECTEDVALUE ( 'Table'[messageCreated] )
VAR Time_difference =
    DATEDIFF ( Current_Date; MINX( temp_table; 'Table'[messageCreated] ); MINUTE ) / 60 + 0
RETURN
    Time_difference

Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



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.