Results 1 to 5 of 5
  1. #1
    dilbert is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2010
    Posts
    21

    Calculate difference from last value

    I am working on a Biggest Loser competition at work. I'm entering the date and weight in a form and saving it to it's own table. Then I query that data and print it on a report. It was pretty easy to get the date in the right order and output the weights. What I would also like is to display the change since their last weight. I am not storing this in the database, I suppose I could, but it seemed better to calculate it when needed, i.e. in case I made a data entry error and needed to edit a weight. I suspect this is not easy.

    This is how I'd like it to look.

    Date Weight
    11/1/2015 220
    11/5/2015 218 -2
    12/1/2015 218 0
    12/22/2015 219 1
    1/5/2016 215 -4

    This is what the (early draft) report looks like. It's in Access 2016.

    Click image for larger version. 

Name:	report.jpg 
Views:	9 
Size:	203.1 KB 
ID:	23430

    Any guidance is appreciated! And please be gentle, I'm an Access beginner.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    You need to pull value from previous record. Review: http://allenbrowne.com/subquery-01.html#AnotherRecord
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    dilbert is offline Novice
    Windows 7 32bit Access 2016
    Join Date
    Aug 2010
    Posts
    21
    Wow, thank you for the fast reply!

    I'm close, but clearly did something wrong.

    Here's the query:

    SELECT Weights.Weight, Weights.DateOfWeight, (SELECT TOP 1 Dupe.Weight
    FROM Weights AS Dupe
    WHERE Dupe.Contacts_ID = Weights.Contacts_ID
    AND Dupe.DateOfWeight < Weights.DateOfWeight
    ORDER BY Dupe.DateOfWeight DESC, Dupe.Contacts_ID) AS Change
    FROM Weights
    WHERE (((Weights.Contacts_ID)=[Forms]![Contact Details]![ID]))
    ORDER BY Weights.DateOfWeight;

    Here's the output.
    Weight DateOfWeight Change
    112 11/11/2015
    155 11/20/2015 112
    212 12/21/2015 155
    210 12/28/2015 212
    208 1/1/2016 210
    205 1/8/2016 208
    202 1/16/2016 205

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    No, it is correct. All the example does is pull the prior value, doesn't subtract it. Now that you have the value from previous record, subtract it. Do it in query or in textbox on report.

    SELECT Weights.Weight, Weights.DateOfWeight, Weight - (SELECT TOP 1 Dupe.Weight
    FROM Weights AS Dupe
    WHERE Dupe.Contacts_ID = Weights.Contacts_ID
    AND Dupe.DateOfWeight < Weights.DateOfWeight
    ORDER BY Dupe.DateOfWeight DESC, Dupe.Contacts_ID) AS Change
    FROM Weights
    WHERE (((Weights.Contacts_ID)=[Forms]![Contact Details]![ID]))
    ORDER BY Weights.DateOfWeight;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    dilbert is offline Novice
    Windows 7 32bit Access 2016
    Join Date
    Aug 2010
    Posts
    21
    Thank you so very much! I really appreciate your help, this is a great addition to the report.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 30
    Last Post: 06-16-2015, 08:37 AM
  2. Replies: 10
    Last Post: 11-08-2014, 07:28 PM
  3. Calculate time difference
    By desireemm1 in forum Access
    Replies: 19
    Last Post: 10-27-2014, 12:06 PM
  4. Replies: 5
    Last Post: 09-11-2013, 03:42 PM
  5. Replies: 0
    Last Post: 08-07-2008, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums