+ Reply to Thread
Results 1 to 12 of 12

Calculating average from every nth row across varying periods

  1. #1
    Registered User
    Join Date
    07-23-2017
    Location
    Wroclaw, Poland
    MS-Off Ver
    2015
    Posts
    11

    Calculating average from every nth row across varying periods

    Hello.
    I have a large number of data for 1h measurements across 18 years in which I would like to calculate the hour average in each season. I am looking for a way to get the average of every nth hour (offset of 24h) across the seasonal scale (winter, spring, summer, fall) from a range of inputs (the period from 2000 to 2018).
    Data selection for every nth hour for each day can be done using the OFFSET function + MATCH returns, but can I use the second-time OFFSET function to return a reference season using an accounting year approach? To return the quarter of the year, I can apply the formula of ROUNDUP(MONTH(…)/3,0) or IF condition where the first date actually starts of the season (e.g. winter) and the last date is the end of the season of the prior year, then move on to the next year by performing the twice OFFSET formula.
    Finally, count all the cells will give the results of 24*4 rows (for 24 hours, e.g for 0:00, 1:00, 2:00 …. 22:00, 23:00; and 4 seasons) across the 2000-2018 time scale.
    However, instead of making a complicated formula that would be likely to have errors in presented massive data I am looking for formula as a combination for matching criteria of next hour for dynamic season range calculations.

    Attached I add test.xls
    Thank you for help.
    Attached Files Attached Files

  2. #2
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,053

    Re: Calculating average from every nth row across varying periods

    Some one will come up with a neater way... This is a monster.


    An array formula, copied down:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    there were a couple of complications with this:

    1. the breaks in the data ("brake" rows) causing errors, and

    2. Floating point arithmetic causing problmes with the times, and

    3. Your wish to have it done by season, rather than month.

    Array Formulae are a little different from ordinary formulae in that they MUST be confirmed in the FIRST CELL ONLY by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER. After that, the array can be dragged down as normal, to cover the desired range.

    You will know the array is active when you see curly brackets { } - or "curly braces" for those of you in the USA, or "flower brackets" for those of you in India - appear around the outside of your formula. If you do not use CTRL+SHIFT+ENTER you will (almost always) get an error message or an incorrect answer. Press F2 on that cell and try again.

    Don't type the curly brackets yourself - it won't work...

    In columns O & P you can see which season I assigned each month to. If you want something different, let me know.
    Attached Files Attached Files
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  3. #3
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: Calculating average from every nth row across varying periods

    Quote Originally Posted by GK
    Some one will come up with a neater way... This is a monster.
    Pivot? e.g. (with rogue break lines removed - presumably there to indicate lots of data in real-life)

    Append the season to the lines: =LOOKUP(MONTH(A1),{1,3,6,9,12},{"Winter","Spring","Summer","Fall","Winter"})

    Create a Pivot with Season as Column, and DateTime as Row - group latter by Hour (only), and set Values to Average.

  4. #4
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,053

    Re: Calculating average from every nth row across varying periods

    Possibly!! I have a bit of a thing about Pivot Tables (don't like 'em), so rarely suggest them.

  5. #5
    Registered User
    Join Date
    07-23-2017
    Location
    Wroclaw, Poland
    MS-Off Ver
    2015
    Posts
    11

    Re: Calculating average from every nth row across varying periods

    Thank you for your answers.

    In the original dataset, there are no breaks, I've just made them because of the huge size of document .xlsx (matrix size 168 000 rows x 214 columns). I have ongoing range from 1/1/2000 00:00 to 12/31/2018 23:00, however, some dates or values of 1h measurements are also lost.

    Kind regards
    Attached Files Attached Files

  6. #6
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: Calculating average from every nth row across varying periods

    Pivot approach attached.

    Given your data is, technically, static in nature you could (if needed) remove the data tab once you've created the Pivot.
    You can, in turn, regenerate the underlying data table by double clicking on the Grand Total in the Pivot itself.
    This approach can be useful if the files become too large and you need to distribute etc.
    Attached Files Attached Files

  7. #7
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,053

    Re: Calculating average from every nth row across varying periods

    That's why we ask for representative data!!!

    Monster formula again. No need for array entry.

    =SUMPRODUCT((MONTH($A$2:$A$317)=IF(I2="Winter",{1,2,12},IF(I2="Spring",{3,4,5},IF(I2="Summer",{6,7,8},{9,10,11}))))*(ROUND(1440*MOD($A$2:$A$317,1),0)/1440=ROUND(1440*J2,0)/1440)*$B$2:$B$317)/SUMPRODUCT((MONTH($A$2:$A$317)=IF(I2="Winter",{1,2,12},IF(I2="Spring",{3,4,5},IF(I2="Summer",{6,7,8},{9,10,11}))))*(ROUND(1440*MOD($A$2:$A$317,1),0)/1440=ROUND(1440*J2,0)/1440))
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    07-23-2017
    Location
    Wroclaw, Poland
    MS-Off Ver
    2015
    Posts
    11

    Re: Calculating average from every nth row across varying periods

    I am grateful for the great solutions!

    What I am thinking about how to properly compare my results because some of data range was lost. In the case of 1 h measurements, some cells were empty (no data) and
    I want to check the data completeness (if the diurnal result includes 24 values - completeness = 100%; if seasonal sum of values, e.g. winter = 31+31+ 28 {Dec, Jan, Feb} * 24 = 2160 - completeness = 100%).

    Thank you once again for your suggestions!

  9. #9
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,053

    Re: Calculating average from every nth row across varying periods

    The count is the denominator of my formula:

    =SUMPRODUCT((MONTH($A$2:$A$317)=IF(I2="Winter",{1,2,12},IF(I2="Spring",{3,4,5},IF(I2="Summer",{6,7,8},{9,10,11}))))*(ROUND(1440*MOD($A$2:$A$317,1),0)/1440=ROUND(1440*J2,0)/1440))
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    07-23-2017
    Location
    Wroclaw, Poland
    MS-Off Ver
    2015
    Posts
    11

    Re: Calculating average from every nth row across varying periods

    Hello,

    I very much appreciate your guidance on this issue.

    We are so close to having the correct formula for my specific data range. The missing thing is how to show the formula what to do when it encounters a blank cells. Many cells in the data range are blank and it is a trouble with getting the formula to ignore blank cells (not calculate as zeros).

    =SUMPRODUCT((MONTH($A$2:$A$317)=IF(I2="Winter",{1,2,12},IF(I2="Spring",{3,4,5},IF(I2="Summer",{6,7,8},{9,10,11}))))*(ROUND(1440*MOD($A$2:$A$317,1),0)/1440=ROUND(1440*J2,0)/1440)*$B$2:$B$317)/SUMPRODUCT((MONTH($A$2:$A$317)=IF(I2="Winter",{1,2,12},IF(I2="Spring",{3,4,5},IF(I2="Summer",{6,7,8},{9,10,11}))))*(ROUND(1440*MOD($A$2:$A$317,1),0)/1440=ROUND(1440*J2,0)/1440))

    I have reviewed other possibilities about this (e.g., IF($B$2:$B$317<>"", ... ) ; IF(NOT(ISBLANK($B$2:$B$317)), ... ) but still, have been unable to amend this long formula to calculate an average from every nth row across varying periods if cells are empty?

    At present formula doesn't ignore Blank Cells when performing calculations and takes them as zeros...

    Thanks in advance!

  11. #11
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,567

    Re: Calculating average from every nth row across varying periods

    What about making a consolidated data set using a formula like: =INDEX(A:A,AGGREGATE(15,6,ROW(A:A)/(A:A<>""),ROWS(A$1:A1)))
    Then apply the above formula to the consolidated data.
    If that doesn't do what you want, it may help if you upload a representative set of data that includes blanks.
    It may also help if at least a few of the expected results are included so that we have something against which to test the results of our proposed formulas/code.
    Let us know if you have any questions.
    Consider taking the time to add to the reputation of everybody that has taken the time to respond to your query.

  12. #12
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: Calculating average from every nth row across varying periods

    Please try at G2

    =AVERAGE(IF((INT(MOD(MONTH($A$2:$A$52604),12)/3)+1=COLUMNS(G1:$G1))*(HOUR($A$2:$A$52604)=ROWS(G$2:G2)-1)*($B$2:$B$52604>0),$B$2:$B$52604))
    Press Ctrl+Shift+Enter
    copy till J25
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calculating maximum of moving average values across varying periods
    By yaro_yaro in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-11-2019, 07:06 AM
  2. [SOLVED] How to use MATCH to get results within varying timestamp periods
    By jrtaylor in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 11-17-2018, 01:36 PM
  3. Charting occurrences over varying time periods
    By bex1210 in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 03-05-2018, 10:25 PM
  4. [SOLVED] Total units made by week number with varying manufacturing periods
    By BRISBANEBOB in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-09-2016, 06:49 PM
  5. IRR with varying hold periods
    By kmorque in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-02-2009, 03:04 AM
  6. Calculating hours in periods
    By XCESIV in forum Excel General
    Replies: 2
    Last Post: 05-29-2009, 04:03 AM
  7. average rolling periods
    By RobPatrick in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2007, 05:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1