Summarize grouping by year and month

后端 未结 6 1878
南笙
南笙 2020-12-24 05:32

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
2         


        
相关标签:
6条回答
  • 2020-12-24 05:44

    You need to use an array approach. How to combine arrays you can learn here https://stackoverflow.com/a/42488671/1393023

    Foremost QUERY

    It's simple and transparent

    =QUERY(
      ARRAYFORMULA({TEXT(Movements!A:A,"YYYY-MM"),Movements!B:B}),
      "select Col1,sum(Col2) group by Col1"
    )
    

    Secondly ARRAYFORMULA(SUMIF)

    Just combine the functions to a chain

    =SORT(UNIQUE(FILTER(
      {
        TEXT(Movements!A2:A,"YYYY-MM"),
        SUMIF(EOMONTH(Movements!A2:A,1),EOMONTH(Movements!A2:A,1),Movements!B2:B)
      },
      LEN(Movements!B2:B)
    )),1,1)
    

    My example

    0 讨论(0)
  • 2020-12-24 05:52

    in addition to contribution from JMax, let me present the following formula using the QUERY function ...

    =query(index('Sheet3'!A:B); "select year(Col1)+(month(Col1)+1)/100,sum(Col2) where Col1 is not null group by year(Col1)+(month(Col1)+1)/100 label year(Col1)+(month(Col1)+1)/100 'mydate',sum(Col2) 'amount' ")
    

    Notes: I have assumed the original data is in Sheet3 and my result presents the year and month combinationas yyyy.mm instead of yyyy-mm I have created an illustration:

    0 讨论(0)
  • 2020-12-24 05:53

    You can do it with an array formula, i put the formula in A2 and in A1, i put the month number:

    =ARRAYFORMULA(SUMPRODUCT(1*(MONTH(Sheet1!$A$2:$A$6)=A1)*Sheet1!$B$2:$B$6))
    

    See the doc in action in Google Spreadsheet

    [EDIT] If you want both year and month:

    =ARRAYFORMULA(SUMPRODUCT(1*(YEAR(Sheet1!$A$2:$A$8)=A2)*(MONTH(Sheet1!$A$2:$A$8)=B2)*Sheet1!$B$2:$B$8))
    
    0 讨论(0)
  • 2020-12-24 05:54

    The answer of matt burns and Cœur is pretty accurate and I would like to add a comment (I did not add it on the comment section as it is quite long)

    You may want to use this formula instead: DATE(YEAR(A2);MONTH(A2);1) to group by year & month.

    This way you will keep values as dates and therefore be able to apply any kind of date formatting or sorting in your pivot table.


    Explanation you can skip:

    Instead of TEXT(A2, "YYYY-MM") converts value in text and therefore remove freedom about further date manipulation.

    DATE(YEAR(A2);MONTH(A2);1) remove the day and time from the date so for example 2018/05/03 and 2018/05/06 both become 2018/05/01 which is still a date which can then be grouped together.

    By applying a format "YYYY-MM" directly on the pivot table you reach the same result.

    0 讨论(0)
  • 2020-12-24 05:57

    I was working with a similar issue. I found this formula to work well.

    =SUMIF(ARRAYFORMULA(MONTH('Sheet1'!A:A)), A2, 'Sheet1'!B:B)
    

    In cell A2 I put the month I wanted totaled.

    0 讨论(0)
  • 2020-12-24 06:02

    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.

    0 讨论(0)
提交回复
热议问题