Perform math calculation in views On Drupal 8 site, I have a exact copy of the below excel example table made with views but without the yellow cell yet.



The view is displaying one content type: Client where all the below fields are available:


Date
Client Name
Client ID
Amount ($)


Now, as you can see, the yellow cell is displaying the total SUM of all amount ($) rows.
which basically is: 15+29+97+115+45+67+23+11+5+91+44 = 542$

How can i achieve this exact calculation in my view ?

I have tried to enable the Use aggregation option under the advanced section on my view, but it seems it doesn't do what i am asking for!

any help please ?

Note: I believe the use Use aggregation option doesn't add a row at the bottom of the table calculating the sum of all above rows and therefore this question Making a colums total (SUM) with Views is not a possible duplicate of this one.

UPDATE #1

After following the answer of @Stef Van Looveren, I am encountering a new error:

InvalidArgumentException: Field field_money_amount is unknown. in Drupal\Core\Entity\ContentEntityBase-&gt;getTranslatedField() (line 587 of /home/####/public_html/#####/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php)


Knowing that: field_money_amount is the id of the field taken from the content type being used inside the view.

Complete code in the .module file:

&lt;?php

use Drupal\views\ViewExecutable;

function my_module_views_pre_render(ViewExecutable $view){
  if ($view-&gt;id() == "view_id" &amp;&amp; $view-&gt;current_display == 'display_id') {
    $total = 0;
    foreach ($view-&gt;result as $value) {
      $number = $value-&gt;_entity-&gt;get('field_money_amount')-&gt;value;
      $total += $number;
    }
    $view-&gt;attachment_after = [
      '#markup' =&gt; '&lt;table class="table cols-0"&gt;&lt;tr&gt;&lt;td&gt;Total:&lt;/td&gt;&lt;td&gt;'.$total.'&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;',
      '#allowed_tags' =&gt; ['table','tr','td']
    ];
  }


}
Interesting question and a typical use case. In a custom module, do the following:

use Drupal\views\ViewExecutable;

function helper_views_pre_render(ViewExecutable $view){
  if ($view-&gt;id() == "numbers" &amp;&amp; $view-&gt;current_display == 'page_1') {
    $total = 0;
    foreach ($view-&gt;result as $value) {
      $number = $value-&gt;_entity-&gt;get('field_number')-&gt;value;
      $total += $number;
    }
    $view-&gt;attachment_after = [
      '#markup' =&gt; '&lt;table class="table cols-0"&gt;&lt;tr&gt;&lt;td&gt;Total:&lt;/td&gt;&lt;td&gt;'.$total.'&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;',
      '#allowed_tags' =&gt; ['table','tr','td']
    ];
  }
}


Change the names according to your view name &amp; field names. I'm sure you get the point: you add a table below with the total result. 

I did a test and it gave me the following result: