Finding the average monthly value time

wayne0881

Board Regular
Joined
Nov 2, 2009
Messages
95
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I am creating a table to try and find the average value of a date range but unsure how to best approach it.

A4:A264 are dates - with the header "Week"
B4:B264 is data (values are between 1-100) - with the header "Data"

Columns D3:P9 is a table
D4:D9 Years (2017-2022)
E3:P3 Months (Jan-Dec)

E4 represents Jan 2017

The end result I am hoping for is that I can get in cell E4 the average total for Jan 2017 from (column B) - E5 would represent Jan 2018 and so on until cell P9 (which would probably show #DIV/0! as we haven't got to Dec 2022 yet so there is no data.

Can excel go and retrieve that data with the data as is? (AVERAGEIFS) or do you think it would be better to split column A into two seperate columns one for Month and one for Year and perform a Vlookup/Match function (or something alike)

Thanks All

5_year_data.PNG
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
@wayne0881 Here is a possible solution.
Formula is a tad ugly looking. Could be simplified dependant upon your Excel version or happiness to use 'helper' columns / row.

Book1
ABCDEFGHIJKLMNOP
3WeekDataJanFebMarAprMayJunJulAugSeptOctNovDec
44/23/17482017   48.545.7544.548.25044.7546.249.550.2
54/30/1749201844.547.25          
65/7/17472019            
75/14/17472020            
85/21/17442021            
95/28/17452022  49         
106/4/1745
116/11/1745
126/18/1743
136/25/1745
147/2/1746
157/9/1748
167/16/1749
177/23/1748
187/30/1750
198/6/1750
208/13/1750
218/20/1749
228/27/1751
239/3/1744
249/10/1745
259/17/1745
269/24/1745
2710/1/1743
2810/8/1745
2910/15/1746
3010/22/1748
3110/29/1749
3211/5/1748
3311/12/1750
3411/19/1750
3511/26/1750
3612/3/1749
3712/10/1751
3812/17/1751
3912/24/1751
4012/31/1749
411/7/1845
421/14/1845
431/21/1843
441/28/1845
452/4/1846
462/11/1848
472/18/1849
482/25/1846
493/4/2252
503/11/2246
Gradings_2
Cell Formulas
RangeFormula
E4:P9E4=IFERROR(SUMPRODUCT((YEAR($A$4:$A$300)=$D4)*(MONTH($A$4:$A$300)=COLUMNS($E:E))*($B$4:$B$300))/SUMPRODUCT((YEAR($A$4:$A$300)=$D4)*(MONTH($A$4:$A$300)=COLUMNS($E:E))),"")


Hope that helps.
 
Upvote 0
An alternative is to use Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Week", type date}}),
    #"Inserted Month Name" = Table.AddColumn(#"Changed Type", "Month Name", each Date.MonthName([Week]), type text),
    #"Inserted Year" = Table.AddColumn(#"Inserted Month Name", "Year", each Date.Year([Week]), Int64.Type),
    #"Grouped Rows" = Table.Group(#"Inserted Year", {"Year", "Month Name"}, {{"Average", each List.Average([Data]), type number}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[#"Month Name"]), "Month Name", "Average"),
    #"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",{"Year", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"})
in
    #"Reordered Columns"
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

With the same data as @Snakehips has used

If you have a recent version with the FILTER function ..

22 04 23.xlsm
ABCDEFGHIJKLMNOP
3WeekDataJanFebMarAprMayJunJulAugSeptOctNovDec
423-Apr-17482017   48.545.7544.548.250 46.249.550.2
530-Apr-1749201844.547.25          
607-May-17472019            
714-May-17472020            
821-May-17442021            
928-May-17452022  49         
1004-Jun-1745
1111-Jun-1745
Month Average
Cell Formulas
RangeFormula
E4:P9E4=IFNA(AVERAGE(FILTER($B$4:$B$264,TEXT($A$4:$A$264,"mmmyyyy")=E$3&$D4,NA())),"")


For older versions ..

22 04 23.xlsm
ABCDEFGHIJKLMNOP
3WeekDataJanFebMarAprMayJunJulAugSeptOctNovDec
423-Apr-17482017   48.545.7544.548.250 46.249.550.2
530-Apr-1749201844.547.25          
607-May-17472019            
714-May-17472020            
821-May-17442021            
928-May-17452022  49         
1004-Jun-1745
1111-Jun-1745
Month Average (2)
Cell Formulas
RangeFormula
E4:P9E4=IFERROR(AVERAGE(IF(TEXT($A$4:$A$264,"mmmyyyy")=E$3&$D4,$B$4:$B$264)),"")
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Here is UDF that may be useful.
First parameter takes range with dates,
second parameter takes range with data and
third parameter takes address top left cell of the final table ("D3" in this case).
NOTE: Don't forget to insert fixed parameter refrences,
you will be able to fill formulas with draging.
VBA Code:
Function MonthAverageByYear(vR1 As Range, vR2 As Range, vR3 As Range)

        Dim vDates, vData, vRows As Long, vNRows As Long, _
            vSum As Double, vTopLeftCell As Integer

        vDates = vR1.Value
        vData = vR2.Value
        vTopLeftCell = vR3.Column
        vYear = Cells(Application.Caller.Row, vTopLeftCell)
        For vN = 1 To UBound(vData)
            If Format(vDates(vN, 1), "yyyy") = CStr(vYear) And _
                Month(vDates(vN, 1)) = Application.Caller.Column - vTopLeftCell Then
                vSum = vSum + vData(vN, 1)
                vNRows = vNRows + 1
            End If
        Next
        If vSum = 0 Then
            MonthAverageByYear = ""
        Else
            MonthAverageByYear = vSum / vNRows
        End If

End Function
 
Upvote 0
Thanks everyone for your very helpful replies.

I will try out all of these later today and update
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top