Excel Challenge #10

Discussion in 'Off-topic' started by Steve Hales, Nov 13, 2015.

  1. Steve Hales

    Steve Hales ActEd Tutor Staff Member

    Got Excel skills? Let's see.

    Support seems to be waning a little for these challenges, so next month (the Christmas Special!) will be the last Excel Challenge for a while. This month's challenge is a genuine problem that I need solving, and the winning solution will find its way into the real world :)

    Here are the usual rules.
    • Create a single Excel formula to solve the stated problem.
    • The shortest formula wins (when calculating the length of the formula there's no need to include the leading "=", or the curly braces that Excel uses to represent array formulae).
    • The formula's number format can be set to whatever you want.
    • Conditional formatting is not allowed.
    Problem #10

    Assume that cell A1 contains the month number and A2 contains the year number of any date. The challenge is to create a single formula to return the day number of the first Monday in that month.

    A1 A2
    6 1955 --> 6
    1 2015 --> 5
    9 2110 --> 1
     
  2. Oxymoron

    Oxymoron Ton up Member

    OK, this time even I can do this in excel:
    =8-WEEKDAY(VALUE(CONCATENATE(A1,"-1-",A2)),12)
     
    Last edited: Nov 13, 2015
  3. pma99car

    pma99car Keen member

    slightly shorter:

    =MOD(10-MOD(DATE(A2,A1,1)+1,7),7)+1

    Sad to hear that these will stop. I normally don't contribute as great solutions are posted so quickly!
     
    Last edited: Nov 13, 2015
  4. Muppet

    Muppet Member

    Merge the two to get: =8-WEEKDAY(DATE(B1,A1,1),12)
     
  5. pma99car

    pma99car Keen member

    A slight edit of this gives an even shorter solution:

    =8-WEEKDAY(DATE(A2,A1,1),12)

    Edit: Muppet beat me to it!
     
  6. Hemant Rupani

    Hemant Rupani Senior Member

    Muppet! pma99car! What a co-accident! :p

    I just shorten it to
    Code:
    =8-WEEKDAY(DATE(A2,A1,),2)
     
  7. Hemant Rupani

    Hemant Rupani Senior Member

    :D So, the count down to 23 :cool:
    Code:
    8-MOD(DATE(A2,A1,)-1,7)
     
  8. Steve Hales

    Steve Hales ActEd Tutor Staff Member

    Not using the last argument of the DATE function does open up some interesting possibilities, and this solution would be excellent, if only it worked :D Unfortunately it gives an answer of Monday 8th May 2000, which of course should be Monday 1st May 2000.

    pma99car has worked out a fix for this, but it requires another MOD function. I'm still hoping for a compact MOD answer :)
     
  9. Bodhisattva

    Bodhisattva Member

    I haven't fully checked this, but wouldn't

    =7-MOD(DATE(B5,A5,-2),7)

    fix this?
     
  10. Steve Hales

    Steve Hales ActEd Tutor Staff Member

    Awesome :D That's definitely the sort of compact MOD answer I was after!

    Maybe you could explain how it works? The -2 in the DATE function returns the antepenultimate day of the preceding month, but it's not immediately obvious how that helps :confused:
     
  11. Bodhisattva

    Bodhisattva Member

    What, and spoil the magic? :p

    Firstly, as numbers, dates that are congruent to 0 mod 7 are Saturdays in Excel. This goes up by one as we advance through the days of the week, and 'ticks over' from Friday to Saturday.

    Secondly, if the first day of the month is a Tuesday, then the first Monday of the month is the 7th. This date goes down by one as we advance through the days of the week, and 'ticks over' from Monday to Tuesday.

    Therefore we need to move the date from the first of the month, so that the 'tick over' occurs from Friday to Saturday. We can do this by counting back three days from the first of the month. This would be

    Code:
    DATE(A2,A1,1)-3
    which is the same as

    Code:
    DATE(A2,A1,1-3)
    i.e.

    Code:
    DATE(A2,A1,-2)
    The range is going down, so we need the minus sign, and our answers mod 7 are in the range [-6,0], so we need to add 7 to get the range [1,7].

    I've explained this very badly - it's much easier with a table, but I don't have time.

    Of course, you could go forward a week and get the same answer, so

    Code:
    7-MOD(DATE(A2,A1,5),7)
    should also work, and is one character shorter.
     
  12. Steve Hales

    Steve Hales ActEd Tutor Staff Member

    That's excellent, thanks. It's still magical ;)
     
  13. Steve Hales

    Steve Hales ActEd Tutor Staff Member

    Watch out for Excel Challenge #11 (the Christmas Special) - launching Friday 11th December 2015.
     

Share This Page