I am working on this report pulls all of our Projected Purchases grouped by Month displaying a sum (called "Monthly_Total" for the month as well as displays the Budget (called "Monthly_Budget" for the month. I also have a subreport that I use a shared currencyvar "Starting_Balance". This shared variable updates whenever the report is run.
I need to find a way to do the following calculation:
for the first month (in this case the report starts with March) it should be
"Starting_Balance" + "Monthly_Total") - "Monthly_Budget" = "New_Balance"
each subsequent Month would use the formula
("New_Balance" + "Monthly_Total") - "Monthly_Budget"
I can get it to work for the first group footer, but with then each month after that is referencing back to the "Starting_Balance" rather than the "New_Balance"
any ideas?
Try using a flag variable that says whether or not the first month has been reported yet. I originally though of using New_Balance as 0, but that could potentially happen naturally.
So, something like
Initializer in the report header:
WhilePrintingRecords;
Global BooleanVar First_Month_Done := false; // have we printed the first month?
""; // print nothing on screen
Monthly formula
WhilePrintingRecords; // Optional when using shared variables
Global BooleanVar First_Month_Done;
Global CurrencyVar New_Balance; //or whatever it is
Shared CurrencyVar Starting_Balance;
// Assuming "Monthly_Total" and "Monthly_Budget" are formulas, not variables
If First_Month_Done
Then New_Balance := New_Balance + {#Monthly_Total} - {#Monthly_Budget}
Else New_Balance := Starting_Balance + {#Monthly_Total} - {#Monthly_Budget};
First_Month_Done := true;
New_Balance
Related
I've been working on a report made by someone else which uses the StDev function. I thought it would be simple enough, but the data can contain multiple values for each record, so there are if statements used to determine which value to take from each record. I've exported a table to Excel which contains one value per record (the one that should be used in the StDev), and then calculated the SD there to provide a check.
The report and Excel are giving me very different values :(
So, I'm going back to the report and using some additional formula fields to calculate the SD longhand to act as a kind of deciding vote (fingers crossed it doesn't produce a third set of values...).
I've worked out the syntax errors, but am still getting a run-time error - "Division by zero" which then highlights the section of code indicated below...
My formula fields are:
{#StDevArrayPopulate} - in the details section
NumberVar Array varStDevArray;
NumberVar varStDevArrayCount;
varStDevArray [varStDevArrayCount] := {ValueToSummarise};
varStDevArrayCount := varStDevArrayCount + 1;
{#StDevArrayCalculate} - in the group footer
NumberVar Array varStDevArray;
NumberVar varCounter :=1;
NumberVar varMean := 0;
NumberVar varStDev := 0;
NumberVar varStDevArrayCount;
// START OF MEAN
// Sum of all of the values in the array
for varCounter:= 1 to varStDevArrayCount step 1 do
(
varMean := varMean + varStDevArray [varStDevArrayCount];
);
// Divide by the total number of values in the array
varMean := varMean / varStDevArrayCount; // !! This is the line that highlights after the error message !!
// END OF MEAN
// START OF STANDARD DEVIATION
// Subtract the mean from each value in the array and square the result
for varCounter := 1 to varStDevArrayCount step 1 do
(
varStDevArray[varStDevArrayCount] := (varStDevArray [varStDevArrayCount] - varMean) * (varStDevArray [varStDevArrayCount] - varMean);
);
// Sum of all of the values in the array
for varCounter:= 1 to varStDevArrayCount step 1 do
(
varStDev := varStDev + varStDevArray [varStDevArrayCount];
);
// Divide by the total number of values in the array
varStDev := varStDev / varStDevArrayCount;
// Square root of mean of differences
varStDev := Sqr(varStDev)
// END OF STANDARD DEVIATION
{#StDevArrayCalculate} - in the group footer
NumberVar varMean;
NumberVar varStDev;
"The mean is " & varMean & ", and the standard deviation is " & varStDev & "."
I've tried using a Running Total field but that was giving errors as it need to be count the first record before the Populate formula field ran. I've also tried adding a fourth field to the header, which initialises the varStDevArrayCount as 1.
Does anyone have any suggestions?
Looks like there are no detail records hence you are getting zero in array... I don't think initializing with 1 will make any difference instead make a small change like below and check.
if varStDevArrayCount=0
then
varMean :=varMean
else
varMean := varMean / varStDevArrayCount;
I would advice you to just check records in detail and then debug
I have difficulty in getting a maxiumu value from a multiple summarized values in group section. For example, I summarized budget, commitment, cost and forecast total in a report group section, and now in group footer I need pick a maximum value of the four totals. How should I do it?
Edit............................................................
1) Sum ({#Total Contracted Value}, {JCM_MASTER__COST_CODE.Cost_Code}),
2) Sum ({#Current Budget}, {JCM_MASTER__COST_CODE.Cost_Code})
3) Sum ({#Costs to Date}, {JCM_MASTER__COST_CODE.Cost_Code})
4) Sum ({#Projected Final PM Input}, {JCM_MASTER__COST_CODE.Cost_Code})
and I need to find the highest value within the four totals in group footer section
You need to create two formula field for that 1st for variable initialization and 2nd for condition which value is bigger than other.
1: Create a first formula field and write down the following code. Give the name of that formula field to Initialization.
WhilePrintingRecords;
numbervar dMax := 0;
Then place that formula field in the Report Header section.
2: Now, create another formula field and write down following code in that formula field.
WhilePrintingRecords;
//dont initialize the 0 in this variable we have already initialized in 1st formula field
numbervar dMax;
//Following condition will check that current sum of that field is greater than
//values stored dMax variable. If the condition is satisfied then value will be initialized
//In dMax variable.
if (IsNull(Sum({TableName.FieldName}, {TableName.GroupField})) = False AND
Sum({TableName.FieldName}, {TableName.GroupField}) > dMax)
dMax:=Sum({TableName.FieldName}, {TableName.GroupField});
dMax;
Now, put this formula field where the summarized value is calculated. for example in group footer.
UPDATE:
If you want to find out the max value withing the four group total. then write down the following code in your formula field and put it in the group footer.
WhilePrintingRecords;
numbervar dMax1:=0;
if (Sum ({#Total Contracted Value}, {JCM_MASTER__COST_CODE.Cost_Code})) > (Sum ({#Current Budget}, {JCM_MASTER__COST_CODE.Cost_Code}))
dMax1:=Sum ({#Total Contracted Value}, {JCM_MASTER__COST_CODE.Cost_Code});
else
dMax1:=Sum ({#Current Budget}, {JCM_MASTER__COST_CODE.Cost_Code});
numbervar dMax2:=0;
if (Sum ({#Costs to Date}, {JCM_MASTER__COST_CODE.Cost_Code}) ) > (Sum ({#Projected Final PM Input}, {JCM_MASTER__COST_CODE.Cost_Code}))
dMax2:=Sum ({#Costs to Date}, {JCM_MASTER__COST_CODE.Cost_Code});
else
dMax2:=Sum ({#Projected Final PM Input}, {JCM_MASTER__COST_CODE.Cost_Code});
if (dMax1 < dMax2)
dMax1 := dMax2;
dMax1;
Try using the arrays...
Store all values in an array and in group footer just take the maximum of the array.
When you summarize the value at the same time store the values in array and retrive maximum from the array. Place below formula after summarizied data in group
//#Storedatainarray
Shared StringVar Array x;
x:=x+totext(Sum ({field}, {#group}));
Now create one more formula #display
EvaluateAfter(#Storedatainarray);
Whileprintingrecords;
Shared StringVar Array x;
Maximum(x);
Let me know how it goes?
Edit....................................
Create a formula #Display and write below code in group footer:
Shared StringVar Array x;
x[1]:= Sum ({#Total Contracted Value}, {JCM_MASTER__COST_CODE.Cost_Code});
x[2]:= Sum ({#Current Budget}, {JCM_MASTER__COST_CODE.Cost_Code});
x[3]:= Sum ({#Costs to Date}, {JCM_MASTER__COST_CODE.Cost_Code});
x[4]:= Sum ({#Projected Final PM Input}, {JCM_MASTER__COST_CODE.Cost_Code});
Maximum(x);
I have created a crystal report which groups products based on order number.
I have created a formula which displays the text 'Partially Completed' or 'Fully Completed' based on whether a field on each product called 'Difference' equals 0 or not. If 'Difference' is 0 then 'Fully Completed' is displayed and vice-versa.
What I need to do is to display the corresponding message for the overall order number (i.e. if any of the products for an order have a difference != 0 then display 'Partially Completed'. If all the products have a difference == 0 for an order then display 'Fully Completed'.
This is the code I have attempted so far:
StringVar ouputText;
if {AD_999_SB_Fulfillment__Summary.FulfillmentPicking.Difference} = 0 then
ouputText := 'Full'
else
ouputText := 'Partial';
ouputText;
I assume I need some kind of for-loop to loop through all of the products for an order and check to see if any of the orders have a difference != 0 and then update the message based on if all the products have a difference == 0 or one of the products breaks the condition by having a difference != 0.
Looping is a lenghty process instead you group your report according to the "Order" then place the product in detail part. This will automatically loop entire products in a Order.
Create formula with name reset and place it in group header. Code is below.
Shared stringVar array y;
y:=" ";
join(y);
Create a formula and place it in detail section. This will take the result and store it in array for future use. Use below code.
StringVar ouputText;
Shared stringVar array y;
if {AD_999_SB_Fulfillment__Summary.FulfillmentPicking.Difference} = 0 then
y:= y+'Full'
else
y:= y+'Partial';
if {AD_999_SB_Fulfillment__Summary.FulfillmentPicking.Difference} = 0 then
ouputText := 'Full'
else
ouputText := 'Partial';
ouputText;
In group footer create another formula to display whether result is full or partial for order.
Shared stringVar array y;
if "Partial" in y
Then "partial"
else "Full";
New to CR and use CR v10 and SQL Server 2000.
For the first record i.e Beginning Balance , the calculation is sum(field) from the input date, which I have calculated in SP as BegDateSum
But for the rest of the records under a group, the calculation should be previous(balance)+IN+OUT
Sample has been given:
Date Doc Descrip IN OUT Balance
Group Header-------- Beginning Balance-------------- 50 <---- sum(field) from my inputdate
3/2/2012 A -1 0 49 <-- (50+(-1)+0)
4/2/2012 B -2 0 47 <-- (49+(-2)+0)
5/2/2012 C 0 3 50
6/2/2012 D -2 3 51
How do I achieve this?
I am not sure whether to use running total, in case I have to how to do it.
A running total field won't work in this case, they are designed to add up (or count, or average, etc) one field and give you the sub-totals automatically. But, we can do some custom functions that will give the results you need. Assuming that your initial 50 is a static value, you would set a variable to that amount, and then add the IN and OUT values as you go along (printing that result of that).
First, initialize the value in the report header with a formula like:
WhilePrintingRecords;
Global NumberVar Balance;
Balance := 50;
""; //print nothing on the screen
Then, the formula to calculate and show the new balance, in the bar where the data is:
WhilePrintingRecords;
Global NumberVar Balance;
Balance := Balance + {tableName.IN} + {tableName.OUT};
The last line both calculates the new value, and tells what the result of the formula should be.
If the "50" is calculated somehow, then that will have to be done before the formula that calculates the new balance. If it is based off of the first record read in, you'll want to use a formula that includes If PreviousIsNull({tableName.Balance}) Then ..., that is usually a good indicator of the first record in the data set (unless that field can be null).
I have a Crystal Report v 9.2.2 and I have three summaries at the end of each group; count of IEP students, count of non-IEP students, and total number of students. For an example, for one group (a class) I have 25 students. 4 Students are IEP, 21 are non-IEP. So I tried to create a formula to calculate the percentages, but the percents are wrong.
sum({#IEP}) % count({Mytable.student_id})
Manually, the math says 4 / 25 * 100 = 16, but when I use the formula above I get 11
I don't think it's a formatting problem.
Check reset value in "Create running Total Field" for the count.
Or
reset the count in the formula.
Your math is right, just do the same thing in the code/formula. The following works for a group based on {YourGroupField} and gives the percentage to one decimal point:
local numbervar IEPstudents := sum({#IEP},{YourGroupField}) //get number of IEP students in group
local numbervar TotalStudents := count({Mytable.student_id},{YourGroupField}) //get total students in group
local numbervar ThePercent := (IEPstudents / TotalStudents) * 100;
totext(ThePercent,1) + '%'
Alternatively, you can keep the formula numeric and add the percent symbol and formatting in the field's format editor.