473,804 Members | 2,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding percent change between two rows

2 New Member
Hi,
This is driving me crazy, but I am sure I am missing something simple. I have built an Access 2007 report that shows 2 rows of sales data from each of a bunch of store locations. My table has the date, the location and the sales_amount. I need to have my report group on locations. The resulting rows include a date and the sales_amount, and I have been able to display the sales_amount for a requested date (through a parameter query) and the sales_amount from the same week in the previous year, and these rows are shown for each location. But I now need to be able to show the $ change in sales_amount and the percent change in sales_amount for each location between the current and previous year. This means that I somehow need to access data from (both of) the grouped rows and calculate and display the results. How do I access data from multiple rows? Do I have to create the report manually using VBA, and if so, any examples out there?

Here is an example of what I am trying to accomplish:
Table Sales has Location, Date and SalesAmount, and records such as:

loc1, 10/10/2007, $40
loc2, 10/10/2007, $40
loc3, 10/10/2007, $60
loc1, 10/10/2008, $50
loc2, 10/10/2008, $30
loc3, 10/10/2008, $70

I want a report that shows:
--- start of report ---

loc1
10/10/2008 $50
10/10/2007 $40
$change = $10 +25%
loc2
10/10/2008 $30
10/10/2007 $40
$change = -$10 -33%
loc2
10/10/2008 $70
10/10/2007 $60
$ change - $10 +17%

----- end of report -----
Oct 25 '08 #1
4 4334
NeoPa
32,579 Recognized Expert Moderator MVP
This will need to be done in code. Queries (SQL) have no concept of relative records.
Oct 25 '08 #2
ADezii
8,834 Recognized Expert Expert
Too close to bedtime, but given your demonstrated format, this code will produce the desired results, but only in a Linear Fashion:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim MyDB As DAO.Database
  3. Dim rstSales As DAO.Recordset
  4. Dim rstClone As DAO.Recordset
  5.  
  6. strSQL = "SELECT Sales.Location, Sales.Date, Sales.[Sales Amount] " & _
  7.          "FROM Sales ORDER BY Sales.Location, Sales.Date DESC;"
  8.  
  9. Set MyDB = CurrentDb
  10. Set rstSales = MyDB.OpenRecordset(strSQL, dbOpenDynaset)
  11. Set rstClone = rstSales.Clone
  12.  
  13. rstSales.MoveFirst
  14. rstClone.MoveFirst
  15. rstClone.Move 1     'Move to the 2nd Record in Clone
  16.  
  17. With rstSales
  18.   Do Until rstClone.EOF
  19.     If ![Location] = rstClone![Location] Then
  20.       Debug.Print ![Location] & " | " & ![Date] & " | " & ![Sales Amount] & _
  21.                   " | " & rstClone![Date] & " | " & rstClone![Sales Amount] & " | " & _
  22.                   Format$(![Sales Amount] - rstClone![Sales Amount], "Currency") & _
  23.                   " | " & Format$((![Sales Amount] - rstClone![Sales Amount]) / ![Sales Amount], "Percent")
  24.     End If
  25.     rstClone.MoveNext
  26.     .MoveNext
  27.   Loop
  28. End With
  29.  
  30. rstSales.Close
  31. rstClone.Close
  32. Set rstSales = Nothing
  33. Set rstClone = Nothing
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. loc1 | 10/10/2008 | 50 | 10/10/2007 | 40 | $10.00 | 20.00%
  2. loc2 | 10/10/2008 | 30 | 10/10/2007 | 40 | ($10.00) | -33.33%
  3. loc3 | 10/10/2008 | 70 | 10/10/2007 | 60 | $10.00 | 14.29%
NOTE: Any questions, feel free to ask.
Oct 26 '08 #3
gagnonconsulting
2 New Member
Thank you very much. So I have to use VBA and create a custom Report, which in hindsight, seems obvious. I will go and get a Access/VBA book and figure out the details, but this has helped me get started.
Oct 27 '08 #4
NeoPa
32,579 Recognized Expert Moderator MVP
Sounds like a plan :)

We're here if you need help of course.

Welcome to Bytes!
Oct 27 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1473
by: rozrabiak | last post by:
Hi! How can I get percent of executed procedure in MSSQL Server, lik in Enterprise Manager and/or dbMgr2k ? I use Visual C# and MSDE. Thank's for help, gregory
3
1341
by: JohnnyRuin | last post by:
Hi, My table has int Field1 and DateTime dtModificationTime. I maintain a history of changes to the table by inserting a new record with a new dtModificationTime. I need to be able to find the last inserted record. It's easy to do Select * from table where field1=x ORDER BY dtModificationTime. That works, but it hits all the records that match the field. I need to come back with an exact hit. Can someone point me in the...
4
22747
by: MX1 | last post by:
Hi all, I've setup a table with one field that will hold percent values. The type is number and the format is percent on the field. When I do data entry directly into the field, I have to put in .03 to get 3%. If I just enter 3, i get 300%. Is there a way to configure the table to let me type in 3 and have it enter as 3%? Thanks!
0
1254
by: Macbeth | last post by:
Thanks to All who Help, I am creating a crosstab querry to place in a report. All my data is correct and I do not seem to have any problem with this area. I am having problems with displaying the data in the format I need. My project is some what simple, I am displaying by the month(Rows)the ave Temp for several location(Columns). This works just fine but now I nees to display the percent of the time above a set point, suct as temp...
10
1759
by: tshad | last post by:
I have a Datagrid with a column: <asp:HyperLinkColumn DataTextField="JobTitle" DataNavigateUrlField="PositionID" DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}" HeaderText="Job Title" Visible="True" SortExpression="JobTitle"/>
39
3093
by: VidTheKid | last post by:
THE PROBLEM The % symbol is too vague when defining dimensions in CSS and HTML. It can relate to an inherited value, a measure of the containing element (which can differ between box models) or for background image placement, the size of the element minus the size of the image. A SOLUTION There should be new %-like units defined in CSS to specify what the measurement is a percentage of. These take the form of the % sign and one or...
14
2823
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given by df -k lopgod10:~/mycrfile # df -k /mnt/mvdg1/vset Filesystem 1K-blocks Used Available Use% Mounted on
1
1886
by: nycjay | last post by:
I am trying to do the following w/ SQL for db2 9... I want to find the top x percent of rows. For example, if i wanted to find the average sale price of the top 10 most expensive items in a store, how could I go about doing that, w/o a stored procedure? I've been searching, and have come across many solutions for other RDBMS, but not yet for DB2. Thanks in advance.
7
2352
by: Bernice J | last post by:
I have a table with four fields: BlockNo, District, SubDate, Status. I would like to create a report that 1)shows the percent of BlockNo that has a SubDate(IsNotNull) grouped by District and 2) shows the percent of BlockNo with SubDate(IsNotNull) and with Status=Y grouped by District. I was able to create queries for both of these and count the number of blocks in the query. What I am unable to figure out is how to refer to the TOTAL BlockNo...
0
10325
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10315
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9140
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.