+ Reply to Thread
Results 1 to 17 of 17

Matching between two sheets and find the closest time match

  1. #1
    Registered User
    Join Date
    03-14-2019
    Location
    Kolkata, India
    MS-Off Ver
    2016
    Posts
    9

    Matching between two sheets and find the closest time match

    HI Guys,

    I have always been looking for some help and have not been able to find any soolution to my problem, hence posting this question. I have two sheets with matching records. There might be more than one match for a record. There is another column in both sheets for date and time, I am looking for match which has the closest time match. Is something like this possible in Index-Match?

    Sheet-1
    Col-1 Col-2
    AAA 20/10/2018
    BBB 21/10/2018
    CCC 21/10/2018

    Sheet-2
    Col-1 Col-2
    AAA 19/10/2018
    AAA 22/10/2018
    AAA 21/10/2018
    BBB 21/10/2018
    CCC 21/10/2018

    O/P
    Col-1 Col-2 Col-3
    AAA 20/10/2018 21/10/2018

    Can somebody help me with this? I have been struggling for quite sometime now!

    Thank You in advance.
    Best,
    Ari

  2. #2
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,548

    Re: Matching between two sheets and find the closest time match

    Hello arindam111 and Welcome to Excel Forum.
    Could you explain why Oct. 21 is a closer match than Oct. 19 as they are both one day different from Oct. 20 It may have to do with the date and time column mentioned, but it is hard to know from information given.
    It might be helpful to upload a sample workbook that demonstrates what you want to accomplish. To upload a sample workbook (not a picture or pasted copy) click on the GO ADVANCED button below the Quick Reply window and then scroll down to Manage Attachments to open the upload window.
    Let us know if you have any questions.
    Consider taking the time to add to the reputation of everybody that has taken the time to respond to your query.

  3. #3
    Registered User
    Join Date
    03-14-2019
    Location
    Kolkata, India
    MS-Off Ver
    2016
    Posts
    9

    Re: Matching between two sheets and find the closest time match

    HI JeteMc,

    Thank You for responding. The reason why October-21 is believed to be a closer match because I am trying to look for anything that comes after that date. To justify, it is like having a connecting flight departing from the airport you land, so anything that left earlier would not be considered. Could I explain my point?

    Best Regards,
    Arindam

  4. #4
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,548

    Re: Matching between two sheets and find the closest time match

    This proposal employs a helper column (Sheet2!C) which may be moved and/or hidden for aesthetic purposes.
    The helper column is populated using: =B2-INDEX(Sheet1!B$2:B$10,MATCH(A2,Sheet1!A$2:A$10,0))
    Columns A:B on the OP sheet are populated using: =IF(Sheet1!A2="","",Sheet1!A2)
    Column C on the OP sheet is populated using:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Let us know if you have any questions.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    03-14-2019
    Location
    Kolkata, India
    MS-Off Ver
    2016
    Posts
    9

    Re: Matching between two sheets and find the closest time match

    Hi JeteMc,

    I cant thank You more! It works perfectly. However, I was wondering if I could map all of the matching data? For example, there are three AAA who want to be mapped and only 1 can board the 21/10/2019 flight and the next one needs to take 22/10/2019 flight. Is it possible?

    Thank You once again!
    Regards,
    Arindam

  6. #6
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,548

    Re: Matching between two sheets and find the closest time match

    It will be easier to help if you will upload a sample that displays the setup. Indicate which cells need to be populated using formula/code. Please manually include the expected data for those cells so that we will know if our formula/code is working correctly. Let us know if you have any questions.

  7. #7
    Registered User
    Join Date
    03-14-2019
    Location
    Kolkata, India
    MS-Off Ver
    2016
    Posts
    9

    Re: Matching between two sheets and find the closest time match

    I understand that the requirements are pretty tricky, hence attaching a sample workbook with the expected output.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    03-14-2019
    Location
    Kolkata, India
    MS-Off Ver
    2016
    Posts
    9

    Re: Matching between two sheets and find the closest time match

    Hi JeteMc,

    I have attached the workbook that I am using. Let me know if there are some issues with the same.

    Thank You once again.
    Regards,
    Ari

  9. #9
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,548

    Re: Matching between two sheets and find the closest time match

    Here is a fairly lengthy proposal for the tricky requirements.
    1. Notice that on the File-2 sheet the range is changed to a table with two extra columns.
    A. The three original columns are custom sorted first by Key and then by Departure.
    B. The Connections column is populated using: =COUNTIFS('File-1'!A$2:A$20,A2,'File-1'!B$2:B$20,"<="&B2)
    C. The Order column is populated using: =IF(OR(D2=0,COUNTIFS(A$2:A2,A2,D$2:D2,D2)>D2),0,COUNTIFS(A$2:A2,A2,D$2:D2,D2))
    2. A helper range, which may be moved and/or hidden for aesthetic purposes, is placed in H:M
    A. Column H is populated using: =IFERROR(INDEX(A$2:A$16,AGGREGATE(15,6,(ROW(A$2:A$16)-ROW(A$1))/(E$2:E$16>0),ROWS(A$1:A1))),"")
    B. Column I is populated using: =IF(H2="","",AGGREGATE(15,6,('File-1'!$B$2:$B$13)/('File-1'!A$2:A$13=H2),COUNTIFS(H$2:H2,H2)))
    C. Columns J:K are populated using: =IF(H2="","",INDEX(B$2:B$16,AGGREGATE(15,6,(ROW(A$2:A$16)-ROW(A$1))/($E$2:$E$16>0),ROWS(A$1:A1))))
    D. Column L is populated using: =IF(H2="","",VALUE(MID(K2,3,4)))
    E. Column M is populated using: =IF(H2="","",RANK.EQ(L2,L$2:L$16,1))
    3. The output, highlighted in green, on the OP sheet is populated using: =IFERROR(INDEX('File-2'!H$2:H$16,MATCH(ROWS(A$1:A1),'File-2'!$M$2:$M$16,0)),"")
    Perhaps one of the other contributors will be able to shorten the process.
    Let us know if you have any questions.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    03-14-2019
    Location
    Kolkata, India
    MS-Off Ver
    2016
    Posts
    9
    Quote Originally Posted by JeteMc View Post
    Here is a fairly lengthy proposal for the tricky requirements.
    1. Notice that on the File-2 sheet the range is changed to a table with two extra columns.
    A. The three original columns are custom sorted first by Key and then by Departure.
    B. The Connections column is populated using: =COUNTIFS('File-1'!A$2:A$20,A2,'File-1'!B$2:B$20,"<="&B2)
    C. The Order column is populated using: =IF(OR(D2=0,COUNTIFS(A$2:A2,A2,D$2:D2,D2)>D2),0,COUNTIFS(A$2:A2,A2,D$2:D2,D2))
    2. A helper range, which may be moved and/or hidden for aesthetic purposes, is placed in H:M
    A. Column H is populated using: =IFERROR(INDEX(A$2:A$16,AGGREGATE(15,6,(ROW(A$2:A$16)-ROW(A$1))/(E$2:E$16>0),ROWS(A$1:A1))),"")
    B. Column I is populated using: =IF(H2="","",AGGREGATE(15,6,('File-1'!$B$2:$B$13)/('File-1'!A$2:A$13=H2),COUNTIFS(H$2:H2,H2)))
    C. Columns J:K are populated using: =IF(H2="","",INDEX(B$2:B$16,AGGREGATE(15,6,(ROW(A$2:A$16)-ROW(A$1))/($E$2:$E$16>0),ROWS(A$1:A1))))
    D. Column L is populated using: =IF(H2="","",VALUE(MID(K2,3,4)))
    E. Column M is populated using: =IF(H2="","",RANK.EQ(L2,L$2:L$16,1))
    3. The output, highlighted in green, on the OP sheet is populated using: =IFERROR(INDEX('File-2'!H$2:H$16,MATCH(ROWS(A$1:A1),'File-2'!$M$2:$M$16,0)),"")
    Perhaps one of the other contributors will be able to shorten the process.
    Let us know if you have any questions.
    Thank You very much for the help. It works perfectly on the dataset that I had provided, but faces issues with the complete dataset that I have. I guess it has more to do with the rank as the solution considers the service to have a numeric value at the end like AA15, AA1, etc. In the full dataset we have instances like AAN, AAU, etc. Can we tackle this?

  11. #11
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,548

    Re: Matching between two sheets and find the closest time match

    We can try. Please re upload your the file attached to post #7 but with service values as described in post #10. Remember to include the expected outcome.
    Let us know if you have any questions.
    By the way, there is no need to quote whole posts on this site, in most cases just using Quick Reply is sufficient.

  12. #12
    Registered User
    Join Date
    03-14-2019
    Location
    Kolkata, India
    MS-Off Ver
    2016
    Posts
    9

    Re: Matching between two sheets and find the closest time match

    Thanks for your response. I have uploaded the file with values as described in post #10. Let me know if there are any issues with the same.

    Thank You.
    Regards,
    Arindam
    Attached Files Attached Files

  13. #13
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,548

    Re: Matching between two sheets and find the closest time match

    This is not quite as automatic as the earlier proposal.
    1. On the File-2 sheet column D is manually filled 1...16 (many ways to do this).
    2. The Connections and Oder columns are populated as described previously.
    3. The Rank column is populated using: =IF(F2=0,"",1+COUNTIFS(F$2:F$16,"<>0",D$2:D$16,"<"&D2))
    4. The entire range is then sorted by ascending departure dates.
    On the OP sheet:
    The Key, Departure and Series columns are populated using: =IFERROR(INDEX('File-2'!A$2:A$16,MATCH(ROWS(A$1:A1),'File-2'!$G$2:$G$16,0)),"")
    Arrival: =IF(G2="","",AGGREGATE(15,6,('File-1'!$B$2:$B$13)/('File-1'!A$2:A$13=G2),COUNTIFS(G$2:G2,G2)))
    Let us know if you have any questions.

  14. #14
    Registered User
    Join Date
    03-14-2019
    Location
    Kolkata, India
    MS-Off Ver
    2016
    Posts
    9

    Re: Matching between two sheets and find the closest time match

    Hi JeteMc,

    Thank You. I does do the trick on the dataset, but my original dataset that is expected would be more than 30k rows, which might run into trouble, I guess.

    I am sorry for complicating the query further, but if this is not possible then it is okay.

    Thank You.
    Best,
    Arindaam

  15. #15
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,548

    Re: Matching between two sheets and find the closest time match

    Perhaps the following will help.
    On the 'File-2' sheet I1 is populated using: =COUNTA(A2:A100000)
    Select cell D2 and type the number 1 > select the fill handle of cell D2 > right click the mouse > pull down then back up
    A list of options should appear > select the last option 'Series'
    In the 'Series' dialog box choose 'Columns' > type the number displayed in cell I1 into the 'Stop value:' window and select OK.
    Select cell B2 and Sort A to Z (oldest to newest)
    Let us know if you have any questions.

  16. #16
    Registered User
    Join Date
    03-14-2019
    Location
    Kolkata, India
    MS-Off Ver
    2016
    Posts
    9

    Re: Matching between two sheets and find the closest time match

    Unfortunately, it did not

  17. #17
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,548

    Re: Matching between two sheets and find the closest time match

    Could you tell us at which part of the suggestion in post #15 didn't work or, better yet, upload a sample of the file that demonstrates the difficulties that you are having.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Find address of closest match/second closest match
    By L.LEE in forum Excel General
    Replies: 1
    Last Post: 11-13-2018, 11:56 PM
  2. Replies: 3
    Last Post: 10-19-2018, 05:45 AM
  3. Compare to sheets with very similar data to find closest match
    By ashley72788 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-10-2017, 02:34 AM
  4. match closest date from within range matching ID number
    By Vend1301 in forum Excel General
    Replies: 4
    Last Post: 11-18-2015, 02:53 PM
  5. Find the closest time match
    By VBAasdf in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-06-2015, 12:20 PM
  6. Replies: 2
    Last Post: 12-04-2013, 01:01 PM
  7. [SOLVED] match closest date from within range matching ID number
    By adrianjaeggi in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-09-2012, 03:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1