Summing variable used in calculation after report is generated - jasper-reports

I am creating a report where a query returns a list of values which are being used in my detail band. A new value shows for each record like it should. I'm trying to sum up these values and then use them in a calculation on the same line. Heres an example:
Color Fruit # of fruit Percent of total
--------------------------------------------------------
red apple 4 57%
green apple 3 43%
Total apples 7
The column being summed is the # of fruit or 3rd column. You can see the total at the end. Then the percent of total is the # of fruit for that row divided by the total. So for the first row it's 4 / 7 which equals 57%.
The problem is that I can't get the total until all the fruit has been summed. But I need that total to be used on each row for the Percent of total column.
How do I create a variable to do this?

Related

Crystal Reports: Need to get percentages from Running Total distinct counts

I have a pre-existing Crystal Report that an end-user wants me to add percentages to. I'm running into an issue because the numbers they want percentages of are Running Total distinct count fields with their own formulas.
At the end of a report, I have several Running Total fields that are distinct counts for cars, one field for a count of each color car. The sum of all of these fields equals a grant total of all cars. So it looks something like this:
Black: 5
Red: 10
Green: 5
Yellow: 20
Total: 40
Each of the car color Running Total distinct count fields has a formula behind it to give us the number for that color, and then the "Total" field is just the distinct count of all cars - no formula. I need to automatically calculate and add the percentages of each of the car color rows towards the total count. It would look like this:
Black: 5 12.5%
Red: 10 25%
Green: 5 12.5%
Yellow: 20 50%
Total: 40
At this point I've tried using the existing formulas used to get the car color counts to next inside a new formula that would divide it by the car total, but I can't seem to get the result I want.
After working with this for some additional time, I was able to find the solution. I created a new formula for each of the car colors and was able to divide using the Running Total distinct count field for each, and then times by 100 to move the percentage 2 decimal places. So an example of a formula was:
{BLACK CARS}/distinctcount({Command.CARS}) * 100
I created one of these for each car color and placed them next to the distinct count fields.

Pick group totals, grand totals and normal summarization in Tableau

I am trying to calculate average of 2 columns in another column in Tableau but difficult part is grand total is not getting average instead it is the sum of 3rd calculated field.
A B Calculated field
10 5 2
6 3 2
T 16 8 4 (Here I should get 2 instead it is taking sum of column)
Here I am unable to write separate formula for row totals and grand totals, Only one formula (Calculated Field) is allowed and when I am dragging on sheet it is by default aggregating to sum.
Note: I am expert in Crystal and BO but beginner in Tableau.
Update
Code used for LoD
{FIXED [Product Category]: AVG([Sales])}
Below image is what I got after implementation I have tried with 2 columns but the result is same if I use only one column (I am trying to get the average of sales)
You are almost there - the Grand Total by default does a SUM function you just have use the Total All Using --> Average option.
Output : Level wise SUM(Profit) later averaged across columns and rows. (Show Column Grand Total & Show Row Grand Total active)
Update: Answering the question below. To get the Row-wise avg (which is Cat1-vag in this case) you could just drop the measure and change it to AVG(). Since you needed in a Calculated Field you could use a Simple FIXED LOD. You can also uncheck aggregated measures from Analysis dropdown and have no Dimension in column or row like unlike what this example shows and still get three different averages. Cheers.
{FIXED [Cat1]:AVG([Profit])}
Check out this very smart work around from Joe Mako.
https://community.tableau.com/thread/112791
create a calc field like:
IF FIRST()==0 THEN
WINDOW_AVG(SUM([Sales]),0,IIF(FIRST()==0,LAST(),0))
END
duplicate your Category field
place "Category (copy)" on the level of detail
set the compute using for the calc field pill to use "Category (copy)"
The window function in the calculated field only takes into account what's in the view, and aggregate based on those number.

Tableau calculate percentage from a value in the table

I'm trying to calculate the percentage in Tableau from a value in a table.
For example. I have a table that contains the following
Name Value
a 10
b 20
c 30
Ref 100
How can I have the percentages calculated based on the Ref value like for example a/Ref
Name Value Percentage (x/Ref)
a 10 10%
b 20 20%
c 30 30%
Thanks
You can either
use tableaus built in functionality:
Right click the measure pill and select "quick table calculation -> Percentage of total"
2.Use calculated fields.
Create a calculated field(Analysis->Create Calculated Field) called reference. As the value enter the integer you want.
Create a second calculated field call it percentage. Here enter the mathematical formula to get a percentage using the reference calculated field as your total.
So something like:
[Value] / [refrence] *100
You don't need the reference calc field, you could just hard code the value into the calculation.
Drag the percentage calculated field into the row shelf.

Running Total To Exclude Supressed Value in Crystal Reports

I wish to sum the data on display in my crystal report and not the suppressed data.
I have used the field editor and suppressed tool in the repeated area and I have the below formula for the rows to be suppressed:
{fieldname} = previous {fieldname}
I have read that if I use the running total and enter the opposite formula to subtotal then the subtotal will sum only the values displayed, e.g.
{fieldnmae} <> previous field-name.
However after doing this the first row is now totaling as the previous will be null. The report is sorted so the first row will always be a true value. Is there a formula I can use to display this to sub total? E.g. if Previous {filedname} isnull or {fieldname} <> previous field-name = sub total.
Or is there another solutions to this?
The data looks something like
Row No Value
0 20.00
0 20.00
1 25.00
2 10.00
3 7.50
4 5.00
4 5.00
If row no repeated than do no sub total or display
Many Thanks !!!!!
Chris
Easy way would be don't manipulate on Value column instead manipulate Row No column.
Create a running total and sum when Row No changes which will give correct results

Any other way to count group records in crystal reports?

I want to count the total no of records found under a group and according to that i need to assign reduction percentages.
In Detail,
if a user got less than 3 products he ll get 10%
if a user got 4-10 products he ll get 15%
if he got 10-20 products he ll get 20% as deduction
For example consider the following as a crystal report:
User 1
Product Deduction
Apple 15 %
Orange 15 %
Lemon 15 %
Strawberry 15 %
Grapes 15 %
Here i made a count of records in this group using running total and worte a formula. Depends on the count its ll make the deductions
But the problem in getting is, I'm getting the following output...
User 1
Product Deduction
Apple 10 % // Since count is 1
Orange 10 % // Since count is 2
Lemon 10 % // Since count is 3
Strawberry 15 % // Since count is 4
Grapes 15 % // Since count is 5
I wanna make all products deduction as 15 % if the total no of records are 4-15. Here the total no of products is 5 its in the range of 4-15 so all the product should have the deduction as 15 %.
Please help me out to solve this?
You don't need a running-total field (RT) for a simple count; use a summary field instead. The summary field can be used in the group's header and footer (a RT can only be used in the footer).
Select a field (preferably a unique field), then select Insert | Summary Field... Choose Count or Distinct Count from the picklist.
You can use this summary in a formula as well:
//{#discount}
//Assumes there is a group on {table.fruit_name}):
SELECT Count({table.key_field}, {table.fruit_name})
CASE 1 to 3: .1
CASE 4 to 5: .15
DEFAULT: 0