Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Summarize grouping by year and month

I have a worksheet Movements with two fields:

mydate      | amount 2009-01-01  | 10.00 2009-01-02  | 13.00 2009-02-01  | 11.00 2009-02-05  | 12.00 2009-02-08  | 52.00 

I want to have in another worksheet MonthSum that displays the sums of the data in the column amount grouped by year+month of the column date:

mydate      | amount 2009-01     | 23.00 2009-02     | 75.00 

I don't want to specify the cells where the spreadsheet has to sum, I want a generic formula to group my data per month+year. If I was on a MySQL database I would simply do:

SELECT DATE_FORMAT('%Y-%m', mydate), SUM(amount)  FROM mytable GROUP BY DATE_FORMAT('%Y-%m', mydate) 

I need the solution to work on Google Spreadsheets.

I think that possible solutions would be using SUMIF or pivot tables or Google Spreadsheets QUERY function.

like image 307
nulll Avatar asked Sep 05 '11 23:09

nulll


People also ask

How do you group data based on months?

Right-Click on any cell within the Dates column and select Group from the fly-out list. Then select Month in the dialog box. Using the Starting at: and Ending at: fields, you can even specify the range of dates that you want to group if you don't want to group the entire list.


1 Answers

This is what pivot tables are for.

I have created an example using your data.

First, add a column to format the date as your monthly string, eg "2009-01" with the formula:

TEXT(A2, "YYYY-MM")

enter image description here


Then highlight the data, and choose "data" > "pivot table report..."

enter image description here


For Rows, select "month"

enter image description here


For Values, select "amount"

enter image description here


TADA! That's it!

enter image description here

For a quick overview of pivot tables, see this Google blog post.

like image 159
matt burns Avatar answered Sep 25 '22 01:09

matt burns