[Power Query] matching two tables with not quite same dates

ursua

New Member
Joined
Jan 9, 2020
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have two sheets named sheet1 and sheet2 with same columns: Identificator (which could be duplicated many times), date column (which is always the same), and date column2 (which i=could be the same or could be different for every row). What I need to do is to add a column in sheet2 which will display "1" when in sheet1 there is a row with the same identificator and date column2 that is not quite same, it could be f.e. 5 minutes later or earlier, and 0 if there is no matching row for identificator+date column2+-5 mins. it would not be a problem if the date column2 would be the same in both tables.
How to achieve that using Power Query?
 

Attachments

  • sheet1.png
    sheet1.png
    14.3 KB · Views: 5
  • sheet2.png
    sheet2.png
    17.2 KB · Views: 5

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
So if i understand you correctly you want this?:

A match from "Identificator" + "Date column 2" (+-5 min) from both tables and build a 3 (result) like this (without the colors)?:

Sheet 1Sheet 2Result
IdentificatorDate columnDate column 2IdentificatorDate columnDate column 2IdentificatorDate columnDate column 2Result
aaa28/11/2023 11:0028/11/2023 11:50aaa28/11/2023 11:0028/11/2023 11:49aaa28/11/2023 11:0028/11/2023 11:501
aab29/11/2023 13:4030/11/2023 13:46aaa28/11/2023 11:0028/11/2023 13:00aab29/11/2023 13:4030/11/2023 13:461
aab29/11/2023 13:4001/12/2023 13:46aab29/11/2023 13:4030/11/2023 13:45aab29/11/2023 13:4001/12/2023 13:461
aac13/10/2023 14:3514/10/2023 03:03aab29/11/2023 13:4001/12/2023 13:45aac13/10/2023 14:3514/10/2023 03:031
aad23/11/2023 05:0023/11/2023 17:49aab29/11/2023 13:4003/12/2023 11:09aad23/11/2023 05:0023/11/2023 17:490
aac13/10/2023 14:3514/10/2023 15:34aac13/10/2023 14:3514/10/2023 03:03aac13/10/2023 14:3514/10/2023 15:340
aaf30/11/2023 12:4408/12/2023 15:09aaf30/11/2023 12:4409/12/2023 03:05aaf30/11/2023 12:4408/12/2023 15:090
aac13/10/2023 14:3516/10/2023 12:30
aad23/11/2023 05:0024/11/2023 14:50
 
Upvote 0
If you have the 2 tables named Sheet1 and Sheet2 like so:

Tables.png


Table2.png



Then you create a third ("Combinar1") with this code:
Power Query:
let
    Origen = Table.NestedJoin(Sheet1, {"Identificator"}, Sheet2, {"Identificator"}, "Sheet2", JoinKind.LeftOuter),
    #"Se expandió Sheet2" = Table.ExpandTableColumn(Origen, "Sheet2", {"Date column 2"}, {"Sheet2.Date column 2"}),
    #"Tipo cambiado" = Table.TransformColumnTypes(#"Se expandió Sheet2",{{"Date column 2", type number}, {"Sheet2.Date column 2", type number}}),
    #"Personalizada agregada" = Table.AddColumn(#"Tipo cambiado", "TimeDif", each Number.Abs([Date column 2]-[Sheet2.Date column 2])),
    #"Tipo cambiado1" = Table.TransformColumnTypes(#"Personalizada agregada",{{"TimeDif", type duration}}),
    #"Filas filtradas" = Table.SelectRows(#"Tipo cambiado1", each [TimeDif] <= #duration(0, 0, 5, 0)),
    #"Duplicados quitados" = Table.Distinct(#"Filas filtradas", {"Identificator", "Date column", "Date column 2", "TimeDif"}),
    #"Personalizada agregada1" = Table.AddColumn(#"Duplicados quitados", "Result", each 1),
    #"Tipo cambiado2" = Table.TransformColumnTypes(#"Personalizada agregada1",{{"Date column 2", type datetime}})
in
    #"Tipo cambiado2"

And then a fourth ("Combinar2"):

Power Query:
let
    Origen = Table.NestedJoin(Sheet1, {"Identificator", "Date column", "Date column 2"}, Combinar1, {"Identificator", "Date column", "Date column 2"}, "Combinar1", JoinKind.LeftOuter),
    #"Se expandió Combinar1" = Table.ExpandTableColumn(Origen, "Combinar1", {"Result"}, {"Combinar1.Result"}),
    #"Valor reemplazado" = Table.ReplaceValue(#"Se expandió Combinar1",null,0,Replacer.ReplaceValue,{"Combinar1.Result"})
in
    #"Valor reemplazado"

And you will get this result:

IdentificatorDate columnDate column 2Combinar1.Result
aaa28/11/2023 11:0028/11/2023 11:501
aab29/11/2023 13:4030/11/2023 13:461
aab29/11/2023 13:4001/12/2023 13:461
aac13/10/2023 14:3514/10/2023 3:031
aad23/11/2023 5:0023/11/2023 17:490
aac13/10/2023 14:3514/10/2023 15:340
aaf30/11/2023 12:4408/12/2023 15:090



Is that what you need?
 
Upvote 0
Solution
That was the solution i've been looking for. Thank You very much!
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,093
Latest member
ripvw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top