Calculation of Previous field - crystal-reports

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).

Related

Manipulating last two rows if there's data based on a Cut date

This question is a slightly varied version of this one...
Now I'm using Measures instead of Calculated columns and the date is static instead of having it based on a dropdown list.
Here's the Power BI test .pbix file:
https://drive.google.com/open?id=1OG7keqhdvDUDYkFQFMHyxcpi9Zi6Pn3d
This printscreen describes what I'm trying to accomplish:
Basically the date in P6 Update table is used as a cut date and will be fixed\static. It's imported from an Excel sheet where the user can customize it however they want.
Here's what should happen when a matching row in Test data table is found for P6 Update date:
column Earned Daily - must have its value summed with the next row if there's one;
column Earned Cum - must grab the next row's value;
all the previous rows should remain intact, that is, their values won't change;
all subsequent rows must have their values assigned 0.
So for example:
If P6 Update is 1-May-2018, this is the expected result:
1-May 7,498 52,106
2-May 0 0
If P6 Update is 30-Apr-2018, this is the expected result:
30-Apr 13,173 50,699
1-May 0 0
2-May 0 0
If P6 Update is 29-Apr-2018, this is the expected result:
29-Apr 11,906 44,608
30-Apr 0 0
1-May 0 0
2-May 0 0
and so on...
Hope this makes sense.
This is easier in Excel, but trying to do this in Power BI is making me go nuts.
I will ignore previously asked related questions and start from scratch.
First, create a measure:
Current Earn =
CALCULATE (
SUM( 'Test data'[Value]),
'Test data'[Act Rem] = "Actual Units",
'Test data'[Type] = "Current"
)
This measure will be used in other measures, to save you from typing all these conditions ("Actual Units" and "Current") again and again. It's a great practice to re-use measures in other measures - saves work, makes code cleaner and easier to refactor.
Create another measure:
Cut Date = SELECTEDVALUE('P6 Update'[Date])
We will use this measure whenever we need a cut off date. Please note that it does not have to be hard-coded - if P6 table contains a list of dates, you can create a pull-down slicer from the dates, and can choose the cut-off date dynamically. The formula will work properly.
Create third measure:
Next Earn =
VAR Cut_Date = [Cut Date]
VAR Current_Date = MAX ( 'Test data'[Date] )
VAR Next_Date = Current_Date + 1
VAR Current_Earn = [Current Earn]
VAR Next_Earn = CALCULATE ( [Current Earn], 'Test data'[Date] = Next_Date )
RETURN
SWITCH (
TRUE,
Current_Date < Cut_Date, Current_Earn,
Current_Date = Cut_Date, Current_Earn + Next_Earn,
BLANK ()
)
I am not sure if "Next Earn" is a good name for it, hopefully you will find a more intuitive name. The way it works: we save all necessary inputs into variables, and then use SWITCH function to define the results. Hopefully it's self-explanatory. (Note: if you need 0 above Cut Date, replace BLANK() with 0).
Finally, we define a measure for cumulative earn. It does not require any special logic, because previous measure takes care of it properly:
Cum Earn =
VAR Current_Date = MAX('Test data'[Date])
RETURN
CALCULATE(
[Next Earn],
FILTER(ALL('Test data'[Date]), 'Test data'[Date] <= Current_Date))
Result:

Display number of rows on printout

If an Order contains 2 lines or more, my report sums it up and prints the whole quantity of items. It should print whichever line I have chosen:
WhilePrintingRecords;
NumberVar ItemCount := ItemCount + 1;
ToText(ItemCount, "0") & "/"
& ToText(Count({rpt_PackingSlip.LabelQTY}, {rpt_PackingSlip.WorkOrderNo}),0,"")
For example, the order below contains a chair called Buzz, but the order contains 3 lines since each has different fabric. The total order quantity is 5:
If I print, the label count shows 1 out of 4 - which automatically sums the chair. If I select the first line, expected output is Buzz 1/2.. and 2/2. Currently output displays Buzz 1/4.. 2/4.. 3/4.. 4/4.. even if I just clicked 1st line. How can I achieve this result?
You'll want to reset the counter on each group. Just create a second formula and drop it in the group header:
global numbervar ItemCount := 0;

Create Cumulative Change Chart in Tableau

I have a bunch of daily change % data. I would like to calculate cumulative change, which should just be (1+change)*previous day in a chart in Tableau.
Seems simple enough right? I can do it in a few seconds in Excel, but I've tried for hours to get it to work in Tableau and cannot do it.
My thought was that I can create a column that is (1+daily change%), then try to do a compound product. However, I can't seem to get it to work.
I can't attach any files here so I pasted the data, along with a column that is "cum change", which is what I would like the calculation to be.
Thank you much in advance!
Date Daily Change Cum Change
4/1/2015 0.47% 1
4/2/2015 0.56% 1.0056
4/3/2015 -0.72% 0.99835968
4/6/2015 -0.56% 0.992768866
4/7/2015 -0.80% 0.984826715
4/8/2015 0.44% 0.989159952
4/9/2015 -0.66% 0.982631497
4/10/2015 0.99% 0.992359549
4/13/2015 0.92% 1.001489256
4/14/2015 0.73% 1.008800128
4/15/2015 0.95% 1.018383729
4/16/2015 0.42% 1.022660941
4/17/2015 0.52% 1.027978778
4/20/2015 0.02% 1.028184373
4/21/2015 0.56% 1.033942206
4/22/2015 0.35% 1.037561004
4/23/2015 -0.34% 1.034033296
4/24/2015 0.18% 1.035894556
4/27/2015 0.61% 1.042213513
4/28/2015 0.46% 1.047007695
4/29/2015 0.94% 1.056849568
Create a calculated field:
IF INDEX() = 1
THEN 1
ELSE
(1 + AVG([Daily Change])) * PREVIOUS_VALUE(1)
END
The condition checking to see if it's the first row of the partition (INDEX() = 1) is necessary to ensure that the first value of the field is a 1. After that, you can just use the self-referential PREVIOUS_VALUE() to get the previous value of this same calculation.

Sumifs in Crystal

I'm working on a database that basically looks like this (in its simplest form):
{Phase} {Code} {Qty}
Example:
{Qty} for {Phase}="R" and {Code}="Nat" = 0
{Qty} for {Phase}="F" and {Code}="Nat" = 5
{Qty} for {Phase}="R" and {Code}="Int" = 10
{Qty} for {Phase}="F" and {Code}="Int" = 15
I am trying to get a result to show me the Qty for phase "R" and Code "Nat" (where R is <> 0) otherwise give me the qty for phase "F". So for the above example I would get an answer = 5 for Nat (because qty for phase R is 0) and an answer of 10 where the code is Int (because qty for phase R <> 0)
I have used three formula fields to do this:
1: if ({PHASE}="F" and {CODE}="NAT") then {QTY} else 0
2: if ({PHASE}="R" and {Code}="NAT") then {QTY} else 0
3: if {2} = 0 then {1} else {2}
Formula fields 1 & 2 come up with the correct amounts. However formula field {3} returns both phases. For example Code "Int" Phase "R" shows as qty = 25 instead of qty = 10.
How do I get around this?
You need to group by {table.code} because this is not a one-row calculation, but needs to be a calculation over each code for 2+ phases (meaning 2+ rows of data).
Create a formula with two variables that will store the values of each phase, F and R. This formula needs to go in the Details section of the report.
whileprintingrecords;
numbervar Fqty;
numbervar Rqty;
if {table.phase}="F" then Fqty:={table.qty}
else Rqty:={table.qty};
Now, in the group footer, you can reference both quantity values via the variables.
whileprintingrecords;
numbervar Fqty;
numbervar Rqty;
if Fqty=0 then Rqty else Fqty
And you're done. Don't forget to reset the two variables in the group header so you don't carry the quantity over between different codes.

Crystal summary and percents

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.