Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calculate number of days

Posted on 2014-11-24
6
Medium Priority
?
118 Views
Last Modified: 2014-11-25
I have a query that calculates Event start and end dates for people with living status changes.  What I need to do is put them into columns showing first quarters for a year, then by year after that.  

In the attached screen shot, the left side is already done in a query.  The section in yellow is what I need to calculate.  I was thinking of adding a query that would calculate each clients date range column, for example, for the first client there would be a column called 90Date which would contain  dateadd("m",3,#1/7/2014#), 180Date column, etc. .  The second client would have dateadd('m',3,#10/11/13#) and so on.  Then use these calculated dates to calculate the number of days for each event.  But I have a feeling there is a much easier way, I just don't see it right now.

Screen shot of query
0
Comment
Question by:UniqueData
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 40463530
All you need to do is just to carry the earliest FromDate for the client on each of the rows. That way it will be very easy to create the columns in the query, or even as a expressions directly in the report. If you do not carry the FromDate you will have to use a sub-query (SELECT MIN(FromDate) From LiveChanges Where ClientId = a.ClientAd) for every row.
If you could show us your original query we can work something out
0
 
LVL 7

Author Comment

by:UniqueData
ID: 40463608
Actually, there are more columns that I didn't do a screen shot of.  There is a column called Date_Opened which is in each row.  So in the first example, the date 1/7/2014 is in the Date_Opened for each record for that client.

So, given that Date_Opened field, what I can't figure out is the formula needed to populate the yellow columns.
0
 
LVL 25

Assisted Solution

by:chaau
chaau earned 1000 total points
ID: 40463615
ok, this is easy. To do so in the query use this columns:
SELECT Date_Opened,
.... as FromDate,
.... as ToDate,
Iif(ToDate >DateAdd("m", 3, Date_Opened) , DateDiff("d", ToDate, DateAdd("m", 3, Date_Opened) ), 0) as 3mo,
Iif(ToDate >DateAdd("m", 6, Date_Opened) , DateDiff("d", ToDate, DateAdd("m", 6, Date_Opened) ), 0)  as 6mo,
Iif(ToDate >DateAdd("m", 9, Date_Opened) , DateDiff("d", ToDate, DateAdd("m", 9, Date_Opened) ), 0)  as 9mo,
Iif(ToDate >DateAdd("y", 1, Date_Opened) , DateDiff("d", ToDate, DateAdd("y", 1, Date_Opened) ), 0)  as 1yr,
Iif(ToDate >DateAdd("y", 2, Date_Opened) , DateDiff("d", ToDate, DateAdd("y", 2, Date_Opened) ), 0)  as 2yr,
Iif(ToDate >DateAdd("y", 3, Date_Opened) , DateDiff("d", ToDate, DateAdd("y", 3, Date_Opened) ), 0)  as 3yr
....
From theTable

Open in new window

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 51

Accepted Solution

by:
Gustav Brock earned 1000 total points
ID: 40464255
You'll be best off with a custom function as shown below.

Then your query could go like this:

Select
    ClientId,
    Date_Opened,
    FromDate,
    ToDate,
    DaysOfStatus(Date_Opened, FromDate, ToDate, 0, 3) As [0-3 mo],
    DaysOfStatus(Date_Opened, FromDate, ToDate, 3, 3) As [4-6 mo],
    DaysOfStatus(Date_Opened, FromDate, ToDate, 6, 3) As [7-9 mo],
    DaysOfStatus(Date_Opened, FromDate, ToDate, 9, 3) As [10-12 mo],
    DaysOfStatus(Date_Opened, FromDate, ToDate, 12, 12) As [1-2 yr],
    DaysOfStatus(Date_Opened, FromDate, ToDate, 24, 12) As [2-3 yr]
From
    tblYourTable
Public Function DaysOfStatus( _
    ByVal datOpen As Date, _
    ByVal datFrom As Date, _
    ByVal datTo As Date, _
    ByVal intMonthFirst As Integer, _
    ByVal intMonths As Integer) _
    As Variant
    
    Dim datFirst    As Date
    Dim datLast     As Date
    Dim varDays     As Variant
    
    datFirst = DateAdd("m", intMonthFirst, datOpen)
    datLast = DateAdd("m", intMonthFirst + intMonths, datOpen)
    
    If DateDiff("d", datFirst, datFrom) > 0 Then
        datFirst = datFrom
    End If
    If DateDiff("d", datLast, datTo) < 0 Then
        datLast = datTo
    End If
    
    varDays = DateDiff("d", datFirst, datLast)
    If varDays <= 0 Then
        varDays = Null
    End If
    
    DaysOfStatus = varDays
    
End Function

Open in new window

/gustav
0
 
LVL 7

Author Closing Comment

by:UniqueData
ID: 40464917
Worked perfectly.  I went with Gustav's because the function helped keep the query a little cleaner but it looks like chaau's is basically the same thing. :)

Thank you both for your time.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 40464938
You are welcome!

/gustav
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

721 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