Forum Discussion

sniakarou's avatar
sniakarou
Frequent Visitor
4 years ago

Subtract same column (time data) based on 3 criteria

Hi everyone! 

 

I must say that the community of power bi has helped me solved a lot of problems I encountered! 

This is a new one that I really need to solve!

 

I have a huge data table with the following details:

 

DateTime of departureStore IDVehicle License Plates
13/1/202209:30352BS 691
13/1/202209:45352S 691

 

I need to subtract column "Time of Departure" if the row fields "Date", "Store ID" and "Vehicle License Plates" are the same and create a new column to calculate the time difference in order to count the visit to the store as 1 in report view if the time difference is less than 30 minutes.

 

Any ideas? 

 

All the help will be appreciated!

 

Thank you so much!!

 

12 Replies

  • sniakarou's avatar
    sniakarou
    Frequent Visitor

    If I can give any more information, please do tell me!

     

    I have included here a table that might help 🙂

     

    Departure DateDeparture TimeVehicle IDKMDriverStore ID
    6/3/217:20BS 6916.08Egerton134
    6/3/217:32BS 6910.06Egerton564
    6/3/217:42BS 6911.33Egerton564
    6/3/218:27BS 69130.72Egerton141
    6/3/219:19BS 6916.85Egerton3
    6/3/219:54BS 6913.85Egerton132
    6/3/2110:27BS 6910.00Egerton4
    6/3/2110:33BS 6914.51Egerton4
    6/3/2110:51BS 6910.03Egerton117
    6/3/2110:58BS 6915.59Egerton117
    6/3/2111:26BS 69133.62Egerton121
    • v-easonf-msft's avatar
      v-easonf-msft
      Icon for Community Support rankCommunity Support

      Hi, sniakarou 

      Is this what you want?

      Result =
      VAR _count =
          CALCULATE (
              COUNTROWS ( Routes ),
              FILTER (
                  Routes,
                  Routes[Departure Date] = EARLIER ( Routes[Departure Date] )
                      && Routes[Vehicle ID] = EARLIER ( Routes[Vehicle ID] )
                      && Routes[Store ID] = EARLIER ( Routes[Store ID] )
                      && Routes[Departure Time]
                          >= EARLIER ( Routes[Departure Time] ) - TIME ( 0, 30, 0 )
                      && Routes[Departure Time] < EARLIER ( Routes[Departure Time] )
              )
          ) + 0
      RETURN
          IF ( _count = 0, "First Route", "Second Route" )

       

      Best Regards,
      Community Support Team _ Eason

      • sniakarou's avatar
        sniakarou
        Frequent Visitor

        Hi @v-easonf-msft ,

         

        Thank you for your help! I tried it it worked actually! The only problem is that in case there are three records with the same vehicle id plates it gets complicated - it shows the 13/1 08:00 as first route, the 13/1 08:30 as second route and the 13/1 09:00 as first route... Any ideas of how can I solve this?

  • Hi sniakarou ,

     

    You want to subtract from what value? in this case the two lines? If you have more than one line how do you handle it?

  • sniakarou's avatar
    sniakarou
    Frequent Visitor

    Hi there, thank you so much for your reply!

     

    Your question is very to the point... the problem is that I have a very big database with the routes of the trucks from the entire year - every truck arrives at X stores every day, but the GPS routing software (most of the times) shows the arrival more than 3 times at the same store with 3 to 20 minutes time difference as the unloading area might not be free - so the truck has to wait and when it shuts off the engine and restarts it the routing software creates a new record. 

     

    The idea is to find the "duplicates" and subtract one value from the same column if it has identical records.

     So basically, if date, store id and vehicle plates have two or more records, ideally to subtract 09:45 from 09:30 and if the difference is up to 30 minutes to count it as 1 record. 

     
    DateTime of departureStore IDVehicle License Plates
    13/1/202209:30352BS 691
    13/1/202209:45352BS 691

     

    I believe this is not feasible but I thought I could ask for help just in case...

     

    The other solution I thought of was to create a calculated column, with this formula, in order to count the first and second occurence of routes and create visuals in report view to filter out errors. 

     

    Occurrence =
    IF(COUNTROWS
    (FILTER(Routes, Routes[Store ID] = Routes[ID] && Routes[Vehicle ID] =Routes[Vehicle ID] &&
    Routes[Date Time] =EARLIER( _Routes[Date Time]))
    ) = 1,
    "First Route","Second Route")

     

    Any help will be greatly appreciated!

     

    Thank you so much!

    • MFelix's avatar
      MFelix
      Icon for Super User rankSuper User

      You want the max and minimum value of dates?

       

  • sniakarou's avatar
    sniakarou
    Frequent Visitor

    That could work too if I can create a calculated column where the last instance of previous route could show as a result based on the filters same store id, date and vehicle id.

     

    I get it is too complicated...

    • MFelix's avatar
      MFelix
      Icon for Super User rankSuper User

      Not complicated just want to understand the data.