Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Combining Two Tables with Date & Time

Hey there,   I'm new to powerBI. For my Masterthesis i need to create a PowerBI report. Therefore i import two different tables. First Table is manuel created: Has Date & Time of an action  Sec...
  • ronrsnfld's avatar
    ronrsnfld
    4 years ago

    I suggest (starting with coilNumber/OrderNumber/Date & Time

    • add a custom column that has a List of each Time at one minute interval during your time frame (1 hour before to 1 hour after)
    • Do an innerJoin between the manual and the machine generated table

    M Code

     

     

    let
    
    //Read in the two tables and set the data types
        Source = Excel.CurrentWorkbook(){[Name="machineTable"]}[Content],
        machineTable = Table.TransformColumnTypes(Source,{{"Date & Time", type datetime}, {"Swapped", type text}, {"Material", type text}},"en-DE"),
    
        Source2 = Excel.CurrentWorkbook(){[Name="manualTable"]}[Content],
        manualTable = Table.TransformColumnTypes(Source2,
            List.Zip({Table.ColumnNames(Source2),{Int64.Type,Int64.Type,DateTime.Type}}),"en-DE"),
    
    //add a custom column with a list of the hour before to hour after at one minute intervals
    // (if times are recorded to the second, could round the times first to the nearest minute,
    //  or make the list at one second intervals)
        #"Added Custom" = Table.AddColumn(manualTable, "machineTime", 
            each List.DateTimes([#"Date & Time"]-#duration(0,1,0,0),
            120,
            #duration(0,0,1,0))),
    
    //expand the list of minutes to rows
        #"Expanded allMinutes" = Table.ExpandListColumn(#"Added Custom", "machineTime"),
    
    //Do an inner join with the machine generated table
        #"Join with Machine Table" = Table.NestedJoin(
            #"Expanded allMinutes","machineTime",machineTable,"Date & Time","joined",JoinKind.Inner),
    
    //expand the Swapped and Material Rows from the joined table
    //  and delete the machineTime Column
        #"Expanded joined" = Table.ExpandTableColumn(#"Join with Machine Table", "joined", {"Swapped", "Material"}, {"Swapped", "Material"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded joined",{"machineTime"})
    in
        #"Removed Columns"

     

     

     

     

     

     

     

    Instead of a Join, you could instead add a filtered table in a custom column, where the filter is set to return anything from the machine table that is in the range of +/- one hour, but I suspect the join method will be faster.  If it is slow, then we can try the filter method

     

    Filter Method Code

     

     

    let
    
    //Read in the two tables and set the data types
        Source = Excel.CurrentWorkbook(){[Name="machineTable"]}[Content],
        machineTable = Table.TransformColumnTypes(Source,{{"Date & Time", type datetime}, {"Swapped", type text}, {"Material", type text}},"en-DE"),
    
        Source2 = Excel.CurrentWorkbook(){[Name="manualTable"]}[Content],
        manualTable = Table.TransformColumnTypes(Source2,
            List.Zip({Table.ColumnNames(Source2),{Int64.Type,Int64.Type,DateTime.Type}}),"en-DE"),
        
        #"Added Custom" = Table.AddColumn(manualTable, "Custom", each 
            let 
                tStart = [#"Date & Time"]-#duration(0,1,0,0),
                tEnd = [#"Date & Time"] + #duration(0,1,0,0),
                mt = Table.SelectRows(machineTable, each 
                            ([#"Date & Time"]>=tStart) and ([#"Date & Time"]<=tEnd))
            in 
                mt),
        
        #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", 
            {"Swapped", "Material"}, {"Custom.Swapped", "Custom.Material"})
    in
        #"Expanded Custom"