+ Reply to Thread
Results 1 to 12 of 12

Find the latest date for each month

  1. #1
    Registered User
    Join Date
    04-19-2017
    Location
    Bangkok
    MS-Off Ver
    MS OFFICE 2010
    Posts
    3

    Find the latest date for each month

    I have a range of date with fuel price and I'd like to find the latest fuel price for each month.
    May I know how should I do it?
    Thank you in advance.

    A B C
    1 Date DSL GSL
    2 5-Jan-17 35.06 27.68
    3 13-Jan-17 34.46 27.08
    4 16-Jan-17 34.46 27.08
    5 19-Jan-17 34.76 27.38
    6 4-Feb-17 35.36 27.98
    7 16-Feb-17 35.36 27.98
    8 23-Feb-17 34.96 27.58

  2. #2
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Find the latest date for each month

    cchi3k welcome to the forum.

    Try this. I used a lookup table in column J and the year in cell E1 to expedite month / year look ups.

    Then this formula in F2:H3
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    The formula only works properly if the dates are in ascending order. If else this needs different formula(s).



    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    1
    Date
    DSL
    GSL
    2017
    Date
    DSL
    GSL
    January
    2
    5-Jan-17
    35.06
    27.68
    January
    19-Jan-17
    34.76
    27.38
    February
    3
    13-Jan-17
    34.46
    27.08
    February
    23-Feb-17
    34.96
    27.58
    March
    4
    16-Jan-17
    34.46
    27.08
    April
    5
    19-Jan-17
    34.76
    27.38
    May
    6
    4-Feb-17
    35.36
    27.98
    June
    7
    16-Feb-17
    35.36
    27.98
    July
    8
    23-Feb-17
    34.96
    27.58
    August
    9
    September
    10
    October
    11
    November
    12
    December
    Last edited by FlameRetired; 04-19-2017 at 12:54 AM.
    Dave

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,944

    Re: Find the latest date for each month

    Hi, welcome to the forum

    See if this works for you...
    A
    B
    C
    D
    E
    F
    1
    Date DSL GSL
    2
    5-Jan-17
    35.06
    27.68
    1/1/2017
    34.76
    3
    13-Jan-17
    34.46
    27.08
    2/1/2017
    34.96
    4
    16-Jan-17
    34.46
    27.08
    5
    19-Jan-17
    34.76
    27.38
    6
    4-Feb-17
    35.36
    27.98
    7
    16-Feb-17
    35.36
    27.98
    8
    23-Feb-17
    34.96
    27.58

    column E contains your months (dates)
    F2=INDEX($B$2:$B$8,MATCH(MAX(IF(($A$2:$A$8>=E2)*($A$2:$A$8<=EOMONTH(E2,0)),$A$2:$A$8)),$A$2:$A$8,0))
    ...confirmed by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER. You will know the array is active when you see curly braces { } appear around your formula. If you do not CTRL+SHIFT+ENTER you will get an error or a clearly incorrect answer.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Registered User
    Join Date
    04-19-2017
    Location
    Bangkok
    MS-Off Ver
    MS OFFICE 2010
    Posts
    3

    Re: Find the latest date for each month

    Thanks much Dave.
    Wow, it looks complicated. Btw, may I know what does "1E+306" mean? And why need "1/(month(...))"?
    Appreciate it.

  5. #5
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,944

    Re: Find the latest date for each month

    My suggest does not need the dates sorted

  6. #6
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Find the latest date for each month

    The 1E+306 is a sufficiently large number guaranteed to be larger than any of the numbers in the 1/(month(...)) array. Expressed in scientific notation it is 1 followed by 306 zeros. I used it mostly out of habit. In this case 2 would have been sufficient and 2 is what I will use below.

    This is how it works:

    (MONTH($A$2:$A$8)=MATCH($E2,$J$1:$J$12,0))*($E$1=YEAR($A$2:$A$8)) returns an array of

    {TRUE;TRUE;TRUE;TRUE;FALSE;FALSE;FALSE}*{TRUE;TRUE;TRUE;TRUE;TRUE;TRUE;TRUE}

    When any math operator is applied to an array of TRUE / FALSE it coerces TRUE and FALSE into their underlying numeric values of 1 and 0. The result in this case looks like this:

    {1;1;1;1;0;0;0}

    When reciprocated 1/{1;1;1;1;0;0;0} it returns

    {1;1;1;1;#DIV/0!;#DIV/0!;#DIV/0!}

    For simplicity let's use 2 for the lookup value instead of 1E+306.

    =LOOKUP(2,{1;1;1;1;#DIV/0!;#DIV/0!;#DIV/0!},A$2:A$8)

    LOOKUP will match the largest number less than or equal to 2 that is furthest to the right in the array. Fortunately LOOKUP also ignores #DIV/0! errors. In this case LOOKUP matches the 2 to the 4th 1 in the array (which is in the fourth row). It returns the value in A2:A8 corresponding to that same row.

    Since A$2:A$8 is addressed relative with respect to column A as the formula is copied across that column reference becomes column B and then C returning the corresponding the Dates, DSL and GSL prices.

    BTW: You can select any step in the formula in the formula bar an press the F9 function key. You will see the arrays as presented above. You can also select any of the formula cells and use 'Evaluate formula' ... FORMULAS > Evaluate formula > click Evaluate button repeatedly. You will see a step by step presentation of how Excel evaluates the arrays in the view port.

    The F9 and Evaluate formula are excellent tools for analyzing, trouble shooting formulas and self instruction. I recommend them whenever I can.

    Does this help?

  7. #7
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Find the latest date for each month

    @ Ford
    Yes. I tried to find a way to do that without an array formula. Even LOOKUP had to be committed Ctrl + Shift + Enter in all my attempts. I think I was doing something wrong.

  8. #8
    Forum Expert
    Join Date
    12-03-2009
    Location
    Florence, Italy
    MS-Off Ver
    Excel 2019
    Posts
    1,796

    Re: Find the latest date for each month

    Hi All

    I'm assuming to work on yearly statistics: data in A1:C300

    =SUMIF($A$1:$A$300,AGGREGATE(14,6,$A$1:$A$300/(MONTH($A$1:$A$300)=ROW($A1)),1),B$1:B$300)

    or

    =SUMIF($A$1:$A$300,MAX(INDEX($A$1:$A$300*(MONTH($A$1:$A$300)=ROW($A1)),)),B$1:B$300)

    copied down 12 times on two columns.


    Regards
    -----------------------------------------------------

    At Excelforum, you can say "Thank you!" by clicking the star icon ("Add Reputation") below the post.

    Please, mark your thread [SOLVED] if you received your answer.

  9. #9
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,944

    Re: Find the latest date for each month

    Quote Originally Posted by FlameRetired View Post
    @ Ford
    Yes. I tried to find a way to do that without an array formula. Even LOOKUP had to be committed Ctrl + Shift + Enter in all my attempts. I think I was doing something wrong.
    Whats wrong with using an array formula? Do you have a ton of data to go through?

  10. #10
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Find the latest date for each month

    No. Nothing wrong.

    That's just me going through a period ... looking for non-array alternatives. I tend to over use them and when they're not necessary. Need to learn some new 'tricks'.

  11. #11
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,944

    Re: Find the latest date for each month

    Quote Originally Posted by FlameRetired View Post
    No. Nothing wrong.

    That's just me going through a period ... looking for non-array alternatives. I tend to over use them and when they're not necessary. Need to learn some new 'tricks'.
    oops sorry, thought that post was from the OP duh

  12. #12
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2404
    Posts
    13,406

    Re: Find the latest date for each month

    That's OK.

    It needed to be said anyway. LOL

+ 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] Choose Latest Date and Number of Month Criteria for Future Date
    By ShakJames in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-07-2017, 12:31 PM
  2. Latest Date in Any Month
    By andrewc in forum Excel General
    Replies: 9
    Last Post: 08-11-2015, 05:51 AM
  3. Replies: 9
    Last Post: 12-21-2014, 06:29 PM
  4. Copy latest date of every month from a column in excel 2007
    By rt99 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-07-2013, 11:49 AM
  5. Replies: 1
    Last Post: 04-04-2012, 08:21 AM
  6. [SOLVED] Find latest date for each row
    By scuba79 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2005, 06:05 PM
  7. Find latest date
    By john.bedford3 in forum Excel General
    Replies: 2
    Last Post: 02-13-2005, 03:06 PM

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