Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: How Do I subtotal the change in a numeric field? Post Reply Post New Topic
Author Message
sundevilkid
Newbie
Newbie
Avatar

Joined: 08 Dec 2009
Location: United States
Online Status: Offline
Posts: 3
Quote sundevilkid Replybullet Topic: How Do I subtotal the change in a numeric field?
     Posted: 08 Dec 2009 at 8:20am
Ok, well, I didn't see an answer to this after searching for 30 minutes, so here goes:

I have the following table:

Unit ID    Department    Repair Date     Repair Odometer
101         01            2009-01-01     100,000
101         01            2009-03-01     104,000
101         01            2009-05-29     108,400
102         01            2009-01-13       92,000
102         01            2009-03-14       96,500
102         01            2009-06-01     101,000
103         02            2009-01-01      45,000
103         02            2009-06-01      59,000


I need to be able to summarize how many miles each vehicle traveled, then summarize the subtotal.

So far, I have been able to create a formula that gets the mileage at the start date and the mileage at the end date and subtracts the two. This helps avoid any data entry mistakes made in between the two dates (which is quite common with our user base, so I'm trying to make this foolproof).



numbervar MinMiles;
numbervar MaxMiles;
select {Repair Date}
case Minimum ({Repair Date}, {Unit ID}) :  MinMiles := {Repair Odometer}
case Maximum ({Repair Date}, {Unit ID}) :  MaxMiles := {Repair Odometer}
numbervar Miles := MaxMiles - MinMiles;


This works great when I put it in the group footer for the Unit, but I can't get a subtotal in the next level up, the group for the department.

Is there anything that will total the change in a field, and then is there any way to subtotal the sum of all the changes? I've spent the last day and a half trying 20 different ways to get this to work. So far they all have the same results, and this is the simplest formula I'd been using. Everything else was having to use a lot of if Unit ID <> Next (Unit ID) logic.



Edited by sundevilkid - 08 Dec 2009 at 2:31pm
IP IP Logged
sundevilkid
Newbie
Newbie
Avatar

Joined: 08 Dec 2009
Location: United States
Online Status: Offline
Posts: 3
Quote sundevilkid Replybullet Posted: 08 Dec 2009 at 2:25pm
Ok, I found a solution.

Of course, as soon as I got it finished, I hit preview one more time before I saved... and bam crw32.exe crashes. So I got to recreate it all over again :)

The solution:
I created a base formula, and placed this in the detail of the report and suppressed it:


//@Miles: Calc (Vehicle)

WhilePrintingRecords;
numbervar MinMiles;
numbervar MaxMiles;
select {@History: Repair Date}
case Maximum ({@History: Repair Date}, {vehfile.vehicle}) : MaxMiles := {@History: Repair Odometer}
case Minimum ({@History: Repair Date}, {vehfile.vehicle}) : MinMiles := {@History: Repair Odometer};
numbervar Miles := MaxMiles - MinMiles;


I have 3 groups, one by Facility, the next by Department, and third by the Vehicle #, as well as a grand total area.

So in order to get totals and a grand total, I have to have running totals for each of these. 

So I created three additional formulas, one for each total:


//@Miles: Calc (Dept)

WhilePrintingRecords;
numbervar Miles;
numbervar TotalMilesDept := TotalMilesDept + Miles;



//@Miles: Calc (Fac)

WhilePrintingRecords;
numbervar Miles;
numbervar TotalMilesFac := TotalMilesFac + Miles;



//@Miles: Calc (Grand)

WhilePrintingRecords;
numbervar Miles;
numbervar TotalMiles := TotalMiles + Miles;


In order to reset the totals though, I had to put an initialize formula in the group headers, so it would reset on each new change of group. Here I hit a snag though, because I was repeating group headers on each page, so it was resetting the values in the middle of the groups. So I adjusted the formula to test the previous values to see if this was a new group or not.


//@Miles: Initialize (Fac)

WhilePrintingRecords;
if {VehFacility.div_mstr_num} <> Previous ({VehFacility.div_mstr_num})
then numbervar TotalMilesFac := 0;



//@Miles: Initialize (Dept)

WhilePrintingRecords;
if {vehfile.division} <> Previous ({vehfile.division})
then numbervar TotalMilesDept := 0;


On the vehicle, I had to preload the Min and Max Miles that the base calculation would be using as well, otherwise I'd end up with some bad totals on vehicles with only 1 repair record.


//@Miles: Initialize (Vehicle)
WhilePrintingRecords;
if ({vehfile.vehicle} <> Previous ({vehfile.vehicle}) or OnFirstRecord)
then
(numbervar MinMiles := {@History: Repair Odometer};
 numbervar MaxMiles := {@History: Repair Odometer};
 numbervar Miles := 0;)


Again, each of the init formulas went into the group headers, and work even if you repeat group headers on each page.

Now, I could have just put the calc formulas on the report in the subtotals area, except they were only displaying the last value for miles (even though the totalmilesx variable was listed last, very odd). I fixed it by copying each of the totals formulas into group footer #3 (lowest subgroup), but then the totals were adding the last set of miles twice (which makes sense, since the calc formula adds the miles to it's last value). So, I left the calc formulas in the group #3 footer, and made a final set of formulas that only display the totalmiles and placed these in each of their respective group footers.


//@Miles: Totals (Vehicle)

Whileprintingrecords;
numbervar Miles;



//@Miles: Totals (Dept)

WhilePrintingRecords;
numbervar TotalMilesDept;



//@Miles: Totals (Facility)

WhilePrintingRecords;
numbervar TotalMilesFac;



//@Miles: Totals (Grand)

WhilePrintingRecords;
numbervar TotalMiles;


So the overall structure looked something like this:

GH#1: @Miles: Initialize (Fac) (Suppressed)
GH#2: @Miles: Initialize (Dept) (Suppressed)
GH#3: @Miles: Initialize (Vehicle) (Suppressed)
D: @Miles: Calc (Vehicle) (Suppressed)
GF#3: @Miles: Totals (Vehicle) (Shown)
GF#3: @Miles: Calc (Dept) (Suppressed)
GF#3: @Miles: Calc (Fac) (Suppressed)
GF#3: @Miles: Calc (Grand) (Suppressed)
GF#2: @Miles: Totals (Dept) (Shown)
GF#1: @Miles: Totals (Fac) (Shown)
RF: @Miles: Totals (Grand) (Shown)

IP IP Logged
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