Calculating an average

In the picture above I’m trying to get the average price /kg. Different transactions can be registered. What I am trying to implement is the total price paid/kg divided by the number of transactions.

This is the error I’m getting.

Screenshot 2024-02-12 094805

Hi Brenden,

There are two errors in that calculation:

There is only one division rule in the conversions of ‘ugx’:

= ‘kg’ / ‘ugx’

That is the rule you are using when you say:

. ‘Total Price paid per kg’ as ‘kg’

But the problem is that ‘Total Price paid per kg’ is itself not a number of the unit ‘kg’, but of ‘UGX’, so that is not correct.

The other problem is with the . ‘Transactions’, which is not a number, but a collection. You should add a property

‘Total ugx’: number ‘ugx’ = count . ‘Transactions’
, for instance. And divide by that.

But you should check your calculations, because I don’t think they do what you want them to do:

I would

per Transaction:

  • have a price (in ugx/kg)
  • have a weight (in kg)
  • calculate an amount ( price * weight ) in ugx

And then in total:

  • calculate a total amount = sum . ‘Transactions’. ‘Amount’ in ugx
  • calculate a total weight = sum . ‘Transactions’ . ‘Weight’ in kg
  • calculate the average price = division ( total amount as ‘ugx’ , total weight ) in ugx/kg

One other thing to take into account: the calculations are not considering decimals at all, all numbers are considered integer, so:

4,35 ugx * 2,50 kg will in reality be calculated as:

435 * 250 = 108750

You should give the output the right unit, with 4 decimals, in this case.
The decimals are used by the gui to show the number with the comma in the right place

1 Like