ankane / groupdate

The simplest way to group temporal data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Labels for Quarters

timokleemann opened this issue · comments

Hi & thanks for creating this awesome gem!

It's much easier to set up and use than much of what I've seen before.

I'm struggling with creating labels for quarters, though.

It works great for months and years:

Payment.group_by_month(:created_at, format: "%m %Y").sum Payment.group_by_year(:created_at, format: "%Y").sum

For quarters I am using this...

Payment.group_by_quarter(:created_at, format: "%m").sum

...but it doesn't make much sense as it only shows the number of the quarter's first month:

  • 1
  • 4
  • 7
  • 10

This would be better:

  • 1
  • 2
  • 3
  • 4

I am fully aware that quarters aren't supported by strftime.

So how can this be done?

Hey @timokleemann, you can use a proc for the format to accomplish that.

Payment.group_by_quarter(:created_at, format: ->(v) { (v.month + 2) / 3 }).sum

Works, thank you!

Actually, use this one to avoid quarters from different years been aggregated together.

.group_by_quarter(:created_at, format: ->(v) { "#{v.year} Q#{(v.month + 2) / 3}" })