Views Groupby Date Granularity

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
rcharamella's picture

Hi all,

I've got a View that displays content types sorted by a date field. The Date field contains month, day and year. The sort works fine.

But I'd like to group the listings by Year. I can Group by the date field, but it doesn't give me enough granularity to specify the Year, only the whole date. This creates a "group" for each day. Is there a way that I can get the granularity that I need to group by year only?

Thanks in advance.

Comments

Solution

rcharamella's picture

Thanks to Shifthappens for sending me this solution

  1. Create a new date format that just displays the year
  2. Update your cache so Views has visibility to the new format
  3. Update your view so the grouping field uses this new display format. Check the 'exclude from display' box which will only show the year once for each grouping

Thanks! Here are the full steps

agmin's picture
  1. Create a new date format
  • Go to Administrator -> Site Configuration -> Date and time
  • Click the "Formats" tab
  • Click "Custom Formats" then "Add Format"
  • Enter "Y" (without quotes) and click Save Configuration
  • Click the "Configure" tab.
  • At the bottom, add Name: "Year Only" Type: "year_only" and click "Save Configuration"
  • Now in the select menu next to "Year Only" select the option that just shows the 4 digit year, then save the configuration
  1. Go to Administration -> Site Configuration -> Performance -> Clear Cache
  2. Now in your view edit form, in your custom date field at the bottom there's a select menu of formats. You should see "Year Only" listed now!

Excelent!

Memoskins's picture

Thank you very much, the solution is really quick and simple. I was looking the way to group a list by date like this:

29/09/2011
1 Item
2 Item
3 Iten

28/09/2011

1 Item
2 Item
3 Item

And this solved my problem!
Greetings
Memoskins

Additional step for this to work in D7/Views3

csuggs4's picture

This solution did not work for me with Views 3 in Drupal 7. It required the additional step of disabling the RDF module in Core, as recommended by KarenS: http://drupal.org/node/1253562#comment-4886998

Thx, csuggs4, after hours of

rwilson0429's picture

Thx, csuggs4, after hours of searching for a solution, disabling the RDF module worked for me.

Amazing who would have

Alauddin's picture

Amazing who would have thought RDF module was the culprit...this still works after all these years.

thanks

Thanks

Slacky08's picture

Thanks,

This solution works perfectly - thanks for updating your quesiton with the solution!

Just what I was looking for

jordimateubesancon's picture

Thank you very much agmin. I was reading this http://drupal.org/node/388140 and I couldn't believe that an easy solution wasn't possible. Override the date field output, write php... augh! To much for a rookie ;-) Thanks again.

The Easiest Solution.

Jeroen's picture

The easiest solution is to add a date field to the "Fields" that are used in the View.

For example if you want to group by year, on the post dates of the content, add the Node: Post date field.
In its option choose to Exclude from display" So it won't be shown and set the "Date format" to Custom. Choose 'Y' (without quotes) for the format. It will then only output the year.

If you set the "Style" of the view to "HTML List" you can click the cog-wheel next to it and select a "Grouping field" from the Fields you added to the view. Select the Post date field and you'll see the view being grouped on that field.

Yes, but it seems to work

ilgriso's picture

Yes, but it seems to work only with "native" date fields (like "post date field"), with some custom date fields, you have to disable the RDF module as suggested above. Thanks 4 giving me this suggestion!

no need for RDF disabled (at

GiorgosK's picture

no need for RDF disabled (at least with latest views)
under grouping field option you have two options

* Use rendered output to group rows
* Remove tags from rendered output

check both of them
and views grouping will happen with the rendered date field and RDF will not bother ...

Highcharts Module and groupby

naeluh's picture

I am using the highcharts module and I also would like to group my graph by month. However I can get it to group by day it will not group by month is there any reason why this might be happening ? I am using the date field but it doesnt seem to change anything when I just use month as my data I d appreciate any info that could help.

thanks

That worked for me

damienroussat's picture

Thank you so much for posting that it worked for me after countless hours of search.
Thank you !!!

Sort by

lammensj's picture

I got to the point that I can group a result set by year. But how can I sort within each group by month? I want to get something like this:

2013
18/01/2013 Bar
01/04/2013 Bar Foo
13/06/2013 Bar Foo Bas

2012
21/05/2012 Bar
30/08/2012 Bar Foo
05/10/2012 Bar Foo Bas

Anybody got an idea?

Sort handler for datetime dates

lammensj's picture

I created a custom Views sort handler that will add some granularity to a datetime field (Like a Date field :p). This basically resolved my entire issue!

See the file on Pastebin here: http://pastebin.com/XpwVkrZc

Also remember you can edit

glynster's picture

Also remember you can edit the view and make sure "Remove tags from rendered output" is turned off if you happen to use RDF.