Sum Number Column Values

What I’m trying to do: I have an app_tables with a column named ‘payment_amount’. The table data is displayed on a Form in a DataGrid component. In another section of the Form, I have a label named ‘label_total’.

I would like to sum the values in the ‘payment_amount’ column then display the output on the ‘lable_total’ label. Furthermore, I would like to include the following calculations (I will make additional labels for the additional calculations):

  1. Total sum of all values
  2. Total sum of all values ‘this month’
  3. Total sum of all values ‘last month’
  4. Total sum of all values ‘this month last year’

Any direction/advice is greatly appreciated. Thank you.

Adam

https://anvil.works/build#clone:GBCVJBIINSA53MQ3=5CJ3UZ6XI6YD6IYNADZUCWLA

Something like this should work, assuming that the items in your datagrid have a column called time:

from datetime import datetime
this_month = datetime.now().month
label_total.text = str(sum(item['payment_amount'] 
                           for item in self.repeating_panel.items]))
label_total_this_month.text = str(sum(item['payment_amount'] 
                           for item in self.repeating_panel.items]
                           if item['time'].month == this_month))
3 Likes

@stefano.menci Thank you so much! Worked like a charm :slight_smile:

2 Likes