+ Reply to Thread
Results 1 to 10 of 10

Find nearest previous date for a date & given value combination

  1. #1
    Registered User
    Join Date
    02-28-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    6

    Find nearest previous date for a date & given value combination

    What I am trying to do is the following.

    I have a range of items that are sampled multiple times over a week and possibly more than once a day. I need a function (not a script since there will be too much endless computing in that way) that will look at the ID (Column A) and the Timestamp (Column D - Date and time) and look through all the data to find the corresponding ID with the previous nearest timestamp to be shown on the given line (say line 1) in column E1. E being the output of the desired equation.

    For instance:
    A - D E
    ID1 2017-11-14 5:20 "no previous"
    ID2 2017-11-14 5:25 "no previous"
    ID3 2017-11-14 5:25 "no previous"
    ID3 2017-11-14 5:30 "no previous"
    ID1 2017-11-15 6:20 2017-11-14 5:20

    The amount of rows will be ever expanding (timestamps) but the amount of unique IDs (column A) will be limited and be repeated (but possibly large).

    And so on. Any ideas?

    Thanks in advance
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by Jules1414; 02-28-2018 at 07:24 AM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    79,401

    Re: Find nearest previous date for a date & given value combination

    Welcome to the forum!

    Members use your location as a guide when offering solutions, which differ based on your locale - please change your profile from 'World' (useless) to something useful like 'US', 'UK', 'Europe', etc.

    Will you then please attach a sample Excel workbook? We are not able to work with or manipulate a picture of one and nobody wants to have to recreate your data from scratch.

    1. Make sure that your sample data are REPRESENTATIVE of your real data. The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired results are also shown (mock up the results manually).

    3. Make sure that all confidential data is removed or replaced with dummy data first (e.g. names, addresses, E-mails, etc.).

    4. Try to avoid using merged cells as they cause lots of problems.

    Unfortunately the attachment icon doesn't work at the moment, so to attach an Excel file you have to do the following: just before posting, scroll down to Go Advanced and then scroll down to Manage Attachments. Now follow the instructions at the top of that screen.

    Please pay particular attention to point 2 (above): without an idea of your intended outcomes, it is often very difficult to offer appropriate advice.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,005

    Re: Find nearest previous date for a date & given value combination

    Try

    in C2


    =IFERROR(IF(MATCH(A2,$A1:A$1,0),INDEX($B1:B$1,MATCH($A2,$A1:A$1,0)),""),"no previous")

  4. #4
    Registered User
    Join Date
    02-28-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    6

    Re: Find nearest previous date for a date & given value combination

    I've added a sample excel file to clarify. In the current form, the equation returns "no Previous" for all lines.
    Last edited by Jules1414; 02-28-2018 at 07:32 AM.

  5. #5
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,005

    Re: Find nearest previous date for a date & given value combination

    See attached and post #3
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    02-28-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    6

    Re: Find nearest previous date for a date & given value combination

    Thanks John! That's doing what I need it to do. Perfect.

  7. #7
    Registered User
    Join Date
    02-28-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    6

    Re: Find nearest previous date for a date & given value combination

    Oups, actually no. Sorry about the eager enthusiasm. I need it to return the nearest previous associated timestamp. the attached return sheet shows what it should return, but what it returns is the first entry ever. I basically need those outputs to calculate the time difference between each entries per ID.
    Attached Files Attached Files
    Last edited by Jules1414; 02-28-2018 at 07:55 AM.

  8. #8
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,005

    Re: Find nearest previous date for a date & given value combination

    Try

    in D2

    =IFERROR(IF(MATCH(A2,$A$1:$A1,0),INDEX($B$1:$B2,SMALL(IF($A$1:$A1=$A2,ROW($A$1:$A1)-ROW($A$1)+1,""),COUNTIF($A$1:$A1,A2)))),"No previous")


    Array formula: enter with Ctrl+Shift+Enter

    THEN copy down


    EDIT: non-array formula

    =IFERROR(IF(MATCH($A2,$A$1:$A1,0),LOOKUP(2,1/($A$1:$A1=$A2),$B$1:$B1)),"No previous")
    Attached Files Attached Files
    Last edited by JohnTopley; 02-28-2018 at 09:29 AM.

  9. #9
    Registered User
    Join Date
    02-28-2018
    Location
    Canada
    MS-Off Ver
    2013
    Posts
    6

    Re: Find nearest previous date for a date & given value combination

    Alright tested and also transferred to the required application on Google Sheet and works perfect -> first version (even if the values are not entered in chronological order - which was a concern I hadn't specified in my post). This will unleash the desired power for that cloud calculation system.

    Thanks very much for your help John.

  10. #10
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,005

    Re: Find nearest previous date for a date & given value combination

    You are welcome.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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. To compare & find out the nearest date in the range with a common date
    By rkulasekaran in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-04-2015, 08:08 AM
  2. [SOLVED] Find nearest Date/Time and copy
    By DHFE in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 11-23-2015, 04:17 AM
  3. [SOLVED] find nearest last date from tody date
    By shoot for moon in forum Excel General
    Replies: 5
    Last Post: 07-18-2015, 06:25 PM
  4. [SOLVED] Find the Exact date, Nearest old and new date for the given input Letter) and date
    By thilag in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-17-2015, 05:13 AM
  5. Compare and find nearest date with criteria
    By Dwexdwex in forum Excel General
    Replies: 4
    Last Post: 08-18-2013, 09:51 AM
  6. Replies: 0
    Last Post: 05-30-2013, 07:50 PM
  7. How to find nearest date to that of given
    By TRJJK73 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-16-2008, 06:30 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