Compare and calculate the number of days between records (date field), and resetting the compare/calculation for each unique ID

Posted on 2014-01-23
Last Modified: 2014-01-23
Hello, Assistance with comparing and calculating the number of days between records, then resetting for each unique ID is much needed! Example:
P-ID      I-ID      TestDate            Difference
1      100      3/2/2012                      0
2      101      2/14/2012      0
3      102      2/29/2012      0
4      103      8/29/2012      0
5      104      8/20/2012      0
5      105      10/25/2012      66
6      106      2/29/2012      0
6      107      2/29/2012      0
7      108      7/20/2012      0
8      109      7/11/2012      0
9      110      4/3/2012                      0
10      111      9/11/2012      0
11      112      8/7/2012                      0
12      113      3/5/2012                      0
13      114      1/24/2012      0
14      115      3/19/2012      0
15      116      1/17/2012      0
16      117      4/6/2012                      0
16      118      6/13/2012      68
17      119      2/14/2012      0
18      120      10/2/2012      0
19      121      1/25/2012      0
19      122      5/4/2012                      24

My data source is an unupdatable query. Much thanks in advance!!
Question by:jaguar5554
  • 2
LVL 34

Expert Comment

by:Dan Craciun
ID: 39804955
See attached Excel file.
Basically it checks if 2 consecutive values in the first column are equal, it calculates the no of days between the dates in the 3rd column.

LVL 120

Accepted Solution

Rey Obrero (Capricorn1) earned 250 total points
ID: 39805000
try this query

SELECT urTable.[p-id], urTable.[i-id], urTable.testDate, DateDiff("d",(select min(b.testDate) from urTable as B where B.[p-id]=urtable.[p-id] and b.[i-id]<=urtable.[i-id]),[testdate]) AS difference
FROM urTable;
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 250 total points
ID: 39805002
Btw, if you want to have more than 2 consecutive ID's, modify the formula as follows:

=IF($A2=$A3, DAYS($C3,$C2) + $D2,0)

Basically, this will add the number of days to the previous value, so in case of multiple identical IDs the last value will be the total number of days for that ID. If you only have max 2 consecutive IDs, then the formula is identical in function to the one in the sheet.

Author Closing Comment

ID: 39805308
Experts! Both of the solutions work perfectly, and both deserve the full 500 points. Unfortunately, I am limited to only 500 points, so I gave the most I could to each (250 points). I selected the query as the best solution because I'm working in an MS Access database; however, the MS Excel solution will certainly be implemented into other of my data projects. Thank you Thank you Thank you.

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

777 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