Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2020 : Technical Questions
Message Icon Topic: Subtraction formula Post Reply Post New Topic
Page  of 2 Next >>
Author Message
clumbsy
Newbie
Newbie
Avatar

Joined: 24 Sep 2009
Location: United States
Online Status: Offline
Posts: 16
Quote clumbsy Replybullet Topic: Subtraction formula
    Posted: 25 Sep 2009 at 5:25am

I have a report grouped by account number , sorted by Datetime

my datetime populates individual read (electric meter reads)
 
example
9-5-09     6200
9-4-09     6000
9-3-09     5800
 
what I need to do is to get a daily use which would require
taking 9-5-09 6200 minus 9-4-09 6000 to get usage of 200 and so forth down the calendar for each account. Then just pull in any days that have a negative figure.
 
IP IP Logged
FrnhtGLI
Senior Member
Senior Member
Avatar

Joined: 22 May 2009
Online Status: Offline
Posts: 347
Quote FrnhtGLI Replybullet Posted: 25 Sep 2009 at 6:11am
So is your report one that only shows negative usage?
 
You could do something like:
 
   global numbervar nUsage:=0;
   global numbervar nUsage:={usage}-next{usage};
   nUsage;
 
or even just:
 
   {usage}-next{usage}
 
You will have to be sure that {usage} is a currency or amount though.
 
This will work if you are trying to show usage starting with the first records. For instance, in your example, this would yeild 200 for date 9-5-09. If you wanted it the other way around change Next  to Previous.
 
One problem you may have though is when the formula doesn't have a next or a previous record to calculate. Such instance would be true for the last record with Next and the first record with Previous. You could start with this and move on from there.
 
Another idea would be to use nextisnull and previousisnull. Something like:
 
   global numbervar nUsage:=0;
   global numbervar nUsage:={Usage}- (if nextisnull({Usage}) then
   {Usage} else next({Usage}));
   nUsage;
 
This should give you an amount of 0 if there is no next usage field (replace nextisnull with previousisnull if you are doing it the other way).
 
Once you have the usage calculated, suppress detail sections where nUsage>0.
 
Hope this helps or at least gives you some ideas.


Edited by FrnhtGLI - 25 Sep 2009 at 6:12am
IP IP Logged
clumbsy
Newbie
Newbie
Avatar

Joined: 24 Sep 2009
Location: United States
Online Status: Offline
Posts: 16
Quote clumbsy Replybullet Posted: 25 Sep 2009 at 7:18am
Oh my, I have not even heard of global var. will look it up.
Thanks
 
I have three crystal books and can not find global var in any.


Edited by clumbsy - 25 Sep 2009 at 7:23am
IP IP Logged
FrnhtGLI
Senior Member
Senior Member
Avatar

Joined: 22 May 2009
Online Status: Offline
Posts: 347
Quote FrnhtGLI Replybullet Posted: 25 Sep 2009 at 7:41am
Have you ever worked with local variables?
 
Essentially, a local variable is restricted to one formula and that value cannot be accessed from a different formula.
 
A global variable lets you access the value in any formula that declares the variable (except for sub reports, for that you would need a shared variable).
 
Variables are pretty useful. I suggest getting familiar with them.


Edited by FrnhtGLI - 25 Sep 2009 at 7:41am
IP IP Logged
clumbsy
Newbie
Newbie
Avatar

Joined: 24 Sep 2009
Location: United States
Online Status: Offline
Posts: 16
Quote clumbsy Replybullet Posted: 25 Sep 2009 at 7:43am
No I haven't.
Thanks for the info.
IP IP Logged
clumbsy
Newbie
Newbie
Avatar

Joined: 24 Sep 2009
Location: United States
Online Status: Offline
Posts: 16
Quote clumbsy Replybullet Posted: 25 Sep 2009 at 12:11pm
global numberVar nUsage :=0;
global numberVar nUsage :={BI_INTERVAL_RDGS.BI_RDG}-(if nextisnull({BI_INTERVAL_RDGS.BI_RDG})then{BI_INTERVAL_RDGS.BI_RDG}
else next ({BI_INTERVAL_RDGS.BI_RDG}));
nUsage;
This is what I have and is populating the correct information other than my oldest date is beginning its calculation with the read from next record.
Am I missing on how to start new on change of record?
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 25 Sep 2009 at 12:16pm
usually if the number is to be reused, you write a simple formula to reset it, like your first line:
global numbervar nUsage :=0;
""//hides the display of the 0 on the report.
 
then you place it, usually in the header of the group that you want to sum on.  Every time the group changes, the variable will be reset to 0.
 
HTH
 
IP IP Logged
FrnhtGLI
Senior Member
Senior Member
Avatar

Joined: 22 May 2009
Online Status: Offline
Posts: 347
Quote FrnhtGLI Replybullet Posted: 25 Sep 2009 at 12:46pm
My impression was that the numbers were not to be reused in the report and that the formula was going on a detail line.
 
The first line of the formula:
 
global numbervar nUsage:=0
 
as lockwelle said resets the variable to 0. If this formula (the full formula from above) is placed on a detail line, it will:
 
1st - set the field to zero.
2nd - calculate the number (current usage -(current usage(if there is no
     next record) or next usage(if there is a next record))).
3rd - display the calculated number.
 
This process will happen every time the detail line the formula is place on is evaluated, therefore should always start with zero then calculate and display the calculated number.
IP IP Logged
clumbsy
Newbie
Newbie
Avatar

Joined: 24 Sep 2009
Location: United States
Online Status: Offline
Posts: 16
Quote clumbsy Replybullet Posted: 25 Sep 2009 at 12:53pm

It is in the detail section, sorry I must be miss understanding. Is global numbervar nUsage:=0 suppose to set it to zero after each change of group. I appologize I should have stated change of group not each record.

 
 


Edited by clumbsy - 25 Sep 2009 at 12:57pm
IP IP Logged
FrnhtGLI
Senior Member
Senior Member
Avatar

Joined: 22 May 2009
Online Status: Offline
Posts: 347
Quote FrnhtGLI Replybullet Posted: 25 Sep 2009 at 1:02pm
If you follow what lockwelle said it will.
 
You will have to take:
 
global numbervar nUsage:=0;
 
out of the original formula and create a new 'Initialize' formula with only that in it. Place the new formula in the group header and the 'Calculate' formula in the detail section.
 
If you have multiple groups, you will have to put the 'Initialize' formula in each group header.


Edited by FrnhtGLI - 25 Sep 2009 at 1:08pm
IP IP Logged
Page  of 2 Next >>
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.047 seconds.