Submitting this as a feature request because I can't find an existing request for this functionality and it would really help in a lot of different use-cases:

We need a way to control date granularity on views aggregation settings. Currently date fields aggregate at the lowest level of the date field, which makes aggregation essentially useless when you want to aggregate on a date field. For example: I have nodes with a year-month-day date field and I want a simple view that shows me how many nodes exist with date field values for each year. I want:

2012: 47 nodes
2011: 122 nodes
2010: 34 nodes

I dive in and configure my view, setting up the typical aggregation settings needed for this type of thing. I hit a snag when I realize that the view is grouping on the full date field, aggregating only the rows that have the exact same month/day/year.

Something like http://drupal.org/project/views_date_format_sql, only for date module fields, not just system dates.

If anyone can inform me on how to aggregate my date field values by year only, that would be awesome; we can close this request.

Comments

naeluh’s picture

I would also be interested in this functionality.
I am using views Highcharts and also notice that it only takes the full date also because I entered in several date formats and they all returned the same view no matter which date format I used

thanks !

Nick

Ryan Osītis’s picture

I'm working on a sports site and this granularity would be perfect for aggregated stats displayed by year/season. Any ideas for a workaround?

naeluh’s picture

I would love to find one too.
but I don't think this works at all yet.

naeluh’s picture

I am willing to work on this if someone can help me get started fixing this issue
thanks !

naeluh’s picture

I am really interested in getting this functionality going is there any one that would want to help put this together ?

thanks

Norberto Ostallo’s picture

I think you should take a look at Views Date Format SQL module. This could be a good starting point.

naeluh’s picture

thanks for the tip @Norberto Ostallo but it doesnt work with date field anymore the functionality got left behind somewhere http://drupal.org/node/1830436

Can you think of any other options ?

thanks again

naeluh’s picture

http://dev.client.gotpantheon.com/graph_example

Here is an example of the graph I am trying to build this should be 2 plots october and november with totals of all that fall on that month
Instead views outputs all of them and shows there respective month ?

thanks for any help

Nick

mpisano’s picture

+1

rbrownell’s picture

Component: Date Field » Views Filter

*Bump* This feature would be incredibly useful!

windmaomao’s picture

+1, can't do anything without it for reporting sites

mototribe’s picture

same here

there seems to be a workaround:
I use a table layout, enabled aggreation, added field "nid" with "Count distinct".
I then add a sort by "post date" with "monthly" granularity.
Then I added a "post date" field with custom format "M-Y". I select "min" instead of "group results together.

naeluh’s picture

yeah I would like this for sure !

RoSk0’s picture

@mototribe: Thank you very much! Your workaround is working(views 3)!

This issue should be noticed by maintainers

RoSk0’s picture

No, not working. Showing minimum as expected.

cvharris’s picture

@mototribe: I tried your workaround for my views aggregation and it worked so long as I only used a date sorting and did not add a field with the date included. I'd say RoSk0's concern is still worth looking into.

vanvemden’s picture

Agree, would be a very useful feature. Unfortunately mototribe's solution (#12) is not working for me.

teammk’s picture

Anyone's found a solution for this ?

The workaround can't works for me, custom format is not available on my date field.

Anyone has found a patch for this ?

This module https://drupal.org/project/views_date_format_sql works only for systems date fields.

Up please !

Roensby’s picture

Issue summary: View changes

This is my temporary solution for any entity such as nodes, users etc.

Just add a computed field to the entity, make it compute the granularity value of the date field and store the result in database - presto! You have a field in your entity that can be used to aggregate on behalf of the date field.

Example: we want to aggregate by month for a content type with a date field.
1. Install the computed fields module: https://drupal.org/project/computed_field
2. Make a note of your content types date fields machine name. Let's assume field_date
3. Add a computed field to the content type. As computed code, type in

$formatted_date = $entity->field_date[LANGUAGE_NONE][0]['value'];
$date = date_parse_from_format('Y-m-d H:i:s', $formatted_date);
$timestamp = mktime(
  $date['hour'],
  $date['minute'],
  $date['second'],
  $date['month'],
  $date['day'],
  $date['year']
);
$entity_field[0]['value'] = date('F', $timestamp);

The last line is converting a unix timestamp to the textual representation of a month. Replace 'F' with anything else, such as 'Y' for year, 'l' for day of week, aso.
Your date_field format might differ from mine, in which case - you need to modify the above.
4. Check "Store value in the database", otherwise the field is not available to views.
5. Once the field is added, resave all existing nodes of that content type ( use VBO or mass-publish from /admin/content )
6. Make a new view. Let's add Node ID (nid) and aggregate with count.
7. Add the computed field and aggregate with grouped by.
That's it!

If you want to sort the view, sorting by computed field in this case is no good, since we have months as text: we can only sort that field alphabetically.
Instead, add the date field to your sort criteria. Don't aggregate by grouping. Instead, choose average. Your months are now sorted by the date_field.

The good thing about this solution is that it is easy to remove, once the date module has support for granularity aggregation. Simply delete the computed field and fix the view :)

Marko B’s picture

This solution from #19 is nice.

GAMe’s picture

Im trying to do the same thing however using #19 workaround with computed field im still not getting the result. Such a shame this isnt working in the date module.

*EDIT* After looking at #19 with fresh eyes today I realised I was being a bit of a muppet and had left in the start and end php tags. Doh! Thanks #19 :-)

secretsayan’s picture

Thanks @mototribe....your workaround is really cool....something to think about

pegahmajma’s picture

hi
I have solution that maybe good for someone:
I create format date in configuration with: Y-m
and in views>config of date field> choose this format for date
and with aggregation group with date.
it's work for me fine.

skadu’s picture

@cchow's workaround in #19 works nicely. Thanks for that.

mooru’s picture

#19 works great but i had to remove the php tags and use computed field tools

moarli’s picture

#19 works great. thank you. How I can show the output in german?

jackjohnsonzm’s picture

#19 Roensby's workaround works just fine, thank you, similar issues to those found above, trying to aggregate by date.

stelmo’s picture

Hello, I tried like pegahmajma #23 said and it doesn't work for me.