Forum Discussion

masterl1983's avatar
masterl1983
Frequent Visitor
7 years ago

Identify rows belonging together based on field value changes

Hi,

 

Maybe the subject isn't that good but it's hard to describe my problem in on sentence.

I'm analyzing multiple IoT Devices which do send every few hours their current geo location.

The travelled path of those devices is (more or less) always a circle. So you can say that they start at 12 o'clock -> travel to 6 o'clock -> travel back to 12 o'clock.

 

I have a table which looks like this one. A new Trip starts when CurrentLocation changes from Location_A to something else.

A Trip ends when currentLocation changes back to LocationA.

 

I addition I need to get the the time (hours) which indicates how long a Device is within the current location.

 

So my initial table looks like

 

DateTime Device ID CurrentLocation Distinct Trip No. Duration in Location

 01.01.2017 06:00ID0001Location_XYZ  
 01.01.2017 08:00ID0001Location_A  
 02.01.2017 10:00ID0001Location_A  
 03.01.2017 12:00ID0001Location_C  
 04.01.2017 12:00ID0001Location_D  
 05.01.2017 10:00ID0001Location_XYZ  
 06.01.2017 08:00ID0001Location_A  
 06.01.2017 10:00ID0001Location_A  
 07.01.2017 12:00ID0001Location_B  
 01.01.2017 08:00ID0002 Location_A  
 01.01.2017 10:00ID0002 Location_XYZ  
 ...........  
08.01.2017ID0002Location_A  

 

and I need to get something like this

 

 

DateTime Device ID CurrentLocation Distinct Trip No. Duration in Location

 01.01.2017 06:00ID0001Location_XYZ  
 01.01.2017 08:00ID0001Location_A  52 hours
 02.01.2017 10:00ID0001Location_A

 

 52 hours
 03.01.2017 12:00ID0001Location_CTrip 1 24 hours
 04.01.2017 12:00ID0001Location_DTrip 1 22 hours
 05.01.2017 10:00ID0001Location_XYZTrip 1 22 hours
 06.01.2017 08:00ID0001Location_A  28 hours
 06.01.2017 10:00ID0001Location_A  28 hours
 07.01.2017 12:00ID0001Location_B Trip 2 
 01.01.2017 08:00ID0002 Location_A  2 hours
 01.01.2017 10:00ID0002 Location_XYZ Trip 3 
 ...........  
08.01.2017ID0002Location_A  

 

Don't know if this is possible...looking forward to your answers.

 

best regards

5 Replies

  • Zubair_Muhammad's avatar
    Zubair_Muhammad
    Community Champion

    masterl1983

     

    I hope there is a better way.

    But this works works with your sample data

     

    Duration =
    VAR myID = [ Device ID]
    VAR myDT = [DateTime]
    VAR mylocation = [ CurrentLocation]
    VAR mytime =
        CALCULATE (
            MAX ( Table1[DateTime] ),
            FILTER (
                Table1,
                [ Device ID] = myID
                    && [DateTime] < myDT
                    && [ CurrentLocation] <> mylocation
            )
        )
    VAR firstsameinstance =
        COUNTROWS (
            FILTER (
                Table1,
                [ Device ID] = myID
                    && [DateTime] > mytime
                    && [DateTime] < myDT
            )
        )
    VAR nextTime =
        CALCULATE (
            MIN ( Table1[DateTime] ),
            FILTER (
                Table1,
                [ Device ID] = myID
                    && [DateTime] > myDT
                    && [ CurrentLocation] <> mylocation
            )
        )
    VAR firstsameinstancetime =
        MINX (
            TOPN (
                firstsameinstance,
                FILTER ( Table1, [ Device ID] = myID && [DateTime] < myDT ),
                [DateTime], DESC
            ),
            [DateTime]
        )
    VAR my_time =
        IF ( firstsameinstance > 0, firstsameinstancetime, [DateTime] )
    VAR firstlocation =
        MINX (
            TOPN ( 1, FILTER ( Table1, [ Device ID] = myID ), [DateTime], ASC ),
            [ CurrentLocation]
        )
    VAR firsttime =
        MINX (
            TOPN ( 1, FILTER ( Table1, [ Device ID] = myID ), [DateTime], ASC ),
            [DateTime]
        )
    RETURN
        IF (
            firstlocation <> "Location_A"
                && [DateTime] = firsttime,
            BLANK (),
            DATEDIFF ( my_time, nextTime, HOUR )
        )