Forum Discussion

MRM_CCM's avatar
MRM_CCM
Regular Visitor
1 year ago
Solved

Recreating excel operations

I'm not sure how to recreate a set of steps I achieved in excel with Power Query. The data is a record of time punches. However they are all  just recorded as in in one column and out in another. For...
  • SundarRaj's avatar
    SundarRaj
    1 year ago

    Hi MRM_CCM , is this what you are looking for? I'll just attach the images of the output and code the used. Thanks!

    Here's the code:
    let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Lastname", type text}, {"Firstname", type text}, {"InPunchDate", type datetime}, {"InPunchTime", type time}, {"OutPunchDate", type datetime}, {"OutPunchTime", type time}, {"EarnHours", type number}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Lastname", "Firstname", "InPunchDate", "OutPunchDate"}, {{"All", each _},{"Sum", each List.Sum(_[EarnHours])}})[[All],[Sum]],
    Try1 = Table.AddColumn(#"Grouped Rows","RowCount", each Table.RowCount(_[All]) - 1),
    Try2 = Table.AddColumn(Try1,"List", each {1.._[RowCount]}),
    Try3 = Table.TransformColumns(Try2,{"List", each List.Transform(_, each 0)}),
    #"Expanded List" = Table.ExpandListColumn(Try3, "List"),
    SumNos = List.RemoveNulls(List.Combine(List.Zip({#"Expanded List"[Sum],#"Expanded List"[List]}))),
    #"Expanded All" = Table.ExpandTableColumn(#"Expanded List"[[All]], "All", {"Lastname", "Firstname", "InPunchDate", "InPunchTime", "OutPunchDate", "OutPunchTime", "EarnHours"}, {"Lastname", "Firstname", "InPunchDate", "InPunchTime", "OutPunchDate", "OutPunchTime", "EarnHours"}),
    FinTable = Table.TransformColumns(Table.AddIndexColumn(#"Expanded All","H3",0,1), {"H3", each SumNos{_}})
    in
    FinTable