wesleysc352
wesleysc352

Reputation: 617

How to calculate the average of a column using other columns as a reference using excel

I have three columns in excel year, month value.

I want to average value considering month and year. In R language this function is done by group_by(). In excel how could this be done?

year    month   value
2019    1   12
2019    1   34
2019    2   56
2019    2   15
2020    1   16
2020    3   67
2020    4   89
2018    6   123
2018    6   45
2018    7   98
2019    3   53
2019    1   23
2020    1   12
2020    3   1

Upvotes: 0

Views: 57

Answers (2)

Scott Craner
Scott Craner

Reputation: 152660

If one has Office 365 we can use:

=LET(
    y,A2:A15,
    m,B2:B15,
    v,C2:C15,
    u,SORT(UNIQUE(CHOOSE({1,2},y,m)),{1,2}),
    CHOOSE({1,1,2},u,AVERAGEIFS(v,y,INDEX(u,0,1),m,INDEX(u,0,2))))

Put this in the first cell and it will spill the results.

enter image description here

Once the HSTACK is release we can replace the CHOOSE with it:

=LET(
    y,A2:A15,
    m,B2:B15,
    v,C2:C15,
    u,SORT(UNIQUE(HSTACK(y,m)),{1,2}),
    HSTACK(u,AVERAGEIFS(v,y,INDEX(u,0,1),m,INDEX(u,0,2))))

Upvotes: 1

pgSystemTester
pgSystemTester

Reputation: 9932

Averageifs would do what you want, but you might want to review using the Filter function to duplicate the Group_By() method for other similar procedures. Once grouped, you can sum/average/sort, etc.

Averageifs:

=AVERAGEIFS(C:C,A:A,2018,B:B,6)

Filter:

=filter(C:C,(A:A=2018)*(B:B=6))
=Average(filter(C:C,(A:A=2018)*(B:B=6)))

See this spreadsheet for examples of both. I realize you're using Excel, but these formulas should work on both (though they are not the same)

Upvotes: 0

Related Questions