+ Reply to Thread
Results 1 to 9 of 9

Problem with date range lookups and calculations

  1. #1
    Registered User
    Join Date
    05-16-2008
    Posts
    7

    Problem with date range lookups and calculations

    I have a date calculation problem, which I hope someone can help me with.

    I have a spreadsheet as follows: (this is a simplified example)

    ……...…A……...………….B………………..C
    01…..….date…………..salary….….rolling average
    02….30-01-08………..£2000………….£24,000
    03….28-02-08………..£2000………….£24,000
    04….31-03-08………..£2300………….£25,200
    05….29-04-08………..£2300………….£25,800
    06….30-06-08………..£2400………………?
    etc
    etc
    etc

    I am trying to calculate my rolling average yearly potential salary (ie “how much would I have earned in the last twelve months if I was paid at my current rate for all those preceding months”?), based on amounts entered throughout the year, usually monthly. However, there can be more, or less, than 12 salary payments in a year, as sometimes I am paid weekly and sometimes not at all for a month. (I work as a musician on cruise ships, and I work short contracts ie 3 months on, 1 month off, three months on, 1 month off, etc. All salaries are paid at month end and amounts can be different from month to month. Prior to this employment, I was paid weekly.)

    In the above simplified example, the calculation for C2 is easy, as I just take the monthly payment B2, multiply by 12 and there it is.

    Also easy is row 04 – I add B2+B3+B4, divide by 3 to get a monthly average and then multiply by 12.

    By row 05, we can see that my rolling average is increasing as my salary has increased, which is the figure I want to calculate. However, row 06 shows there was no payment in May, and I negotiated a new contract rate in June, so the calculation I want is much harder. I have to now work out a DAILY RATE, by looking at all earnings in the last 365 days, adding them, dividing by the number of days (there will be less than 365 for at least the first 12 entries) and then multiplying by 365 for the answer. In my example, this would be as follows:

    £11,000 (total earned) / 153 (days between 30-01-08 and 30-06-08) = £71.89
    £71.89 x 365 = £26,241.83

    showing that my rolling average increased, which is good news!

    I therefore need a formula that will look at all dates in column A, select those that are within a year of the most recent entry, calculate a daily rate using the amounts in column B and put the result in column C. I have found the function EDATE which I guess is the one to use (as the operator ‘-12’ will return the date exactly a year earlier), but I’m unclear how to use this for what I want. I think I need to create an array, but how do I specify parameters for the highest and lowest dates in that array? Or perhaps this might have to be done using pivot tables/calculated fields/calculated items/etc, but that is a very alien area to me and I’m totally unsure where to start if such is the case.

    If anyone can offer any assistance or suggestions, then I would be most grateful.

    Many Thanks
    Last edited by miles_muso; 09-27-2009 at 06:44 AM. Reason: make table look nicer!

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Problem with date range lookups and calculations

    Hello miles_muso,

    I'm not sure about your logic, if you earned roughly the same amount as previously on 30th June, but you hadn't been paid for 2 months then wouldn't you expect the average to decrease?

    Although you've earned a total of £11,000 over the period isn't that period longer than 153 days....because you've effectively earned it from the payday before 30-01-08 until 30-06-08. Of course from your table we don't know what that date is......but by the same logic if you count days from 30-01-08 then you have to count earnings from the following payday (for example the pay received on 28-02-08 is your earnings from 31-01-08 (isn't it?)

    On that basis you would still divide by 153 (or 152 I make it) but the amount to be divided is only £9000. Therefore the amount per day is £59.21, making an annual rate of £21,612.

    To get rolling average using that logic you can use this formula in C3 copied down

    =SUM(IF(A$2:A3>MIN(IF(A$2:A3>=EDATE(A3,-12),A$2:A3)),B$2:B3))*365/(A3-MIN(IF(A$2:A3>=EDATE(A3,-12),A$2:A3)))

    confirmed with CTRL+SHIFT+ENTER

    [You'd need to calculate C2 manually as that formula will fail]

    I can't help feeling that there's a simpler way to calculate that but it's too early on a Sunday for me......

  3. #3
    Registered User
    Join Date
    05-16-2008
    Posts
    7

    Re: Problem with date range lookups and calculations

    Thanks for your early reply.

    I understand what you’re saying here, but I don’t think I should be counting from months before or months after: I think that if I measure from pay date to pay date, irrespective of the period that that payment represents, then that still represents a monthly period. The actual spreadsheet dates back to 2005, so moving backward or forward 30 days won’t really have much effect. The earnings in 2005/06 were much less than in 2007/08, so I expect the lesser figures to be filtered out within a few years. True, the calculation may not give me a right now answer, as it may always be a month out, but then if you look at earnings per year for tax purposes (as most people do) then by the same process they’re a month out as well. I suppose it’s the difference between how much you earned in a year and how much you were paid in a year.

    This whole thing came up because someone once asked me how much I earned in a year. Well, because I work 3 month contracts with a month off in-between, I can’t just add the money up: the answer would be different depending when in the year he asked me.

    Anyway, to look at your formula, which is what really interests me:

    =SUM(IF(A$2:A3>MIN(IF(A$2:A3>=EDATE(A3,-12),A$2:A3)),B$2:B3))*365/(A3-MIN(IF(A$2:A3>=EDATE(A3,-12),A$2:A3)))

    I think I understand the use of absolute row A$2. When I copy the formula, it will be A2:A4 in the next row, then A2:A5 in the one after that, and so on up to A2:A9999 (or whatever). But I don’t understand use of “A2:A5>” Is this using some function of the fact that the cells are dates? I understand that you can subtract one date from another to give a difference in days, but how does it work with a range of dates?

    Is there any way you could explain your formula to me in layman’s terms? (I hope you've had your cornflakes by now!!)

  4. #4
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Problem with date range lookups and calculations

    Hello Miles,

    You can use array formulas in Excel to evaluate a whole range where you would normally only evaluate a single cell, so in the formula I suggested, i.e.

    =SUM(IF(A$2:A3>MIN(IF(A$2:A3>=EDATE(A3,-12),A$2:A3)),B$2:B3))*365/(A3-MIN(IF(A$2:A3>=EDATE(A3,-12),A$2:A3)))

    Then this part repeats twice to calculate a constant

    =MIN(IF(A$2:A3>=EDATE(A3,-12),A$2:A3))

    The IF formula looks at the dates in the range A2:A3 and if any are greater than the date 12 months before A3 then it returns those dates (or FALSE if the date is more than 12 months old) so the IF returns an array of either dates or FALSE values, MIN then takes the lowest of those (FALSE is ignored). In this way that part gives you the earliest date within the period you want to evaluate.

    The rest of the formula sums all the amounts paid on dates later than that date (not the same) and divides by the number of days between that date and the date in A3.

    If you look at the formula simplified to show the above as Mindate

    =SUM(IF(A$2:A3>Mindate,B$2:B3))*365/(A3-Mindate)

    You can probably see that the first part is a similar array formula to that discussed above. The dates are evaluated to see if they are greater than Mindate, if they are then the amount in column B is added to the sum, that gives your total amount and then you multiply by 365 and divide by the number of days in the period.

    Note that "array formulas" need to be confirmed with CTRL+SHIFT+ENTER so that curly braces appear around the formula in the formula bar.

    If you want the calculation to work exactly the way you suggested then you can make two very small (but significant) changes like this

    =SUM(IF(A$2:A3>=MIN(IF(A$2:A3>=EDATE(A3,-12),A$2:A3)),B$2:B3))*365/(A3-MIN(IF(A$2:A3>=EDATE(A3,-12),A$2:A3))+1)

    That will give exactly the amount you suggested for the C6 calculation.

    If you look at the attached I used my original formula in column C and the revised one in column D. Note the large differences in the early dates, that's because of the "one month too many" discrepancy that I pointed out.

    In D3 for instance the formula is taking the sum of two months, B2 and B3 but dividing by the days for one month, i.e. the difference in days between A2 and A3. Obviously the discrepancy lessens when the period is greater but even for a year you will be effectively taking 13 months earnings and dividing by 12 months' days I think.
    Attached Files Attached Files
    Last edited by daddylonglegs; 09-27-2009 at 10:25 AM.

  5. #5
    Registered User
    Join Date
    05-16-2008
    Posts
    7

    Re: Problem with date range lookups and calculations

    Hi DaddyLongLegs,

    Thanks for your replies, and your explanations. Clever things, these range chappies!

    NB: I work on a cruise ship and internet access is pathetically slow and expensive and I therefore have to wait til we get on land and I can find a good WIFI signal, hence the delay in this reply...

    Anyway, I pasted your formula into my real spreadsheet, changed a few of the cell references, and hey presto........... it didn't work

    I get a #NAME? error when the calculation is performed. On examining the error it seems not to like the EDATE part, which I find confusing.

    I have attached a copy of the spreadsheet and I'd be very grateful if you could look into the problem for me (NB I've changed all the values - well, you can never be too sure who's browsing forums nowadays....!)

    I'll also download your example and see if I can work out what went wrong in mine.

    Thanks again for all your help so far!

    miles

  6. #6
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Problem with date range lookups and calculations

    Sorry, You mentioned EDATE so I assumed you'd used it.....

    Unless you have Excel 2007 then EDATE is an Analysis ToolPak add-in function. You can enable the add-in like this

    Tools > Add-ins > tick "Analysis ToolPak" box.

    You may need to refresh the formulas after doing that.....

    If that doesn't work for any reason you could replace EDATE with this formula to get the date 12 months back

    =DATE(YEAR(A3)-1,MONTH(A3),DAY(A3))

  7. #7
    Registered User
    Join Date
    05-16-2008
    Posts
    7

    Re: Problem with date range lookups and calculations

    Hi DLL,

    (Hey.....DLL! Was that a deliberate acronym?)

    I am in fact using Excel 2007, and EDATE is an accessible function. I forgot to actually attach the spreadsheet in my last post (DOH!) so here it is now.

    Your help, as ever, etc.......
    Attached Files Attached Files

  8. #8
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Problem with date range lookups and calculations

    The formula works for me....it came up with #NAME? errors when I opened so I simply clicked in the first cell and re-applied CTRL+SHIFT+ENTER and the formula works.

    I assume still it's an EDATE issue as advised, the worksheet you attached isn't an Excel 2007 version, are you sure that's the version you are using?

  9. #9
    Registered User
    Join Date
    05-16-2008
    Posts
    7

    Re: Problem with date range lookups and calculations

    Errr, I'm TOTALLY sure that I'm using Excel 2007, right up to the point where it turned out to be Excel 2002 SP3. Doh!!

    I will use your alternative EDATE replacement and see what happens.

    Thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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