Making Sense of the SharePoint World


Advanced Calculations in SharePoint Designer Workflow

Aug-252009

MCj02380230000[1]

In this two part series, I'll be showing you how to take advantage of SharePoint calculated columns in your SharePoint Designer workflows. Part 1 will introduce calculated columns. Part 2 will show you how to consolidate your columns into a "function library" of sorts.

Part 1 of 2: When Basic Arithmetic Won't Cut It

SharePoint Designer workflows are very powerful, but there are a number of acknowledged limitations. One of these is the fairly limited selection of operations for performing calculations. For numbers, you have a basic selection of arithmetic (add, subtract, multiply, divide, and mod) operations, but no advanced math. For strings, you have the dynamic string builder, but you have no way to break down, or analyze, a string.

One way around this would be to design custom actions to perform the desired manipulation. But that requires Visual Studio, coding skills, and installation on the server by a willing administrator. Fortunately there is another option, already built into SharePoint, that gives you access to a wide array of functions.

Meet the Calculated Column

This option is the "Calculated Column". While this isn't a direct part of a SharePoint Designer workflow, your workflows do have access to the calculated columns in he lists and libraries of your site. If you haven't used a calculated column, this is a feature in SharePoint that allows you to create a column (or field) based on information contained in other fields in your list.

Creating a calculated column is pretty easy. Just select "Create Column", either from the Settings dropdown of a list view, or from the Columns section of your list's settings page. Near the bottom of your Type choices will be "Calculated".

image

When you select Calculated, the Additional Column Settings section is changed to show the Formula builder. In the example shown below, I have two date fields, Start and End. I want to know how many days are between them. All I need to do is enter the formula [End]-[Start], and I now have the duration of my event.

image

Note: After you save a formula, it is reformatted slightly, so if you go back into the field to review it later, you will see "=End-Start". You can also enter the formula directly in this format if you like.

But in addition to being able to display this in my list, I can now use that duration in my workflow to make decisions. For example, if the list is a time-off notification form, I might want to alert HR to arrange temp coverage if the employee is going to be out for more than 3 days:

image

Of course, that was a very simple calculation, and one that could have been done within the workflow. Where the calculated column starts coming into its own is more complex operations.

Let's say our time-off notification list is configured to allow people to email their requests into it. That's simple enough - SharePoint supports email enabling in several list types.

But, what if you want to process the request differently based on information in the subject line, such as whether the person is on vacation or sick. Your convention might be to put extra information in the subject (e.g. "sick - going to the doctor"). The relevant information to the workflow is to the left of the dash, so you need to grab just that. You could create another calculated column, called "Reason". The formula here is a bit more complex:

TRIM(LEFT([Title],FIND("-",[Title])-1))

You can find a complete list of the functions available for calculated columns at either of these links (the list is the same for WSS and MOSS):

http://office.microsoft.com/en-us/sharepointtechnology/CH100650061033.aspx 

http://office.microsoft.com/en-us/sharepointserver/CH101760291033.aspx

There are a couple of limits to be aware of when creating calculated columns:

  • You can only nest functions 8-deep
  • Your total formula length can't exceed around 1000 characters.

A Good Start

In this post, I've shown you how SharePoint calculated columns can help you get around the limited calculation ability built into SharePoint Designer. You can use calculated columns to perform almost unlimited manipulation of your SharePoint data, and make use of that in your workflow. But what if you need many calculations in your workflow, or you don't want your users to see any "interim" values? In Part 2, I'll show you how to build a "function library" that you can call from any SharePoint Designer workflow in your site.

Until then, Happy Computing!

 
Posted by Woody Windischman | 8 Comments | Trackback Url | Bookmark with:        
Tags: Administration, Design, SharePoint Designer, Customization, Workflow, SharePoint

Comments

Thursday, 22 Oct 2009 04:03 by Guru
Hi, I want to know whether we can hide calculated formulas in sharepoint like the way we do in excel.

Thursday, 5 Nov 2009 06:41 by Andrea
Any advice on how to get past the limit of 8 calculated columns in a given list?

Friday, 6 Nov 2009 07:55 by Woody
Hi Andrea, The limit isn't 8 calculated columns (I just created a list with 9, just to be sure). The limit is nesting functions. In other words having one function called inside another, like this: Function1(Function2(Function3(Function4))).

Wednesday, 17 Mar 2010 07:32 by Paul
Hi Woody, I've had problems trying to get my calculated columns to show up in my workflow. I wonder if there are limitations? I wanted to alter the list ID to create reference numbers eg 00001, 00002, so I used a calculated column to do this calculation... a)By creating a column called [ID set], below =IF(FormID<10,"0000",IF(AND(FormID<100,FormID>=10),"000",IF(AND(FormID<1000,FormID>=100),"00",IF(AND(FormID<10000,FormID>=1000),"0","")))) b) Then I created a number column called [Form ID]I created a workflow to run when an item was created to set the FormID to be the ID of the list item. C)Finally the column for the reference number is..called Reference number =[ID set]&FormID This creates the correct reference number which is great. Then I wanted to use this reference number in my workflow by either creating a variable that pulled it in or just simply doing a lookup in an email to send out. Even if I pause the workflow for 5 mins, my workflow errors as soon as I try to add a step that looksup my new ‘reference number’ column

Sunday, 11 Jul 2010 06:17 by Woody
Paul, One thing you might try is to use a second list to host the calculation as I describe in part 2 of this article.

Name:
URL:
Email:
Comments: