Ideally you can do this as part of your dataset. I am not sure what is the source of your data. If it is a database a Custom SQL can handle all this easily.
However, if you are not familiar with SQL you can create calculated fields in your dataset to do these calculations and get the results you want.