Date Functions in Calculated Fields

Date Functions in Calculated Fields

  • Comments 32

Hi everyone,

This is Amber from the SharePoint Designer Data team. This post walks you through using calculated fields in Windows SharePoint Services (WSS) to show items from the Current month. Lots of posts cover the use of calculated fields, but none specifically address the use of Formulas for dates in a calculated field. At the end of this blog you will have a good understanding about calculated fields with some helpful links.

In this example we have a Service Request List and the end goal is to display all the service requests placed in the current month. The way we will do this is by creating a calculated field that returns something that is filterable.

Step 1:

First, we will create a “Custom list” called “Service Request List” with the following fields:

· Service Request Title (String) - Comes by default

· Request Date (Date)

· Resolution (Date)

· Today (Single line of text)

Now go ahead and add few items in the list with Request Dates with different months.

clip_image002

Step 2:

Now, let’s create a calculated field to evaluate if the initial request date belongs to the current month or not. If it does then it would return Yes, else No.

1. Now click back on list settings option and create a new column

clip_image004

2. Specify a column name (I used OpenedThismonth)

3. For type select “Calculated based on other columns”

4. For the formula I used

=IF(MONTH([Today])=MONTH([Request Date]),"Yes","No")

*Note: While specifying a formula for the Calculated fields you can’t use [Today] expression. To work around this problem we created a Today field in step 1 and then deleted it in step 3 (listed below).

5. Now for Data type returned for this formula select “Single line of text”

6. You can also uncheck “Add to default view” to make sure that this calculated field is not displayed in the new view that you are creating

7. Click OK

Now you have a calculated field “OpenedThisMonth” which specifies weather a Service Request was created in the current month.

clip_image006

Step 3:

In this step lets use the filterable field that we created in step 2 to modify our existing view.

NOTE: Before we create this filter we will first delete the "Today" field so that our calculated field created in step 2 works.

To do so here are the steps:

  • Go to the list settings page
  • In the columns section click on the "Today" column
  • Click Delete, and then OK
  • At this point we should have a view without the "Today" column

1. Now click on Modify View to go to the Edit View page

2. Scroll down to the filter section

3. For the columns value select the column you created in step 2 (OpenedThisMonth)

4. In the comparison dropdown select “is equal to”

5. In the value text box enter “Yes”

clip_image008

6. Click OK

At this point you should have a view showing all Service Requests placed in the current month.

End Result:

clip_image010

Some useful links:

Hope this helps,
Amber

  • All the workarounds fail in one or the other way. So, Microsoft have to add 'today' option for calculation. That would be the only solution.

  • Adding the [Today] column worked great for what I was trying to do.  I am using it to track the progress of projects, and I simply want to know if a project is On Schedule or Late for a report I need for my boss.  The work around worked fine for this purpose.

  • You need to create a text only column and Title it "Today."  Create your calculated column and reference the today.  Once the Today column is deleted Sarepoint treats it as a "Today" variable.

  • Worked great for me.  Thanks Amber:)

  • I've done this by running a designer workflow to touch each item each day, but its a PITA. MS really dropped the ball on date filtering for lists in sharepoint. This is something that really needs addressed if SP wants to be taken seriously out-of-the-box (instead of having a bunch of devs write up a bunch of custom code).

  • This worked great Amber thanks! I don't know what all these other guys are talking about. if they follow the instructions it works...

  • The solution works perfect for me, I had Have to change the separator comma by the semicolom because in my sharepoint have installed Spanish languaje pack and it uses the comma as decimal comma.

    The formula that works in my site is: =IF(MONTH([Today])=MONTH([Request Date]);"Yes";"No")

    Thank you for your post.

  • If you have Problems to Query the Calculated Field read my Blog:

    http://www.schonebeck.net/?p=5

  • This technique DOES work however has the limitation that the "Yes/No" value calculated only at item creation time or when the "today" column is added and subsequently deleted.   You will find that as the current crosses into a new month, the "OpenedThisMonth" column will not be automatically recalculated.

    To make this a viable solution, someone with list admin privileges would need to recreate the fake today column and delete it again manually each month.

  • I don't get why people keep saying "the Today trick works - it just doesn't update"? Doesn't that make it about as useful as a parachute that opens... 5 foot above the ground? ;)

    I've talked the today trick here -

    http://blog.pentalogic.net/2008/11/truth-about-using-today-in-calculated-columns/

    @Brent - the abve also includes some links to workarounds by Dessie such as a batch job  that can automatically add/remove the column to keep it up to date.

    I've also blogged about an alternative method for filtering items from the current/previous calendar month

    http://blog.pentalogic.net/2009/11/howto-filter-items-current-calendar-month-view-sharepoint/

  • I would like to assign icons to the results.  How do I do that?

  • This worked great!! Now I can impress all my friends :-)Thanks for the post.

  • Hey this works perfectly fine ... Great great tip ... Thanks a Lot :)... people please try before commenting on something. i don have words to thank... I have been looking for this sometime

  • Hi This is nice trick, but it has a problem. Many times Today or Modified Date is used in calculated column, shows as 30 Dec 1899. This is known problem and many has faced this issue. Still no one has it's solution. Initially it works perfect but when sites are moved to any other server for production or new template is created with the existing sites. it will be visible. If any one has any solution for dates in calculated column (shows as 30 Dec 1899), please post.

    Thanks,

    Chirantan

  • This works for me, though I had to further customize the formula to include the year. With the current formula given by Amber, the view will show all items for the current month but the year is never filtered. So for the month of August for example, you'll get items from August 2010, August 2009, August 2008, etc.

    This is my formula that worked to also check the year before checking the month.

    =IF(YEAR(Today)=YEAR([Article Date]),IF(MONTH(Today)=MONTH([Article Date]),"YES","NO"), "NO")

Page 2 of 3 (32 items) 123
Leave a Comment
  • Please add 5 and 3 and type the answer here:
  • Post