Im using drupal views. I have created a view with and inside the view i need to perform some calculations on each field inside the view. for example I have a beginning balance, payment, ending balance. I need to retain the ending balance in the first row and replace beggining balance on the next row. Thanks everyone in advance


  • Have you looked at Views Calc or Views Aggregator Plus? – Felix Eve Oct 19 '17 at 23:49
  • Yes i have played with those two modules. i have not been able to keep the ending balance to be used in the next calculation. – EdMedina Oct 20 '17 at 13:34

There are two approaches to balance calculation like this: 1) on-the-fly, or 2) maintaining the values separately in the database. It sounds like you're trying to build these values on-the-fly (every time the View is rendered, they are recalculated dynamically). This is the simpler approach of the two, but you may run into performance issues if you are trying to render a large amount of results. Approach #2 is essentially a form of caching for the balance - and thus comes with all the complexity of caching (ie: invalidating certain values if one in the past is changed, etc).

Assuming you want to stick with approach #1, there are two sub-approaches - aka two possible places to do the balance calculation: 1a) in the SQL query, or 1b) in PHP.

1a: It's possible to build these "balance" fields in the SQL query itself using a sub-query field that adds up all the payments up to the date of the current payment. In order to achieve this in Views, you would need to create a custom Views field handler that adds the subquery field.

1b: Using PHP and one of the hook_X_alter() functions that Views provides, it's possible to alter the values of fields AFTER the database query runs, and BEFORE it gets rendered on the page. This previous answer might help: How to alter single custom field value in views?

| improve this answer | |
  • Thanks for all the help. I ended up using a views hook pre render and changed the values there. – EdMedina Oct 23 '17 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.