Solved

How to find the Maximum Date from Columns

Posted on 2015-01-13
13
182 Views
Last Modified: 2015-01-18
I am trying to pick the correct date (Result Date) when comparing two dates in an Access query.  I am able to determine which date to pick first in an IIf statement if one of the date fields is Null by the following function:

=iif(IsNull(Second Date), First Date)

but I haven't been able to figure out how to add the "then" to pick the Maximum of the remaining dates.  So my data currently looks like (see attachment for clearer view of columns):

First Date                    Second Date        Result Date
3/19/2012                      3/19/2012
10/14/2014      10/14/2014      
8/13/2014      11/24/2014      
3/18/2014                      3/18/2014
6/9/2014                6/9/2014
11/4/2013                      11/4/2013
6/20/2014      12/11/2014      
7/14/2014      11/22/2014      
9/10/2013      12/11/2014      
2/6/2014                2/6/2014
7/25/2012                      7/25/2012

I need guidance on how to get the blanks in the Result Date filled in with the max of the two dates.  Hope this makes sense.  Thanks.
C--Users-E221037-Desktop-Capture.PNG
0
Comment
Question by:tomfarrar
  • 6
  • 2
  • 2
  • +2
13 Comments
 
LVL 18

Accepted Solution

by:
Simon earned 250 total points
ID: 40547788
Try
=iif(nz([second date],#1/1/1900#)>nz([first date],#1/1/1900#),nz([second date],null),nz([first date],null))

I haven't got Access in front of me at the moment to test it, but it should give the more recent date of the two, or NULL if both dates are empty.

Or as a column definition in a query...
[Result Date]: IIf(Nz([second date],#01/01/1900#)>Nz([first date],#01/01/1900#),Nz([second date],Null),Nz([first date],Null))
0
 
LVL 7

Author Comment

by:tomfarrar
ID: 40547827
Hi Simon - It is close, but for some reason the second option does not appear to be working.  I am getting the minimum of the Second Date and the First Date.  I need the Max.  Am I looking at this wrong?
0
 
LVL 18

Expert Comment

by:Simon
ID: 40547840
When you say Max, I assume you mean the most recent of the two dates?

Are your fields actually dates or text representations of dates?

I just fired up my laptop to test this and I get the correct results (the later date of the two or the only date if one is blank or empty result column if both dates were blank).

Please let me know the actual dates you are using to test this, and possibly  try some extra test cases
e.g. 1/1/2014 v 12/31/2014
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40547861
try this

ResultDate: IIf([FirstDate]>Nz([SecondDate]),[FirstDate],[SecondDate])
0
 
LVL 7

Expert Comment

by:Robert Sherman
ID: 40547887
Alternatively, especially if you need to do this in more than one query, you can put the below code into a module.

This will give you the max of any number of values, and can be put into your query as
ResultDate: max([FirstDate],[SecondDate])

Function max(ParamArray p()) As Variant

    Dim i As Integer
    
    max = p(0)
    
    For i = 1 To UBound(p)
        If max < Nz(p(i), "") Then
            max = p(i)
        End If
    Next
    
End Function

Open in new window

0
 
LVL 7

Author Comment

by:tomfarrar
ID: 40547894
Hi Simon - They are date-defined in the tables.  Here is the actual function, and the result is shown in the attached.  

LAST_USED_DT: IIf(Nz([Transaction_Dates_JDE]![LST_XFER_DT],#1/1/1900#)>Nz([ConversionDateImport Table]![LST_XFER_DT],#1/1/1900#),Nz([Transaction_Dates_JDE]![LST_USED_DT],Null),Nz([ConversionDateImport Table]![LST_USED_DT],Null))
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 7

Author Comment

by:tomfarrar
ID: 40547897
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40547900
I'd try this

=iif(nz([Second Date],0)>=nz([First Date,0]), [Second Date], First Date)
Nz() is an isnull and iif wrapped together

But what do you want to have happen if both dates are null
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40547907
These make little sense
Nz([Transaction_Dates_JDE]![LST_USED_DT],Null)
Nz([ConversionDateImport Table]![LST_USED_DT],Null)
There's little point in testing for and replacing null just to replace it with null
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
ID: 40547914
@tomfarrar,

you have four date fields, using the date fields listed below
please explain your requirement

[Transaction_Dates_JDE]![LST_XFER_DT]
[Transaction_Dates_JDE]![LST_USED_DT]
[ConversionDateImport Table]![LST_XFER_DT]
[ConversionDateImport Table]![LST_USED_DT]
0
 
LVL 7

Author Comment

by:tomfarrar
ID: 40547924
Thanks, Rey, that is what happens when I get to the end of the day.  My mistake.  Should always be [LST_USED_DT] for both tables.  Now the functions are working.  I am going home, but will be back to confirm in the morning.  Thanks for everyone chipping in.  Sorry for the error.  - Reilly
0
 
LVL 7

Author Comment

by:tomfarrar
ID: 40556501
A lot of good input; thank you.
0
 
LVL 7

Author Closing Comment

by:tomfarrar
ID: 40556507
Thank, Simon, for the immediate answer as it was important.  Thanks, Rey, for the alternative solution, and pointing out my date error.  I am sure the rest of you have provided good detail that I hope to look at closer as time permits.  Thanks again.  - Reilly
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now