+ Reply to Thread
Results 1 to 16 of 16

Non-continuous Averages

  1. #1
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Non-continuous Averages

    Hello,
    Can anyone help me with making some changes to a AVERAGE formula?

    I need a average formula that includes the values of price every four hours over the last five instances,current price included.

    Below is formula I've been working with. I'm thinking adding an Offset or IsNumber might be the best way to go because it'll allow me to skip between the prices and only factor in the prices I want.

    That's the gist of what I'm trying to do, be able to include any combination of numbers in a average formula without the numbers having to be in continuous order.

    Basic Average Formula:
    Please Login or Register  to view this content.

    Please see attached spreadsheet for a better understanding.
    Thanking you for taking the time to read this post and for any and all help you can provide.
    Attached Files Attached Files
    Last edited by artiststevens; 02-24-2011 at 01:26 AM.

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Non-continuous Averages

    Hi Artiststevens,

    I couldn't get INDIRECT to work generating parts of an array, so I used this and it seems to work with your sample data.

    In D19:

    =IF(MOD(HOUR(A19),4)=0,AVERAGE(INDIRECT("B"&ROW()-16),INDIRECT("B"&ROW()-12),INDIRECT("B"&ROW()-8),INDIRECT("B"&ROW()-4),B19),AVERAGE(INDIRECT("B"&ROW()-12-MOD(HOUR(A19),4)),INDIRECT("B"&ROW()-8-MOD(HOUR(A19),4)),INDIRECT("B"&ROW()-4-MOD(HOUR(A19),4)),INDIRECT("B"&ROW()-MOD(HOUR(A19),4)),B19))

  3. #3
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Non-continuous Averages

    Thank you Paul! That's perfect! I have one question though. How would I go about add more values, if I wanted to get the averages of 6 or more numbers? Thank You Again!

  4. #4
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Non-continuous Averages

    Well, you couldn't start averaging 6 numbers in row 19 if your values started in row 3, of course (3, 7, 11, 15, 19). You would just need to add another section to each formula as shown in red below (I think!):

    =IF(MOD(HOUR(A20),4)=0,AVERAGE(INDIRECT("B"&ROW()-20),INDIRECT("B"&ROW()-16),INDIRECT("B"&ROW()-12),INDIRECT("B"&ROW()-8),INDIRECT("B"&ROW()-4),B20),AVERAGE(INDIRECT("B"&ROW()-16-MOD(HOUR(A20),4)),INDIRECT("B"&ROW()-12-MOD(HOUR(A20),4)),INDIRECT("B"&ROW()-8-MOD(HOUR(A20),4)),INDIRECT("B"&ROW()-4-MOD(HOUR(A20),4)),INDIRECT("B"&ROW()-MOD(HOUR(A20),4)),B20))

  5. #5
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Non-continuous Averages

    Thanks Paul! That worked also, I just have to make sure there are enough instances for the formula to compute. I have one more question. On the weekends there is one instance where price shortens to two hours instead of four. I've tried to adjust the formula to accommodate but I can't get it to work.Maybe you can figure out what I'm doing wrong.

    Please see attached spreadsheet.

    Thanks again. You've been a great help.
    Attached Files Attached Files

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Non-continuous Averages

    Hi, I'm in here late but,

    Did you think of simply ctrl clicking on the cells you wanted to average, then give the selected cells a range name. Then do an average using the name?

    See attached.
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  7. #7
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Non-continuous Averages

    The formulas I've provided thus far expect the data to be spaced evenly (it uses the time value in the current row, but once that is calculated it then averages cells that are each 4 rows apart. In your latest sample some are two rows apart while others are four. I could create a similar formula that would take that EXACT setup into account, but if anything changed, or you tried dragging it down even 1 row it would fail. It would be a one cell formula, essentially. It would be easier to just manually type in

    =AVERAGE(B3,B5,B7,B11,B15)

  8. #8
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Non-continuous Averages

    And to Marvin's point - yes, that would make sense in the last example, although it would still be easier to just use the cell addresses in that case. Using names would likely not work in the earlier example as it is more of a rolling average as you drag the formula down.

  9. #9
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Non-continuous Averages

    Thanks for the additional help Marvin. I have alot of data so that would take quite awhile... I was looking for something I could paste and copy down. Paul's formula works but I having trouble with the weekends. I'm attempting to get a better understanding of how the formula works, so that maybe I can get it to work for weekends with the session is shortened. Thanks again for all of the help! Both of you guys.

  10. #10
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Non-continuous Averages

    This sounds like a stock price problem. If you have real dates do you also have symbols?
    I'm really thinking about Pivot Tables. They can do Sums as well as averages and NO FORMULAS at all.

    I also do a water meter spreadsheet. About 170 meters that spit out their readings 4 times a day. I simply suck in the csv data to the bottom of my table, Pivot Table it out and Easy Peasy averages, etc.

    To see if your data works, I'd need a better look at what it really looks like. Better sample?

  11. #11
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Non-continuous Averages

    I'll try to break down my original formula for you so you hopefully understand how it works:

    =IF(MOD(HOUR(A19),4)=0,

    This checks to see if the hour in A19 divided by 4 leaves a remainder of 0. MOD(12,4) equals 0, while MOD(13,4) equals 1 since 13 divided by 4 is "3 remainder 1". So if it's true that this test results in 0, perform the next (TRUE) part of the IF function.
    AVERAGE(INDIRECT("B"&ROW()-16),INDIRECT("B"&ROW()-12),INDIRECT("B"&ROW()-8),INDIRECT("B"&ROW()-4),B19),

    This can be broken down into:

    AVERAGE(
    ....INDIRECT("B"&ROW()-16),
    ....INDIRECT("B"&ROW()-12),
    ....INDIRECT("B"&ROW()-8),
    ....INDIRECT("B"&ROW()-4),
    ....B19,

    The ROW() functions within the formula simply return the current row that the formula resides in. So if this formula is in D19, ROW() returns 19. INDIRECT allows you to combine strings and numbers to create a valid cell, worksheet or workbook reference (amongst other things, probably).

    INDIRECT("B"&ROW()-16) becomes
    INDIRECT("B"&19-16 which becomes
    INDIRECT("B"&3) which becomes
    INDIRECT("B3") which becomes simply B3 (or $B$3).

    The same then applies to the -12, -8, -4 sections, for which you get 19-12 (7), 19-8 (11) and 19-4 (15). This creates cell references B7, B11 and B15. Finally average those four "created" cell references with B19.

    If the result of the IF test was FALSE (the hour is not divisible by 4 with 0 remainder, e.g. 13:00, 14:00, 15:00, 17:00, etc.) then the second AVERAGE function is performed. It is similar to the first, but doesn't have to go back as far since it is the average of the past four major times and the current time. This is why it starts at -12, then -8, -4, etc. It also subtracts the REMAINDER of the hour in the current row divided by 4 to get the correct rows. So when the hour in A20 is 13:00 (13/4 = 12 remainder 1) it finds the earliest row needed in the average by subtracting 20-12-1 (ROW()-12-MOD(HOUR(A20),4)).

    That's a lot to swallow probably.

    AVERAGE(
    ....INDIRECT("B"&ROW()-12-MOD(HOUR(A20),4)),
    ....INDIRECT("B"&ROW()-8-MOD(HOUR(A20),4)),
    ....INDIRECT("B"&ROW()-4-MOD(HOUR(A20),4)),
    ....INDIRECT("B"&ROW()-MOD(HOUR(A20),4))
    ....,B20))

    INDIRECT("B"&ROW()-12-MOD(HOUR(A20),4)), becomes
    INDIRECT("B"&20-12-MOD(13,4)) which becomes
    INDIRECT("B"&20-12-1)) which becomes
    INDIRECT("B"&7)) which becomes
    INDIRECT("B7") which is just B7 (or $B$7)

    Whew.. hopefully that made sense for you

  12. #12
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Non-continuous Averages

    WOW! Thanks Paul! That's quite a breakdown. I have a much better understanding of the formula. Thank both of you gentlemen for all of the help. I have more than enough information to work with. I'm going to mark this as solved and get to work on it. Thank you again for all of your help I really appreciate it.

  13. #13
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Non-continuous Averages

    FWIW, you might find you can adapt the FORECAST approach from your prior thread:

    Please Login or Register  to view this content.
    above is for 5 items obviously and per linked thread we know that the hour values only exist every 4 cells (ie 13:00 etc doesn't actually exist in your real file)

  14. #14
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Non-continuous Averages

    Hi DO,
    Following my instructions of "One Test..." I tried your CSE above on the file. It gave a different Average. I believe Blank cells in A and B give a zero which is averaged in.

    I study your CSEs, hoping someday to make my own. See attached.
    Attached Files Attached Files

  15. #15
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Non-continuous Averages

    As mentioned the CSE assumes consistent spacing - per OP's other thread - this is seemingly not reality here and so the suggestion should be disregarded (my bad)

  16. #16
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Non-continuous Averages

    The below might work for inconsistent gaps:

    Please Login or Register  to view this content.
    the 4 in the LARGE represents n-1 where n is data points required (current record is always included so is removed from n)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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