Formula for members in a field

ringmaker

Board Regular
Joined
Oct 26, 2003
Messages
75
Hi - I am a MS Access beginner. I am using access instead of excel (where I am much more savvy) because my data set is huge and moving slowly. I have successly created an Access Pivot Table Form however I would like to create a formula which subtracts the items/members within one field from others within that field.

Here is an example of my data:

Proj Account Oct Nov Dec
ProjA Rev 100 150 200
Wages 25 35 40
Travel 5 3 1
Other Costs 3 0 1
ProjB Rev 200 300 400
Wages 50 70 80
Travel 10 6 2
Other Costs 6 0 2

I would like to create a formula of items within the account field. Namely Gross Profit = Rev - Wages - Travel - Other Costs. I'd like this new calculation to sit within the account field. I know this must be a snap but I can't seem to figure it out. Thanks in advance for your help.
 
Hi,

You will need 2 more queries to take account of the nil values and changing values. I purposely held this back so that you could get started without getting confused. If you base the 2 new queries on the 3rd query but save them as queries 4 and 5 then you are already half way there.

Query 4 : an append that was query 3 (keep query 3 intact) but saved as query 4, add a new column being the [total_expenses], append it to nothing (i.e. leave the append line blank), in the criteria section under [total_expenses] enter "Is Null" (without the quotes), then change this bit of code :
[total_revenue] - [total_expenses]

to this :
[total_revenue] - iif(isnull([total_expenses]),0,[total_expenses]) {late edit - or you could try leaving it as just [total_revenue]}

and in the criteria section under that (i.e. the gp value) enter "<>0" (without the quotes) and lastly, double click on each line (one at a time) linking the two queries (in the top half of the screen) and change the Join Property to include all records from the first query and only the matching records from the second query.

Save and Run.

Query 5 : an append that was also query 3 but saved as query 5 (keep query 3 intact), add a new column being the [total_revenue], append it to nothing (i.e. leave append blank), in the criteria section under [total_revenue] enter "Is Null" (no quotes), then change this bit of code :
[total_revenue] - [total_expenses]

to this :
iif(isnull([total_revenue]),0,[total_revenue]) - [total_expenses] {late edit - or you could try leaving it as just minus[total_expenses]}

and in the criteria section enter "<>0" (without the quotes) and lastly, double click on each line (one at a time) linking the two queries (in the top half of the screen) and change the Join Property to include all records from the second query and only the matching records from the first query (<- note the change).

An extra change you will need to make to query 5 is in the "project" and "month" columns - where the table say "query1" you need to change it to "query2" for both items.

Save and Run.

To automate all of this (i.e. to accommodate new data and changing value), you can create one more query (i.e. a delete query to delete all records from the main table where the account = "gp") and then roll up the new delete query as well as queries 3, 4 & 5 into a single macro.

If you go into the macro screen, create a new macro, one of the drop down commands is "open query" <- this is the one you want to run to get the queries to run. Please be aware that you only run one query at a time, first the new delete query to flush out the old data, then queries 3, 4 & 5 to append the gp values, but they can all be in the same macro. Don't forget to include the stop macro command as the last line in the macro.

If you save and run the macro it will then run the 4 queries for you - in the correct sequence.

Once you are happy the macro is working ok then you might also want to turn off the pop up warnings by setting the "setwarnings" value to "no" at the start of the macro and then turning it back on at the end (plus the "echo" on and off command stops the screen going beserk).

Sorry for the long and involved answer but you can see why I held it back from my earlier response.

HTH, Andrew.:)
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,221,827
Messages
6,162,200
Members
451,753
Latest member
freddocp

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top