let
Source = Excel.CurrentWorkbook(){[Name="data"]}[Content],
Add_Index = Table.AddIndexColumn(Source, "StartPos", 1, 1, Int64.Type),
Group_all_inv_vendor = Table.Group(Add_Index, {"Inv No", "Vendor Code"}, {{"All", each _, type table [Inv No=number, #"Net Weight."=number, In Date=datetime, In Time=number, Out Date=datetime, Out Time=number, Mode=text, Vendor Code=number, #"Time_Tare:Gross"=number]}}),
Add_index_all = Table.AddColumn(Group_all_inv_vendor, "Add Count vendor code", each Table.AddIndexColumn([All], "Count vendor code",1,1, Int64.Type)),
Group_on_invoice = Table.Group(Add_index_all, {"Inv No"}, {{"Total Invoice No as per vendor", each Table.RowCount(_), Int64.Type}, {"All", each _, type table [Inv No=number, Vendor Code=number, All=table, Add Count vendor code=table]}}),
Expand_all_indexed = Table.ExpandTableColumn(Group_on_invoice, "All", {"Add Count vendor code"}, {"Add Count vendor code"}),
Keep_indexed_table = Table.SelectColumns(Expand_all_indexed,{"Total Invoice No as per vendor", "Add Count vendor code"}),
Move_all_to_first_col = Table.ReorderColumns(Keep_indexed_table,List.Reverse(Table.ColumnNames((Keep_indexed_table)))),
Expand_indexed_table = Table.ExpandTableColumn(Move_all_to_first_col, "Add Count vendor code", {"Inv No", "Net Weight.", "In Date", "In Time", "Out Date", "Out Time", "Mode", "Vendor Code", "Time_Tare:Gross", "StartPos", "Count vendor code"}, {"Inv No", "Net Weight.", "In Date", "In Time", "Out Date", "Out Time", "Mode", "Vendor Code", "Time_Tare:Gross", "StartPos", "Count vendor code"}),
Resort_original = Table.Sort(Expand_indexed_table,{{"StartPos", Order.Ascending}}),
Add_actual_time = Table.AddColumn(Resort_original, "Actual time", each if [Count vendor code] = 1 then [#"Time_Tare:Gross"] else null, type duration),
Remove_StartPos_col = Table.RemoveColumns(Add_actual_time,{"StartPos"}),
Set_types = Table.TransformColumnTypes(Remove_StartPos_col,{{"Inv No", Int64.Type}, {"Net Weight.", type number}, {"In Date", type datetime}, {"In Time", type time}, {"Out Date", type datetime}, {"Out Time", type time}, {"Mode", type text}, {"Vendor Code", Int64.Type}, {"Time_Tare:Gross", type duration}, {"Count vendor code", Int64.Type}, {"Actual time", type duration}})
in
Set_types