Difference between Columns in Crosstab Query (1 Viewer)

stryfe3003

New member
Local time
Today, 07:35
Joined
Jul 22, 2021
Messages
3
Hi, I have a list of 10 products, say A, B, C, D...... and I log the sales of each product every day.
I created a crosstab query such that products are in the rows and columns are two dates that I specify in the query.
I wanted to find out whats the difference in sales between these two dates for each product. Eg
22 Jul 15 Jul Difference
Product A 10 3 7
Product B 15 20 -5
Product C 12 4 8

How can I create the above automatically? I have already created the userform to input the 2 dates required.
Thank you!
 

Ranman256

Well-known member
Local time
Yesterday, 19:35
Joined
Apr 9, 2015
Messages
4,337
user forms dont work well with crosstabs, since the fields can change.
you should view them in datasheet only.
but you can make a 'report' table that holds generic column , append the xtab to it , and the form can show the math on columns that wont change names.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:35
Joined
May 7, 2009
Messages
19,230
you need some VBA to add the Difference column.
 

Attachments

  • dbprouct.accdb
    608 KB · Views: 211

Gasman

Enthusiastic Amateur
Local time
Today, 00:35
Joined
Sep 21, 2011
Messages
14,265
Thanks very much, how do I reference to the column headers which changes according crosstab query result? Thanks again
Have you even looked at the db arnelgp uploaded ? :(
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 19:35
Joined
Feb 19, 2002
Messages
43,257
As Ranman suggested, using the crosstab to calculate the difference is the WRONG method. You need to use queries. I modified arnel's example. It doesn't require code to do the calculation so it will be easier for you to understand. It uses three queries.
query1 selects and summarizes the values for date1
query2 selects and summarizes the values for date2
query3 joins the two queries and calculates the difference. Since both query1 and query2 return the same rows, there won't be a problem with the inner join in query3. A more complicated method would use a full outer join.
 

Attachments

  • dbprouctPAT.accdb
    536 KB · Views: 220

stryfe3003

New member
Local time
Today, 07:35
Joined
Jul 22, 2021
Messages
3
That is very very helpful guys, much appreciated. Thank you!
 

Users who are viewing this thread

Top Bottom