+ Reply to Thread
Results 1 to 6 of 6

Excel 2007 : Averaging across rows based on some criteria

  1. #1
    Registered User
    Join Date
    05-13-2010
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    14

    Averaging across rows based on some criteria

    I have an excel file with over 500,000 rows, each representing monthly temperature data for a particular weather station and year. I would like to find the average temperature and accompanying standard deviation across all available years for each individual weather station and each month. However, I only need to do this for a small portion of the weather stations. In the attached shortened example of my data the second sheet lists the weather stations for which I need averages and standard deviations.

    Short of manually selecting all the rows I need for each location, is there a macro or function that can average across all appropriate rows in the first sheet for each row in the second sheet?
    Attached Files Attached Files

  2. #2
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Averaging across rows based on some criteria

    Can you explain the data structure a bit more? There seem to be an awful lot of duplicates.

    Can you provide a mocked up example of a few results?

    On Sheet1, you have a code for Country. On Sheet2 you have country names. Several countries seem to have the same Temp station number (Belgium, Netherlands).
    When there are several rows for each country, with the same temp station, which data should be used for the average?

    It can be done with formulas, but there needs to be a logic for the formula to find the data in the first place.

  3. #3
    Registered User
    Join Date
    05-13-2010
    Location
    Knoxville, TN
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Averaging across rows based on some criteria

    I have updated the excel file with a bit more information to help aid in answering some of your questions. There are duplicates on Sheet 2 because this sheet represents locations of populations (both male and female), many of which are located close to the same weather station. The country column in Sheet 2 refers not to the country where the temp station is located but the country where the population is located. It can be ignored; I was simply using it to orient myself.

    Countries like Belgium and the Netherlands have the same temperature station because there are no stations in Belgium, and the station in the Netherlands is the closest weather station to the Belgian population.

    Therefore, all that is needed is matching between weather station numbers in Sheet 2 and Sheet 1. The weather station numbers are unique (irrespective of country code), so country should not need to be factored into the formula.

    I appreciate you taking a look at the data! I provided some example formulas that I would have to do by hand. Hopefully, this is a little more straight forward...
    Attached Files Attached Files

  4. #4
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Averaging across rows based on some criteria

    Well,

    one problem is that your temp station codes in the two sheets are not the same.

    If they were, you could do a simple AVERAGEIF() like

    =AVERAGEIF(Sheet1!C:C,Sheet2!E10,Sheet1!E:E)

    But in Sheet1, the temp station is text, with a leading zero, for example

    028970000

    In Sheet2, though, the same temp station is a number, with no leading zero and only three zeros at the end, like

    2897000

    Averageif can't manage that. A workaround could be:

    =SUMPRODUCT(--(Sheet1!C:C=TEXT(Sheet2!E2,"00000000")&"0"),Sheet1!E:E)/SUMPRODUCT(--(Sheet1!C:C=TEXT(Sheet2!E2,"00000000")&"0"))

    but that is assuming that there will always be a leading and a trailing zero.

    If this assumption is not true, you will need to adjust your temp station codes to be the same on each sheet and go with the Averageif() version.

    cheers

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

    Re: Averaging across rows based on some criteria

    Quote Originally Posted by teylyn View Post
    ...in Sheet1, the temp station is text, with a leading zero, for example

    028970000

    In Sheet2, though, the same temp station is a number, with no leading zero and only three zeros at the end, like

    2897000

    Averageif can't manage that.
    =AVERAGEIF(Sheet1!$C:$C,TEXT($E10,"00000000")&"0",Sheet1!E:E)

    (you can't manipulate the source values, you can manipulate the criteria however you wish)

    Your point regards consistency of temp_station convention does hold true however.

  6. #6
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Averaging across rows based on some criteria

    You're right, DO (as expected). When I ran the Averageif, I had not even realised there was a trailing 0 ....

+ 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