Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2020 : Technical Questions
Message Icon Topic: Subtracting a field based another field's value Post Reply Post New Topic
Page  of 2 Next >>
Author Message
bigburb
Newbie
Newbie


Joined: 07 Oct 2009
Location: United States
Online Status: Offline
Posts: 26
Quote bigburb Replybullet Topic: Subtracting a field based another field's value
    Posted: 07 Oct 2009 at 11:45am
Hello All, I'm a newbie so bear with me, running CR9. I'm trying to calculate a sum of employee hours and have it automatically subtract out the hours associated with lunch. The wall I'm hitting is that I don't know how to tell crystal that if "ind_code" = LUN then subtract the "act_hrs" associated with that record from the total.
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 07 Oct 2009 at 1:08pm
can you list your row level data and an example of how you need it calculated?
IP IP Logged
bigburb
Newbie
Newbie


Joined: 07 Oct 2009
Location: United States
Online Status: Offline
Posts: 26
Quote bigburb Replybullet Posted: 07 Oct 2009 at 1:33pm
Yes. Lunch is considered indirect labor for purposes of clocking in and out, but I don't want it included in the group summary. This is how I'd like it to look.

Name   Total Direct   Ind. Code   Total Indirect Ttl Hrs
Joe        1                                         0             1
Joe        0                      SHIP            1             1
Joe        0                      LUN            .5           .5
                                                         Total      2

I've tried using the Select Expert to choose only records that are <>LUN but it filters out all the direct labor too for some reason. I was hoping maybe there was a formula I could use to total all labor and subtract any indirect labor that used the indirect code of "LUN" for that employee.
IP IP Logged
FrnhtGLI
Senior Member
Senior Member
Avatar

Joined: 22 May 2009
Online Status: Offline
Posts: 347
Quote FrnhtGLI Replybullet Posted: 07 Oct 2009 at 1:38pm
Group by Employee Name and create a running total:
 
     global numbervar nTotalHrs:= nTotalHrs + (if {table.indcode}='LUN'
     then 0 else {table.ttlhrs});
 
     nTotalHrs;
//This is assuming that you want to include total hours into the final total for ALL ind code entries EXCEPT LUN.
 
 
Reset on the group header:
 
     global numbervar nTotalHrs:=0;


Edited by FrnhtGLI - 07 Oct 2009 at 1:44pm
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 07 Oct 2009 at 1:49pm

FrnhtGLI's variable formula should work.

as another option, likely the total direct rows have a NULL in the Ind. Code field and this was making them drop when you did your select statement (<>'Lun'). If so you can use the select statment to filter out your Lunch records without dropping the other records by including that condition:
isnull({table.ind_code_field}) or {table.ind_code_field}<>'LUN'
IP IP Logged
bigburb
Newbie
Newbie


Joined: 07 Oct 2009
Location: United States
Online Status: Offline
Posts: 26
Quote bigburb Replybullet Posted: 07 Oct 2009 at 2:48pm
DBlank, we tried that already, for some reason its not working. FrnhtFHI's code worked! The only problem is that it is only showing up for the first employee on my list?
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 07 Oct 2009 at 2:53pm
You need 3 formulas for FrnhtFHI option. 1 for group header to reset to 0, 1 to calcualte and 1 to display in the group footer.
Or make one formula called 'NoLunch' (or whatever):
if {table.indcode}='LUN' then 0 else {table.ttlhrs}
Then just sum that formula at any group level.
Sum({@NoLunch},group1)


Edited by DBlank - 07 Oct 2009 at 2:53pm
IP IP Logged
FrnhtGLI
Senior Member
Senior Member
Avatar

Joined: 22 May 2009
Online Status: Offline
Posts: 347
Quote FrnhtGLI Replybullet Posted: 08 Oct 2009 at 6:54am
Yeah, forgot to mention the 'Display' field.
 
The first field I gave is the 'Calculate' field. You can put this in the repeating detail section so that it evaluates the repeating records. This formula should be suppressed.
 
The second field is to 'Initialize' the running total for each group (set it to 0 and start fresh). This field should also be suppressed.
 
The third field is just a 'Display' field. This field displays the amount of the running total:
 
     global numbervar nTotalhrs;
 
This field should be in the group footer.
 
My formula may work, and I only use manual running totals out of habit, but DBlank's one formula should get the same result and be easier to execute.
IP IP Logged
bigburb
Newbie
Newbie


Joined: 07 Oct 2009
Location: United States
Online Status: Offline
Posts: 26
Quote bigburb Replybullet Posted: 08 Oct 2009 at 7:42am
OK its adding up all the indirect hours minus LUN just fine, but its not adding the direct hours in there?

What DBlank said was right, direct hours do have a value of "NULL", however making it part of the selection criteria isn't working for some reason. I don't mind have LUN on the report, just don't want it in the total.
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 08 Oct 2009 at 8:00am

Start testing out the formula by placing it on the detail row and seeing when you can get it to display the altered total correctly on each line.

If it is not doing the IF-Then correctly for each row then the sum will always be off.
Try changing the FOrmula to use 'Default Values for Nulls' in stead of 'Exceptions for NUlls'.
 
or try changing the formula to handle the NULLS issue via code. Make sure to use the NULL condition first. I have found that for some reason the if you use the NULL second it is ignored (this would be the same in your select statement that was not working).
if isnull({table.indcode}) then {table.ttlhrs} else if {table.indcode}='LUN' then 0 else {table.ttlhrs}
 
Are the NULLS null becasue the one table leaves them null or becasue it is join of tables that are NULL values?
 
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.