I think you have aproblem with your CANTIDAD, in your first table, you get two rows with sum(CANTIDAD) is 2 on each row. I think it should be only one (looking at the records, e.g. field CANTIDAD with the selection from your example xls.)
In the second table, the sum(CANTIDAD) is not correctly taken into account either, I am still looking into that, too.
That'sbecause it has twopurchase prices,if we compare thefirst frame andthe second picturewe see the difference, myintention is toscore asprecio.sum(quantity) is wellis 2.
If you look atthe third columnof the second tablethat is thecorrect price,whichshould leadin the fourth column, you couldleave itbut I have totell if it isa chargeor creditand alsoifI haveselectedasimpleboxaño_hasta=9999sales quantitiesvary