Forum Discussion

Pick's avatar
Pick
Icon for Helper II rankHelper II
6 years ago
Solved

Create new table to show time between orders

Hi,

 

  I have a table with a list of orders with start date and end time.  I need to produce another table to show the time in minutes from the end of the last order to the strat of the next order.  Before and after tables are shown below.  Any help is appreciated.

 

BEFORE

 

SonoOrganizational_UnitOrder_NoProduct_CodeWeekStart_TimeEnd_TimeMinutes
Sono2Hair132458609118496102/01/2020 15:0702/01/2020 19:21254.25
Sono2Hair132458619118496102/01/2020 19:5202/01/2020 23:10198
Sono2Hair1324764567836033103/01/2020 00:0403/01/2020 02:24139.75
Sono2Hair1324764667836033103/01/2020 02:1903/01/2020 05:21

181.75

 

 

AFTER

 

SonoOrganizational_UnitPrevious_Order_NoPreviois_Product_CodeNext_Order_NoNext_Product_CodePrevious_Order_End_TimeNext_Order_Start_TimeMinutes
Sono2Hair13245860911849613245861911849602/01/2020 19:2102/01/2020 19:5221
Sono2Hair132458619118496132476456783603302/01/2020 23:1003/01/2020 00:0454
Sono2Hair1324764567836033132476466783603303/01/2020 02:2403/01/2020 02:19-5
Sono2Hair132476466783603313247849911851603/01/2020 05:2103/01/2020 06:2776

 

 

  Regards

 

   Ian

  • Hi Pick , 

    You could refer to my sample for details.

    Best Regards,
    Zoe Zhi

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • Hi Pick ,

     

    In Power Query, sort [start_time] and [end_time] so that they show correctly in chronological order.

    Add an index column starting from 0, call it [index0] or similar.

    Add index column starting from 1, call it [index1] or similar.

    Merge (join) the table on itself using [index0] as the first join column and [index1] as the second.

    Expand the [time_start] and [time_end] columns and rename them to [prev_time_end] etc.

     

    EDIT:

    Here's the Power Query code to follow the steps I took:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZAxCsMwDEWvUjyHRPq2ZEsn6N4xZOjYpYHef6id0kINziAkHtLjo3UNt/25I0zhen+8auOIJEWpjsZckmmDtQgL8QICXViccofM0bYgaYaEbRqJ+VRsLvhHiM4tDFsZWbMmqaPmEpVi/Grjz0HklDoERzrubc7DvNWs52Y4W4fk8wkufJi3Nw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Sono = _t, Organizational_Unit = _t, Order_No = _t, Product_Code = _t, Week = _t, Start_Time = _t, End_Time = _t, Minutes = _t]),
        chgAllTypes = Table.TransformColumnTypes(Source,{{"Sono", type text}, {"Organizational_Unit", type text}, {"Order_No", type text}, {"Product_Code", type text}, {"Week", type text}, {"Start_Time", type datetime}, {"End_Time", type datetime}, {"Minutes", type number}}),
        sortDateTimes = Table.Sort(chgAllTypes,{{"Start_Time", Order.Ascending}, {"End_Time", Order.Ascending}}),
        addIndex0 = Table.AddIndexColumn(sortDateTimes, "Index0", 0, 1),
        addIndex1 = Table.AddIndexColumn(addIndex0, "Index1", 1, 1),
        mergeOnMyself = Table.NestedJoin(addIndex1, {"Index0"}, addIndex1, {"Index1"}, "Added Index1", JoinKind.LeftOuter),
        expandPrevTimes = Table.ExpandTableColumn(mergeOnMyself, "Added Index1", {"Start_Time", "End_Time"}, {"Prev_Start_Time", "Prev_End_Time"})
    in
        expandPrevTimes

     

    In Power Query, go to New Source > Blank Query, then in Advanced Editor paste my code over the default code.

    I've only expanded the DateTime columns in my example, the principle is the same for any other columns you want to show the previous value of.

     

    Pete

  • dax's avatar
    dax
    Icon for Community Support rankCommunity Support

    Hi Pick , 

    You could refer to my sample for details.

    Best Regards,
    Zoe Zhi

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.