I am trying to create a column with a data field to sum my other two columns. For example, below, I'd like to add a column to the right of "2015" that is "Total."

Total column

Attached is an export of my current view.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jurgenhaas’s picture

I've started implementing this so that for each field in the table you can select if aggregation for their column and/or for their row should be calculated. For now, this only work for the sum, others like average etc can be added later easily.

Please give the attached patch a try. It should apply to the latest dev release and you can then go into the view sstyle setting where you turn on aggregation for the amount field.

Please let me know if that's working and what the issues may be.

MrPeanut’s picture

FileSize
4.73 KB

Wow, @jurgenhaas, you are good! Patch applied cleanly. Changed my style options and selected "sum" for both the column aggregate and row aggregate on the data field. I'm not a programmer so additional feature requests would be formatting and labels. :) However, this is exactly what I was looking for! Thank you!

Aggregation

jurgenhaas’s picture

Thanks for your quick feedback, that's good news.

Regarding labels and formatting I was thinking this: the label should be available in the views plugin settings next to the select field where you enable aggregation. Formatting however, I'd like to use the field formatting settings that are applied to the field which is aggregated.

What do you think?

MrPeanut’s picture

I think that makes perfect sense. For example, my data field is formatted with a comma, two decimal points, and prefix/suffix (in my case, a dollar sign). It would make sense to use that same formatting in the row or column aggregation. Fantastic module you're putting together here!

jurgenhaas’s picture

Right, this is now also included and you'll find the results in the latest dev release of this module. Please give it a try as soon as it's available for download from d.o

  • jurgenhaas committed 448ed3d on 7.x-1.x
    #2374947 Columns and rows for aggregation: implement more aggregation...
petednz’s picture

hi - this looks exciting possibility.

just tried dev to get row and column sub-totals

is the below an error i am getting because some of my 'cells' are null as there is no data for some options?

or, more likely, have i simply mis understood the required set up.

The Pivot Table looks great and gives me the results I wanted in the Rows and Columns - so thank you for putting this module together - but either I get no 'SUM' results or the below error depending on which options i try.

I am assuming that to have a sub total for the rows, where the data being aggregated is 'field 3' that I should be setting the Row Aggregate to 'sum of values' for Field 3?

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /admin/structure/views/view/members1/preview/default/ajax
StatusText: OK
ResponseText: 
Fatal error:  Cannot access empty property in /srv/www/internetnz/current/sites/all/modules/views_pivot/views_pivot.theme.inc on line 329
jurgenhaas’s picture

Your setup looks correct. Debugging the error however is pretty hard, looking at just the code hasn't helped identifying the reson for the problem. Is there a way you could send me the data (db tables and exported view definition) or could you debug the lines 327-329 in views_pivot.theme.inc and probably find out more details what's going on there?

petednz’s picture

thanks for your prompt reply - the data is complicated as it is civicrm + drupal data

i will fire up a view using just drupal data that has some NULL values and see if that triggers same issue

and get a civi view that has no null values in case it is at that end ;-)

and then get someone to look over my shoulder and see if we can debug it a bit

petednz’s picture

FileSize
4.65 KB

grabbed from git in case the above 'commit' was not in latest dev

now seeing

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /admin/structure/views/view/user_pivot/preview/default/ajax
StatusText: OK
ResponseText:
Fatal error: Call to undefined method views_handler_field_user::set_items() in /srv/www/chch.alted.org.nz/sites/all/modules/views_pivot/views_pivot.theme.inc on line 330

Not sure what I am doing wrong, or what is different about my system.

it may be i just don't get what the setting should be ie if field1 is my row, and field2 is my column, and field3 is my 'data', and if i want to see eg Sum of Values at the end of each Row, do i set the Row Aggregate for Field 1 or 3?

I tried both, Field 1 gives nothing, Field 3 throws the above

here is about as basic a view as we can do ;-)

3 fields
user name
User email
user uid

with name as row, email as column, uid as data in pivot table cell

with the assumption that I should then be able to use the Row Aggregate = Number of Values (not a useful set of data but perhaps it lets you very easily either confirm the error - or explain the user error

FYI Drupal 7, Views 3 etc everything up to date

  • jurgenhaas committed a08a561 on 7.x-1.x
    #2374947 Improve the rendering of aggregated values
    
jurgenhaas’s picture

Thanks a lot @petednz, this is great help to hopefully nail this down quickly. I have improved the code and comitted that to the dev release. Please give that a try as soon as it is available for download and let us know if it now works.

petednz’s picture

FileSize
4.74 KB

again thanks for the prompt engagement

updated via git pull

With the new code ....

setting to 'number of values' for Field3 for both Column and Row Aggregate does now show values (as per updated View attached).

changing them to 'sum of values' gives the error below and shows no results

n AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /admin/structure/views/view/user_pivot/preview/default/ajax
StatusText: OK
ResponseText: 
Fatal error:  Cannot access empty property in /srv/www/internetnz/current/sites/all/modules/views_pivot/views_pivot.theme.inc on line 348

(nb i was getting no result but no error when I had the uid field set to 'link to user')

I am reattaching the view with it set so it shows results of 'number of values' - can you confirm that changing either to 'sum' or 'average' creates the error?

jurgenhaas’s picture

Thanks for your further request. I've had another look and realized that you're aggregating over a property (uid) rather than a field. I have improved the code so that it can deal with that as well.

However, there must be another difference. I guess that comes from the PHP version. Which PHP version are you using?

In any case, you can test the latest dev release again and see if that solved your problems now.

  • jurgenhaas committed 9d0b281 on 7.x-1.x
    #2374947 Fix the rendering of aggregated values where field is a...
petednz’s picture

php is 5.4

petednz’s picture

and yes that fix fixed things for UID - many thanks.

still no joy with our civicrm data but we will take a poke in the views_civicrm code as we expect it is an issue of how the data is getting in to the view that means it is not 'summable'

thanks for your work on this

MrPeanut’s picture

FileSize
6.8 KB

Hi jurgenhaas — I am running into an issue with this feature. I have a content type that collects a location (taxonomy term) and a multiple-value select field (gadgets). I have a view set up with your incredible module that totals the number of gadgets per location.

My fields are: Location, Gadgets, and Gadgets (COUNT).

My pivot table row is Location and column is Gadgets. The Gadgets (COUNT) is then the data. I set a column aggregate and row aggregate to sum values on Gadgets (COUNT). However, it is showing up blank.

If I change it to number of values, it does count the number of values, but doesn't sum them. Any thoughts?

Image

Cheope’s picture

Hi! Same problem and situation to MrPeanut's here. I have to count nodes (subscribers) grouping them by two different entity reference fields (location and belonging group) and get the total row and column sum for each, but the total values don't show up. Where I go wrong? Thanks.

MrPeanut’s picture

FileSize
6 KB
178.06 KB

@jurgenhaas — As we discussed on #drupal, here's the view and database export. Thank you!

jurgenhaas’s picture

This was an interesting one: you are using an aggregated field (count on gadgets) but views isn't counting properly. You can see that when you change your view into a normal table (instead of a pivot table). What you're getting is a list of 15 records with a count = 1 for each row. That means, views is just listing all the rows but doesn't count those together where gadget and store are having the same values.

So, the view as such is not producing the correct results, even without using the pivot formatter.

You can also see the wrong result, that you only get values of 1 in each of the cells where as you would expect higher numbers in some cases, even without calculating the totals per column or per row.

However, while I was on it to analyze that, I found a way to resolve that problem which isn't caused by views_pivot (as explained above). And while solving that alien problem the aggregation for pivot got solved automatically as well. For now, this is only implemented and tested for views count aggregation.

PLease give the latest dev release a try and let me know if you're satisfied with this.

MrPeanut’s picture

@jurgenhaas — Perfect! Thank you so much! My one and last feature request would be a "grand total" (sum either the row totals or column totals to come up with a grand total for the entire table). Thanks again for your prompt response and great work.

Cheope’s picture

@jurgenhaas - Thank you very much! I had an aggregated count column like MrPeanut's above. I've just installed your latest release and by disabling column aggregation everything works like a charm!
Thanks for your great work! :-D
Cheers!

jurgenhaas’s picture

@Cheope, great to hear, thanks for your feedback.

@MrPeanut, not a bad idea but I wonder about a little detail: if you use both, column and row aggregation, a grand total is easier because we already have a cell in the table where to put the result. However, if one sets up a view with either only row aggregation or only column aggregation, then we're missing either a row or a column where we cut stick the grand toal into. Any ideas on how we should handle that?

MrPeanut’s picture

@jurgenhaas — Hmm, well, not really. In the case of row totals, I suppose I would expect it to be in a new row. All but the last column would have empty cells, but the last cell would be the grand total (or, the total of all row totals). However, I'm not sure if that's possible with the way Views or Views Pivot creates the tables or if that even makes the most sense.

nadeeke’s picture

Here I want to calculate some columns.
I collect readings from a machine monthly basis.
I want the result between two months. (two months can be filter) in pivot two columns
meter = last month - previous month reading
meter with tax = meter * 12%
Meter with another Tax = meter with tax * 5%
Total = meter with tax + meter with tax + Meter with another Tax
Can we do this

jurgenhaas’s picture

@nadeeke that sounds like a very specific requirement and I wonder if more than one site is going to need something like this. Unless we manage to define a generic requirement that is useful to a wider range of use cases, I would want to include that into a module. You should either develop a custom module for this or use the views_php module to define the output with a little PHP snippet that's assigned to the view.

@MrPeanut regarding your summary of sums I was wondering if we should enable that only in cases where both column sum and row sum was enabled? Because in that case we already have that one empty cell where we could output that overall result.

MrPeanut’s picture

@jurgenhaas — Yes, I think that makes sense. That would be the case where you would most likely want it anyway (i.e. already having the row and column totals).

  • jurgenhaas committed d4b47e3 on 7.x-1.x
    Issue #2374947 by jurgenhaas: Create additional column for calculations...
jurgenhaas’s picture

@MrPeanut OK, I've committed an attempt to the latest dev, please let me know if that works for you as expected.

MrPeanut’s picture

@jurgenhaas — Perfect! You are good!

jurgenhaas’s picture

Status: Active » Closed (fixed)

Thanks @MrPeanut so it's time for a new release :-) Going to publish that the next couple of days.

nadeeke’s picture

Thank you very much for your reply; I want to calculate again with calculated column. but in views we have no column named that. FInal problem is total of total grand total please help to generate it. Thank you again this is a great help.

jurgenhaas’s picture

@nadeeke it sounds that what you're asking for has been implemented yesterday. If you turn on SUM for columns and rows, then this module is now also outputting the SUM of the sums in the bottom right corner. If this is not what you're looking for, please open a new issue and describe in detail what it is you're looking for - ideally by providing some examples, that's easier to understand.

nadeeke’s picture

Thank you @jurgenhaas
Can you give us a way to identify this aggregated column to reuse in the global php or global text field as available variable or a replacement pattern.

jurgenhaas’s picture

Don't think that's possible. The value(s) get calculated at the very end of the themeing layer and I don't believe there is a way to pass that back as a token. I might be wrong and would of course accept patches. But again, that is a different issue and this one is marked as closed and fixed.

nadeeke’s picture

Thank you very much

najamfzl’s picture

Thanks for this great module. I have similar problem like MrPeanut about aggregated columns in pivot table. I want to list all types of items related to a topic and then aggregated them. The topic node, item types nodes (MCQs nodes, CRQs nodes, ERQs nodes) are all group content.

If I choose a table layout, the data seems to be displayed correctly (see image) which means the views is aggregating it correctly, however switching to pivot with topic title in 'rows', Item Type in 'columns' and COUNT(Distinct) NID as 'data', I cannot aggregate a "Sum of values" in either row or column. When selecting "number of values", it displays the count correctly but I want the sum of count of NIDs.

Now in #23, I don't understand the "alien" problem mentioned by jurgenhaas.

"And while solving that alien problem the aggregation for pivot got solved automatically as well"

There must be something wrong with my config of Pivot Table. Please suggest me a solution. I tried with the latest dev version with same results.

jurgenhaas’s picture

As this is a closed issue it would be better to start a new one and refer back to this one if necessary or helpful. And also, you may have hidden attachments from other users by accident, I've reverted that change.

Looking at your screenshot it looks like you're combining two columns (ID and Title) into one and that one is then the row for the pivot table. That is probably the reason for this. To verify that assumption, please remove the ID or Title field and work with a simple one only - or define the title as a "Pivot Extra Row" and leave it in its own column.

najamfzl’s picture

@juegenhaas: Yup they were added accidently. I have opened a new issue here as a support request.

Thanks