# Adhoc Calculated Field, Calculate AFTER Aggregation

Is it possible to create a field that calculates based off the sums of two other fields (i.e., after aggregation)?

I have retail prices and discounted prices, and want to create a weighted average discount, which is the sum of all discounted prices over the sum of all retail prices. This is a fundamentally different # than taking the discount rate on a row by row basis and then averaging those numbers. I can figure out how to do the latter (unweighted) calculation in domain designer, and how to calculate something simple like dividing the sum of discounted prices by the total for the column in the adhoc view creator, but not to do a division of two fields post aggregation.

Example data:

ITEM ID RETAIL DSCNTED DSCNT PCT
1 10 8 .20
2 10 5 .50
3 20 20 .00
4 100 95 .05
5 100 95 .05
6 10 4 .60
TOTAL 250 227

.092 weighted (this row discount/retail)
.233 unweighted (average of column)

14
Joined: Mar 4 2013 - 10:56am
Last seen: 8 years 2 months ago

Thanks, this is a good example of something we should be able to support in the product. We are implementing an expansion of the calculated fields capabilities in Ad Hoc for next release, and one thing I would like to enhance is the way we handle summary calculations. Currently, we just allow the user to specify a function, but for cases like this, the user should be allowed to have full control over how to calculate the summary.

A few releases back, we did make a change so that the sum of a calculated field "a/b" would be "sum(a)/sum(b)". The formula that you want for the summary is actually "1 - sum(discount)/sum(retail)".

I put your sample data into a database and tried it out. What I found was that the summary for "discount/retail" comes out as "sum(discount)/sum(retail)", but not summary for "1 - discount/retail" is really "sum(1 - discount/retail)", which is not correct.

Thanks again for bringing this up. Often we are trying to strike a balance between ease of use and power in designing the user interface. In cases like this, users have a good idea of what to do, and we should figure out how to get out of the way and let them do it!

Attachments:
AttachmentSize
5.74 KB
1069
Joined: Aug 17 2006 - 5:03pm
Last seen: 3 years 5 months ago

Interesting... did not realize that it differentiated and did something else for a/b than for 1-(a/b). Using that info, I was able to get around this by creating an intermediary calculated field for retail - discounted (call it dscntdol), then divide dscntdol by retail. I then get the answer I seek, calculated at the last possible moment.

kheaps - 9 years 4 months ago

Hello,

I've got the same issue, i want to create a measure in domain designer which is SUM(fieldA)/SUM(fieldB) in aggregation view.

I don't find find the solution. I use Jasperserver pro 4.5. Is it possible or this feature is available only for next release ?

Joined: Feb 6 2014 - 1:20am
Last seen: 8 years 9 months ago

per @bobtins above, I can verify that simply sum(a/b) is translated to sum(a) / sum(b), at least in version 5.5 - not sure if that is true in 4.5, but you might want to try it.

kheaps - 8 years 10 months ago

If I understand well, I create a new calculated field which is equals to a/b. In display tab I use this field like a measure with SUM aggregate function ?
So, if it's good the query send to MySQL will be SUM(a)/SUM(b) instead of SUM(a/b) ?
Is it clear ?
I'll check it quickly.

christophe.suzzoni - 8 years 10 months ago

That is how I understand it. It might process the sum on the java side, though. If you turn on the button that allows you to see the query being sent, you could confirm.

kheaps - 8 years 10 months ago
show 2 more...

Thanks for your reply. I'll check it. But what is the button ? Where is it ? I don't know it.

christophe.suzzoni - 8 years 10 months ago

There is a button in the ad hoc editor that can be enabled (it is disabled by default) (you must be the super user to enable it) that allows you to view the query that is being sent to the database. In the JasperReports Server 5.5 Admin Guide it's described in section 7.8 Configuring Ad Hoc, 7.8.1 Ad Hoc Query Settings. Not sure where it is in the 4.5 guide, but I'd image it is similar.

kheaps - 8 years 10 months ago