Referencing first record in a group in crystal reports - crystal-reports

In my report, in group footer, I want to add the sum on a column from all records in the group except the first one. How can I do that?
I tried Insert - Subtotal but it adds all records. If I can access the first record from each group, I can subtract it from the sum.

Let's say you're grouping your report by {Table.GroupID} and want to get the total per group, excluding the first record in the group, for {Table.NumericField}.
You can do this by creating a running total where:
Field to summarize: Table.NumericField; Type of summary: Sum
Set to evaluate with a formula: previous({Table.GroupID})={Table.GroupID}
Reset on change of group
The second step ensures that the group total is updated for every record except the first one in the group and can be read as "Only evaluate this running total when the previous record's GroupID is the same as the current record's GroupID.

Related

Suppress records in grouped section between first two and last records in Crystal Reports

First, I am using Crystal Reports 2011. I need to suppress records in a group that fall between the first two records and the last record. In other words I only want to see record number 1 and 2 and the last record. I can easily suppress records that are not the first two, but getting the last record to not be suppressed has me at a standstill. I am creating a report where we want to only show the first, last and next-to-last time we purchased items. Some items have been purchased many, many times. I have the report grouped by part number, then all the dates we purchased the part (and other data) are in the group details. I have tried using a running total count, where I simply suppress details that are not 1 or 2, but the last record count varies depending on how many times we have purchased the item, so I don't know what the last count number will be for each group to use it in the suppress formula. I have also tried with declaring a variable and increment the variable for each record, but end up with the same problem as using running total count. Anyone have a suggestion?
To clarify my question a little further, I am sorting the group by date, most recent date first. So record count 1 is the last time we purchased and record count 2 is the next-to-last time we purchased. So by using record count I can suppress records that are not 1 or 2. The problem comes when trying to figure out how to not suppress the last record (which is the first time we purchased). Below is a link to a pic of what my report looks like with nothing suppressed. For this part I would want to suppress all records except 1, 2 and 5. But again, the last count number will change per part number depending on how may times we have purchased the part.
Help is greatly appreciated!!!!
Report cropped image
Using Crystal syntax you can test on OnLastRecord which returns true if you are on the last record or false if not. Simply suppress accordingly.
EDIT:
As pointed out by jmears, the above won't work for multiple groups of differing products.
Instead you can add a running total field to count each record within the group, reset on change of group. Then suppress the details based on the running total not being 1 or 2 (for the first two records) and not equal to the total count of records for the group.
Here's an example suppression formula, where {#rt} is the running total count of records. It uses the Count function to return the total count for the group
not({#rt} = 1 or {#rt} = 2 or {#rt} = Count ({fieldToCount}, {GroupField}))

Crystal reports - Can't filter on custom formula number field

Crystal reports don't let me use a custom count formula field to filter which transactions to show in a manager report.
I'm creating a Crystal report that team leaders are supposed to take out to see on how many occasions their employees have reported in sick. A record is only supposed to show if that person has reported in sick 6 or more times the last 12 months.
The report shows a record (a page) for each employee belonging to the managers organisational unit. Below the employee information is a subreport where I show the transactions from the salary/time system. Using select expert, I have filtered out the transactions that is supposed to show. I have then created a database field that count which day was 12 months back from today, and filtered so that only the transactions falling into this period shows.
My last problem is that I only want to show the record that has a minimum of 6 such transactions during the period. I created a formula field named #Antal ("amount" in Swedish) that simply counts the distinct number of dates in the "from"-date for the salary transactions I'm showing (since a change of law 2019-01-01 we needed to create a new transaction type, so some of the occasions after 2019 may have two transactions referring to one sick leave, thus I'm counting the first day of the period instead), DistinctCount ({P_LSTAT.P_SXXX06})
Now, the subreport has a new column with Antal (amount) that counts the amount of the desired salary transaction. I then try to use the selection formula to only show records where {#Antal} >= 6 but I get the following error:
This formula cannot be used because it must be evaluated later
Is there any other (better) way of doing this, or am I simply missing something?
For your selection based on {#Antal} >= 6 you need to use the group selection formula, not the record selection formula. Record selection is used to select records which meet the criteria before reading in the data. Group selection is used to filter out entire groups of records based on summarised values, after the records have been read in and the summaries calculated - which sounds like exactly what you need here.
The value of a Formula Field is out of scope when the Select Expert is evaluated.
There is no process for calculating the value of a Formula Field before it is printed within the section of the report it is placed. The Select Expert is evaluated prior to any section of the report being printed, so at this time all Formula Fields are effectively Nothing.

Jaspersoft Studio subtotal from amount shown on group header

I have data that is "filtered" through three groups.
First group is defined by departments, second is defined by course id and third is for individual class session id (i.e. room and schedule). Also the source query is giving me the list of all students for each class session, and this causes that the other columns get repeated for every student (i.e. credits per course, total enrollment per session, total students dropped per session, total students auditing per session, etc).
When you drop any of those query counts on the report group's footer the sum is not correct. For example if we have 10 students for a session our sum is 100 enrollment. I want to count 10 enrolled students only once.
The amounts are not shown multiple times because I placed the fields on the group header. So on every group break the row of data is displayed only once.
I believe I need a custom running total and not the default created when you drag and drop the field on the group's footer.
I got something working.
On the report Outline I right clicked on 'Variables' and 'Create Variable'.
For example for the audit count I did the following:
Named my variable 'sum_au'
Value Class Name set to java.math.BigDecimal
Calculation set to Sum
Expression set to the query field for the audit total 'AU': $F{AU}
Increment Type set to [Group]GroupBy_CRN
Reset Type set to [Group]GroupBy_CrseNum
All other fields left untouched (default values)
Once this variable is created you can drag and drop it to the CrseNum Group Footer and it will add the values only once per break on the course session id (CRN).
I will reproduce this for the other values to see if this solution is consistent...

Check last record of each group

My formula needs to detect when it reaches the last record in a group before the group changes. I tried using both OnLastRecord and creating a running total field (which resets on change of group) to do this. In both cases it only checks the very last record on the very last page of the report.
My ultimate goal is that I want my report to check if a number field, {#Calc} on the last record is < 0. If so output "Outsanding:" &{#Calc}*-1.
I have figured out my problem. I am now using:
{grouped field} <> next({grouped field})
which gives the value of last record for each group value.
Have you considered using the Group Footer for the group in question?
When you reach that particular footer, the active record will be the last one in the group. So any calculations you perform, or any fields you choose to print, will be based on the final record read in that group.
Note: If you're suppressing some records in the group, this might not work. In which case, you can instead use Shared Variables to store the latest valid value for Calc.
Then by the time you reach the footer, you can check to see if the variable is less than zero and display your Outstanding field based on the result.

Print field from first record in a group in the group footer

I have a series of transactions that contain a receive and return date and are grouped. In the group footer, I want to print the receive date of the first record in the group and the return date of the last record in the group. I created a formula to populate only when it is the first record in the group. But if the group contains more than one record, the formula field prints out blank. If there is only one record in the group, it prints OK. I'm using SAP Business Object Crystal Reports 2013.
There are a few ways to handle this. If you were sorting by Date, (which I've just learned you aren't) you could use a Summary to find the Minimum value. You'd place it in the relevant Group Footer and it should reset each time.
But since you're not sorting them by Date, I'd try setting up a Running Total that counts the number of records in the current Group (and resets on change of group.) Then set up a Shared Variable in a Formula that gets set every time the Group Count resets to 1. Finally, put another Formula in the Group Footer which prints out the Shared Variable.
Done correctly each Group Footer will display the first date value in that Group.