Forum Discussion

mrkeastwd's avatar
mrkeastwd
Frequent Visitor
5 years ago
Solved

Add rows based on time between existing rows

The screen captures are from an Excel workbook in order to simplify and anonymize the data.  This is a project I'm working on in Power BI and trying to figure out how to do this with Power Query but ...
  • Anonymous's avatar
    Anonymous
    5 years ago

    OK I get it now.  I would add a new query that selects the rows that mee the criteria, replace values in that table, and then Append the table back to the original table.  First Duplicate your table, then:

     

    Selections = Table.SelectRows(DuplicateTableName, each [UndocTime] >5),

    Undoc = Table.ReplaceValues(Selections, each [WorkType], "UnDoc", Replacer.ReplaceText, {WorkType}),

    Start = Table.AddColumn(Undoc, "Start Time", each [PreviousEndTime]),

    End = Table.AddColumn(Start, "End Time", each [StartTime])

    in End

     

    Now you can append to a new Query:

     

    Combined = Table.Combine(OriginalTable, DuplicateTableName[[User], [WorkType], [Start Time], [End Time]])

    That should work, but I'm not sure how you can sort it correctly...

    --Nate

  • mrkeastwd's avatar
    mrkeastwd
    5 years ago

    Thanks Nate!

     

    I was able to take what you gave me, with a few other manipulations and the end result is exactly what I've been trying to accomplish!

     

     

    I used this Table.Sort(Source,{{"User", Order.Ascending}, {"StartTime", Order.Ascending}}) to sort the data.  Here is the final code for anyone that reads this and needs it...

     

    First Table (WorkLog)...

    let
        Source = Excel.Workbook(File.Contents("C:\MyFiles\SampleData-UndocTime.xlsx"), null, true),
        Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"User", type text}, {"WorkType", type text}, {"StartTime", type datetime}, {"EndTime", type datetime}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index2", 1, 1, Int64.Type),
        #"Reordered Columns" = Table.ReorderColumns(#"Added Index1",{"Index", "Index2", "User", "WorkType", "StartTime", "EndTime"}),
        #"Add PreviousEndTime" = Table.AddColumn(#"Reordered Columns", "PreviousEndTime", each if [Index] = 0 then [StartTime] else #"Reordered Columns"{[Index]-1}[EndTime]),
        #"Add UndocTime" = Table.AddColumn(#"Add PreviousEndTime", "UndocTimeMin", each Duration.Minutes([StartTime]-[PreviousEndTime]))
    in
        #"Add UndocTime"

     

    This is the duplicate table to filter the records for undocumented time entries (WorkLogDupe)

    let
        Source = WorkLog,
        Selections = Table.SelectRows(Source, each [UndocTimeMin] >5),
        Undoc = Table.ReplaceValue(Selections, each [WorkType], "UnDoc", Replacer.ReplaceValue,{"WorkType"}),
        UpdateUndocTimeMin = Table.ReplaceValue(Undoc, each [UndocTimeMin], 0, Replacer.ReplaceValue,{"UndocTimeMin"}),
        Start = Table.AddColumn(UpdateUndocTimeMin, "Start Time", each [PreviousEndTime]),
        End = Table.AddColumn(Start, "End Time", each [StartTime]),
        RemoveOldTimeColumns = Table.RemoveColumns(End,{"StartTime","EndTime"}),
        RenameNewTimeColumns = Table.RenameColumns(RemoveOldTimeColumns,{{"Start Time", "StartTime"},{"End Time", "EndTime"}}),
        #"Reordered Columns" = Table.ReorderColumns(RenameNewTimeColumns,{"Index", "Index2", "User", "WorkType", "StartTime", "EndTime", "PreviousEndTime", "UndocTimeMin"})
    in
        #"Reordered Columns"

     

    And this is the append as new final table (FullWorkLog)...

    let
        Source = Table.Combine({WorkLog, WorkLogDupe}),
        #"Sorted Rows" = Table.Sort(Source,{{"User", Order.Ascending}, {"StartTime", Order.Ascending}})
    in
        #"Sorted Rows"

     

    Nate...Thank you very much for your help to accomplish this!