1,804 articles and 14,860 comments as of Thursday, May 19th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Friday, May 29, 2009

Taming the Elusive “Calculated Column” – Date and Time – Part 2

Deciphering the Puzzle (v1.0)

Back to our formula, lets convert it into “plain English” (or close to, at least) to see just what’s occurring in the logic being processed:

The formula:

=IF(AND((WEEKDAY([End Date],2))<(WEEKDAY([Start Date],2)),((WEEKDAY([Start Date],2))-(WEEKDAY([End Date],2)))>1),(((DATEDIF([Start Date],[End Date],"D")+1))-(FLOOR((DATEDIF([Start Date],[End Date],"D")+1)/7,1)*2)-2),(((DATEDIF([Start Date],[End Date],"D")+1))-(FLOOR((DATEDIF([Start Date],[End Date],"D")+1)/7,1)*2)))

The (albeit somewhat confusing) translation:

If the number of the day of the week in the “End Date” column (with “1” equaling “Monday” through “7” equaling “Sunday”) is less than the number of the day of the week in the “Start Date” column (with “1” equaling “Monday” through “7” equaling “Sunday”), AND the number of the day of the week in the “Start Date” column (with “1” equaling “Monday” through “7” equaling “Sunday”) minus the number of the day of the week in the “End Date” column (with “1” equaling “Monday” through “7” equaling “Sunday”) is greater than 1, count the number of days that separate the “Start Date” from the “End Date” plus 1, minus the number of days that separate the “Start Date” from the “End Date” plus 1 divided by 7 and rounded down to the nearest 1 number and multiplied by 2 with 2 subtracted from the result.  If the number of the day of the week in the “End Date” column (with “1” equaling “Monday” through “7” equaling “Sunday”) is less than the number of the day of the week in the “Start Date” column (with “1” equaling “Monday” through “7” equaling “Sunday”), AND the number of the day of the week in the “Start Date” column (with “1” equaling “Monday” through “7” equaling “Sunday”) minus the number of the day of the week in the “End Date” column (with “1” equaling “Monday” through “7” equaling “Sunday”) is not greater than 1, count the number of days that separate the “Start Date” from the “End Date” plus 1, minus the number of days that separate the “Start Date” from the “End Date” plus 1 divided by 7 rounded down to the nearest 1 multiplied by 2, and display the result.

Whew!!!  That was a mouthful (told you it’d be somewhat confusing).

Let’s try and simplify this some to help clear it up.

We know we’re using the “Return_type”  of 2 for each and every “WEEKDAY” function (resulting in “Monday” equaling “1” through “Sunday” equaling “7), so we can skip mentioning it each time in the paragraph.

We also know what our question is, so we don’t have to repeat it in the “FALSE” side of the “IF”.

Since we also now know how the “WEEKDAY”, “DATEDIF”, and “FLOOR” functions work, we don’t need to expand each of them out, so we’ll just replace the long-explanation of each one with their actual formula instead.

Stripping out each of these three pieces should make it clearer to understand (no guarantees at this point).

Deciphering the Puzzle (v1.1)

Our question:

Part 1: If WEEKDAY(“End Date”,2) is less than WEEKDAY(“Start Date”)
Part 2: If WEEKDAY(“Start Date”,2) plus one, minus WEEKDAY(“End Date”,2) is greater than one

Result if both parts of the question are “TRUE”:

Take the DATEDIF(“Start Date”,”End Date”,”D”) plus 1, then subtract the FLOOR value (to the nearest “1”) divided by 7 of the DATEDIF(“Start Date”,”End Date”,”D”) plus 1 multiplied by 2, then subtract 2 from the result.

Result if either or both of the pieces of the question are “FALSE”:

Take the DATEDIF(“Start Date”,”End Date”,”D”) plus 1, then subtract the FLOOR value (to the nearest “1”) divided by 7 of the DATEDIF(“Start Date”,”End Date”,”D”) plus 1 multiplied by 2.

Clear as mud yet?  Let’s try and make it a little simpler.

Our question is just an “IF” with two parts that must both result in “TRUE” for the entire question to be “TRUE” (an “AND” function).  We’re using the “WEEKDAY” function to determine the day of the week for both the “Start Date” and “End Date” along with some simple math to determine if the “End Date” occurred later than the “Start Date”.

Probably the best approach to really be able to see just what’s going on is to plug in a few test dates and run through the calculations, which is what we’ll begin with next time.

Till then…

- Dessie

Dessie LunsfordDessie Lunsford
Points-of-Sharing

View all entries in this series: Dessie Lunsford-Logic Formulas»
Entries in this series:
  1. Taming the Elusive "Calculated Column"
  2. EndUserSharePoint.com: Taming the Elusive "Calculated Column" - Customizing a Task List
  3. EndUserSharePoint.com: Taming the Elusive “Calculated Column” - Customizing a Task List (Part II)
  4. EndUserSharePoint.com: Taming the Elusive “Calculated Column” - Customizing a Task List (Part III)
  5. EndUserSharePoint.com: Taming the Elusive “Calculated Column” - Customizing a Task List (Part IV)
  6. EndUserSharePoint.com: Taming the Elusive “Calculated Column” - Text and Data
  7. EndUserSharePoint.com: Taming the Elusive "Calculated Column" - Customizing a Contacts List (Part I)
  8. EndUserSharePoint.com: Taming the Elusive "Calculated Column" - Customizing a Contacts List (Part II-I)
  9. EndUserSharePoint.com: Taming the Elusive “Calculated Column” - Customizing a Contacts List (Part II-II)
  10. EndUserSharePoint.com: Taming the Elusive “Calculated Column” - Customizing a Contacts List (Part II-III)
  11. EndUserSharePoint.com: Taming the Elusive “Calculated Column” – Text and Data (Part III)
  12. EndUserSharePoint.com: Taming the Elusive “Calculated Column” – Text and Data (Part IV)
  13. EndUserSharePoint.com: Taming the Elusive “Calculated Column” – Text and Data (Part V)
  14. EndUserSharePoint.com: Taming the Elusive “Calculated Column” – Text and Data (Part VI)
  15. Taming the Elusive “Calculated Column” – Lookup and Reference
  16. Taming the Elusive “Calculated Column” - Logic
  17. Taming the Elusive “Calculated Column” - Logic - Part 1
  18. Taming the Elusive “Calculated Column” - Logic - Part 2
  19. Taming the Elusive “Calculated Column” - Logic - Part 3
  20. Taming the Elusive “Calculated Column” - Logic - Part 4
  21. Taming the Elusive “Calculated Column” - Logic - Part 5
  22. Taming the Elusive “Calculated Column” - Logic - Part 7
  23. Taming the Elusive “Calculated Column” - Logic - Part 6
  24. Taming the Elusive “Calculated Column” - Logic - Part 8
  25. Taming the Elusive “Calculated Column” - Logic - Part 9
  26. Taming the Elusive “Calculated Column” - Logic - Part 10
  27. Taming the Elusive “Calculated Column” - Date and Time - Part 1
  28. Taming the Elusive “Calculated Column” - Date and Time - Part 2
  29. Taming the Elusive “Calculated Column” - Date and Time - Part 3
  30. Taming the Elusive “Calculated Column” - Date and Time - Part 4
  31. Taming the Elusive “Calculated Column” - Referencing Multiple Lines of Text Column
  32. Taming the Elusive “Calculated Column” – Date and Time – Part 5
  33. Taming the Elusive “Calculated Column” – Date and Time – Part 6
  34. Taming the Elusive “Calculated Column” – Date and Time – Part 7
  35. Taming the Elusive "Calculated Column" - Referencing a Multiple Lines of Text Column With Append Text: Part 1
  36. Taming the Elusive "Calculated Column" - Referencing a Multiple Lines of Text Column With Append Text: Part 2
  37. Taming the Elusive "Calculated Column" - Referencing a Multiple Lines of Text Column With Append Text: Part 3
 

Please Join the Discussion

One Response to “Taming the Elusive “Calculated Column” – Date and Time – Part 2”
  1. santhi swaroop naik says:

    hi hw r u i like ur articles

    i have some doubts regarding calculated column

    in announcement i created a column” Publish date”

    again i created one more column “Expiry date”

    now here i want condition between these two columns like

    if “end user ” gives Publish date future date and expiry date as present then it has to raise error
    and i have to set it should greater than publish date

    how please send me the solution


Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!