End User SharePoint » Calculated Column http://www.endusersharepoint.com Follow me on Twitter: @eusp Sat, 26 Sep 2009 04:34:03 +0000 http://wordpress.org/?v=2.8.4 en hourly 1 Calculated Column Formula Tips – Aggregating Calculations Over 1000 http://www.endusersharepoint.com/2009/08/06/calculated-column-formula-tips-aggregating-calculations-over-1000/ http://www.endusersharepoint.com/2009/08/06/calculated-column-formula-tips-aggregating-calculations-over-1000/#comments Thu, 06 Aug 2009 18:56:31 +0000 Jim Bob Howard http://www.endusersharepoint.com/?p=1871 Contributing Author: Jim Bob Howard

Congratulations, Mark, on the posting of the 1000th article on EndUserSharePoint.com! Thanks for letting me be a part of it. In honor of passing that milestone, maybe this will come in handy for you as you continuing calculating the number of articles here. —Jim Bob

Aggregating data with a DVWP and a Linked Data Source can really save you lots of time and make your SharePoint lists do some great online reporting.

I was recently working on a Mileage Reimbursement application and was able to use some tricks I learned from Laura Rogers in her screen cast, 4 Minute Screencast: Join View of SharePoint Lists, to link data from different lists. Her techniques gave me the direction I needed to display all of the mileage records for a given employee for a given month, as well as show aggregated totals based on the data brought in through the linked data source.

Calculated Column Formula Tips
Figure 1 – Data Form Web Part, plus a Data View Web Part

In Figure 1, two of the fields in the Data Form Web Part come from a Linked Data source, as shown in Figure 2.

Calculated Column Formula Tips
Figure 2 – Data Form Web Part – XSLT aggregation

The DVWP also has some aggregated fields that sum up Calculated Columns, as show in Figure 3.

Calculated Column Formula Tips
Figure 3 – Data View Web Part – XSLT aggregation

But, if you are aggregating Calculated Columns—using XSLT calculations on columns that are SharePoint Calculated Columns—you may have noticed, like in Figure 4, that your data disappears from your form when the calculated value goes over 1,000.

Calculated Column Formula Tips
Figure 4 – Calculated column data disappears

Notice, though—in Figure 5, that the data is actually calculated correctly. It’s just not displayed correctly in the DVWP row, nor is XSLT able to perform the summation of that column.

Calculated Column Formula Tips
Figure 5 – Calculated Column functioning and displaying properly

Why 1,000?

The answer is in the answer. That’s right, it’s the comma.

The Calculated Column is stored as a string (Single Line of Text). SharePoint formulas will automatically cast a string that "looks like a number" into a number, when necessary, for a given formula that requires numbers. Let me explain.

Consider the following situation
X = 100
Y = 6X

X is a string with a value of "100" and Y is a number with a value of 6 times X. The formula (6*[X]) will convert the string "100" to the number 100, multiply it by 6 and return 600, storing it either as a string or a number depending on how Y is defined.

But a Calculated Column which results in a value of "1,000" no longer "looks like a number" because of the comma. So, if X has a value of "1,000", Y will not equal 6000. Further XSLT cannot cast a non-number-looking string to a number, either.

To make matters worse, the Calculated Column doesn’t give you the option of turning off the comma.

Calculated Column Formula Tips
Figure 6 – Typical Calculated Column definition

So, what do you do?

Formula Tip – FIXED

Since you are already working with a Calculated Column, simply add another formula to make sure the number stays a number. Here’s the description for just thing you need.


Calculated Column Formula Tips
Figure 7 – The FIXED function

That last parameter—no_commas—is what is going to save us. The default for this parameter is FALSE, meaning: "No, don’t show no commas." Nice, huh? TRUE, on the other hand, says: "Yes, we want no commas."

Remember, that even though this function is used to "convert" our number to text, that’s OK, because the Calculated Column is going to be text anyway. What we want to ensure is that the text "looks like a number," meaning, no commas.

Let’s try this out on our Calculated Column, Trip Miles, by wrapping our calculation in this new formula:

=FIXED([Ending Odometer]-[Beginning Odometer],1,TRUE)

Since we want to have only one decimal place, the decimals parameter is set to 1. But, since we don’t want commas, we’ll set no_commas to TRUE.

Is that all? Yes, that’s all.

The Result

Now that our text field looks like a number, the DVWP can treat it like one when doing calculations or formatting. See Figure 8 to see how it looks now.

Calculated Column Formula Tips
Figure 8 – DFWP and DVWP both now properly display Calculated Columns with values 1000 or higher

Jim Bob HowardContributing Author: Jim Bob Howard
Jim Bob Howard is a web designer / web master in the healthcare industry. He has been working with SharePoint only since March 2009 and enjoys sharing what he has learned. You can email him at [email protected].

]]>
http://www.endusersharepoint.com/2009/08/06/calculated-column-formula-tips-aggregating-calculations-over-1000/feed/ 8
SharePoint Multi-Media Center: Dynamically generate a URL string http://www.endusersharepoint.com/2009/07/09/sharepoint-multi-media-center-dynamically-generate-a-url-string/ http://www.endusersharepoint.com/2009/07/09/sharepoint-multi-media-center-dynamically-generate-a-url-string/#comments Thu, 09 Jul 2009 18:41:05 +0000 EndUserSharePoint http://www.endusersharepoint.com/?p=1810 One of the annoyances of trying to use a file name in a calculated column is that the “Name” column can not be used in a calculation. This limits the ability to create a dynamically generated link when trying to trigger LyteBox or build a URL string.

In this screencast, I’ll show a manual way to handle this when entering the metadata information for an item. Tomorrow, I’ll show a technique for using workflow to generate the column entry so there will be less chance of error.

Create a SharePoint Multi-Media Resource Center” is a live, hands-on workshop for those needing a media center on their site. Please join us for the next scheduled workshop. You can read more information about the content of the workshop or register below.

Embed this screencast on your site.

<object name="Video" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject"  width="528" height="429"> <param name="url" value="http://content.screencast.com/users/EndUserSharePoint/folders/SharePoint%20-%20Workshop%20Demos/media/417d3382-4aef-46f7-a0b3-7200b3e9544f/2009-07-21-MediaCenter.wmv"></param> <param name="AutoStart" value="0"></param> <param name="ShowControls" value="true"></param> <param name="uiMode" value="full"></param> <param name="playCount" value="1"></param> <param name="CurrentPosition" value="0"></param> <embed name="Video" type="application/x-mplayer2" src="http://content.screencast.com/users/EndUserSharePoint/folders/SharePoint%20-%20Workshop%20Demos/media/417d3382-4aef-46f7-a0b3-7200b3e9544f/2009-07-21-MediaCenter.wmv" autoStart="0" showcontrols="1" uimode="full" playcount="1" currentposition="0" width="528" height="429"></embed> </object>

Code from Calculated Column

="<DIV><a href='../"&[File Name]&"' rel='lyteframe' title='EndUserSharePoint.com' rev='width"&":"&"900px"&";"&" height"&":"&"500px"&";"&"' ><img src='../../ResourcesMediaCenter/playbtn-icon.gif' border='0' width='24' height='24' alt='Click to View the Screencast' /></a></DIV>"


]]>
http://www.endusersharepoint.com/2009/07/09/sharepoint-multi-media-center-dynamically-generate-a-url-string/feed/ 6
Taming the Elusive “Calculated Column” – Referencing Multiple Lines of Text Column http://www.endusersharepoint.com/2009/06/17/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-referencing-multiple-lines-of-text-column/ http://www.endusersharepoint.com/2009/06/17/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-referencing-multiple-lines-of-text-column/#comments Wed, 17 Jun 2009 15:00:55 +0000 Dessie Lunsford http://www.endusersharepoint.com/?p=1751 One of the more frustrating (albeit understandable) elements of working with calculated columns is the restrictions on what column types can be referenced in your formulas.  What it all boils down to is that you can only reference fields that have a return type of “Text” and these must be in the form of a single value (i.e. “Single line of text”, “Number”, “date/Time”, etc.).  What you can’t reference is things that can have more than one value or more than one line (“Lookups”, “Multiple Select” choice fields, “Multiple lines of text”, etc.)…or can you?

Overview

What I’m going to show you is how to reference a “Multiple lines of text” column in a formula, then truncate it to 300 characters in order to have it display as a “Summary” field (thanks to a question asked by “ebrown” on the STP forums).

For this example, I’ll just be using a custom list with the default “Title” column.

Referencing Multiple Lines

The Setup

Since we can’t “directly” reference a “Multiple lines of text” column with a calculated column formula, we need to use the trick of creating a “Temporary” column first in order to make the reference, then delete/recreate the column with the appropriate data type – this will “Fool” the system into thinking it has a good reference and allow it to pass through without throwing up an error.

To do this, create a new column called “Description” and leave all of its settings as default (leave it as a “Single line of text”).

Referencing Multiple Lines

Create another column called “DescriptionCalc”, make it a “Calculated” type and enter in the following formula:

=[Description]

This does nothing more than make it so the calculated column will equal the text column (as in, display whatever is in the text column).

Immediately after creating the calculated column (while still in the “List Settings” page), delete the “Description” column.

As soon as it’s deleted, immediately go back and recreate it, but this time make it a “Multiple lines of text” type…and make sure that you use the exact same name (“Description”) for the column.

This will now give us our starting columns that will include a calculated column with a reference to a type of column it would normally not be able to have.

Referencing Multiple Lines

To test things as they are (to make sure everything is now working), create a new item on the list and add in a few lines, or paragraphs into the text field.  On saving, we should see the following:

Referencing Multiple Lines

So far so good, but notice the added “<div>” tags surrounding the text displayed in the calculated column?  This is just one of those things that SharePoint does in order to render content in the different fields, and since a calculated column is a “text-only” field, it renders the content of the “Description” field as literal text and not html.

So, we’re partly there, but we need a way to remove the extra tags added in the text so it appears exactly the same as the original column. 

Adding the Script

Once again, we’re going to be using the “TextToHTML” script created by Christophe at http://www.pathtosharepoint.com in order to take the literal text displayed, and make it html instead.

On the view page for our list, add in a CEWP (“Content Editor Web Part”), drag it down below the list web part, then open its source editor and paste in the following (*Note – I’ve stripped down the script from Christophe’s original version in order to meet the immediate needs for this list):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<script type="text/javascript">
  var theTDs = document.getElementsByTagName("TD");
  var i=0;
  var TDContent = " ";
  while (i <theTDs.length) 
  {
 try 
{
TDContent = theTDs[i].innerText ||  theTDs[i].textContent;
if  (TDContent.indexOf("<div") == 0) 
{
theTDs[i].innerHTML = TDContent;
}
}
catch(err){}
i=i+1;
}
</script>

The script itself, simply finds the “TD” (table cell) tags on the page that contain the text “<div”, and if so, takes it and modifies the “TD” tag’s “inner html” property to use the text instead.  By doing this, the tags themselves are not rendered on the page, but rather rendered as full html tags.  Also, for those of you already familiar with this script, notice how I’m searching for “<div” and not “<div>”.  I’m doing it this way in order to be able to use this script with “Enhanced rich text” as well as the standard “Rich text” and “Plain text” options of the “Multiple lines of text” field type.  When using the “Enhanced rich text” option, the opening “div” tag will also contain a “class” attribute before its closing “>” part.  By searching for just the opening part of the div (“<div”), we can trap each formatting type.

Now we have the calculated column referencing a column it shouldn’t be and displaying its text as html.

Referencing Multiple Lines

The Summary View

The next step is to “Truncate” or trim-down the text being displayed so we can have a “Summary” view of the content in the “Description” field.
We could accomplish this by more script if we wanted, but in order to make this as simple as it can be, we’ll want to use another calculated column that will display a portion of the “DescriptionCalc” column.

First though, you may be asking “Why don’t we just modify the formula in the “DescriptionCalc” column to trim down what it’s displaying?” 

We could do this, but the problem is that since we’ve fooled the system into allowing us to reference a “Multiple lines of text” field, if we were to modify and attempt to save our original formula, it would try and reconnect, or re-reference the “Description” column and would now throw an error since it’s a type that we can’t reference.  To make it work, we’d have to delete the “Description” column and recreate it as a “single line of text” type, update our formula in the calculated column, the delete/recreate the “Description column again as a “Multiple lines of text” type…and we’d have to do this each and every time we wanted to make a change (a hassle in itself, but also dangerous in that it would remove any and all data we have in the “Description” column each time – no good).

The workaround to this is to create yet another calculated column, but this time make a reference to the first calculated column and use it to create our formula for trimming down the content we’ll display.  Since we won’t be “directly” referencing a non-allowed field type, we can freely modify the formula as much as needed and never run into problems when saving (gotta love workarounds to workarounds).

So, create a new column called “Summary”, make it a “Calculated” type and enter in the following formula:

=LEFT([DescriptionCalc],300)&"…"

This formula simply starts at the far left of the text in displayed in the “DescriptionCalc” column and grabs the first 300 characters, then adds a 3 ellipse set at the end (I used “300” arbitrarily for this- in reality, you can use whatever value you want since the end result will be rendered html and not “text”).

On saving, we’ll see a trimmed down “Summary” display of the “Description” content, along with the other columns (which takes up a lot of space) so to clean up a bit, remove the “DescriptionCalc” column from the view:

Referencing Multiple Lines

Another neat test is to go back in and change the “Type of text to allow” in the “Description” field to “Enhanced rich text” and add in an image and various formatting elements:

Referencing Multiple Lines

I did have to bump up the number of characters in the “summary” formula to 400 for this (all the extra formatting adds a lot of characters to the html), but the idea is still the same – start at the left of the text displayed and grab the defined number of characters, then display just that part.

Conclusion

Just because the system tells you that you can’t do it, doesn’t always mean that it can’t be done…we just have to get “creative” in how we get around some of the limitations, and that’s where it can get fun.

Till next time…

- Dessie

Dessie LunsfordDessie Lunsford
Points-of-Sharing

]]>
http://www.endusersharepoint.com/2009/06/17/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-referencing-multiple-lines-of-text-column/feed/ 11
How to replace the Learning Management System with SharePoint http://www.endusersharepoint.com/2009/06/09/how-to-replace-the-learning-management-system-with-sharepoint/ http://www.endusersharepoint.com/2009/06/09/how-to-replace-the-learning-management-system-with-sharepoint/#comments Tue, 09 Jun 2009 16:07:37 +0000 Michael Hinckley http://www.endusersharepoint.com/?p=1739 One of the main reasons I chose to dedicate so much of my professional time on SharePoint is because it gave me the possibility to own the very site where I post and work. As a knowledge manager and trainer I have the constant need to keep materials updated. I also need to keep my end user engaged. Working within the constraints of enterprise learning and publishing structures means you have to send materials out to teams that then in turn publish the materials out, not always swiftly.

Many times my materials are updated multiple times throughout a week. The teams that support do not understand the dynamic nature of market fluctuations and extensions from the FED that get updated through out the day.  To them you post a course, then after a year or so you update. My SharePoint sites understand this and are indeed built to handle it.

In my Case Study post I described how I set up my site to give me the proprietorship that I needed but I still needed a key component, the quiz.

Now, I am not pretending to fully replace and substitute the advantages of reporting, scheduling and class creation that Learning Management Systems provide. My case was to gain more ownership to allow my goals to drive the site not the over all corporate standards. In my case, there is not such a great need to couple quick assessments with corporate compliance training.

Enough of the disclaimer, back to the quiz…

Harping again on ownership, without SharePoint Designer this quizzing would not be possible. So let’s set a simple quiz up to illustrate.

You’ll need to:

First, set up the list columns

I created a list that has a Student Name, single line of text. Then I set up a column for each of the questions (I named mine Q1, Q2, etc). I chose multiple choices and left the default blank. I then created a number column for each question to contain its score, (A1, A2, etc.). For both of these you can continue on till you reach the number of questions you want. I only need short quizzes, so I have no designs to make them with over 10 questions.

Next I create a Calculated Colum to collect the scores and calculate the average for the final score. Simply put for a 2 question quiz I used this formula. =[A1]+[A2]/2. And finally a Ready to Send Column at Yes/No.

So for a 2 question quiz I have a total of 7 columns (1 for the student’s name, 1 each for Q 1 & Q 2, 1 each for A1 & A2, and one for the total score as well as Ready to Send.)

Once more into the SPD trenches, for this simple 2 question quiz I have 4 Steps

Step 1 is for the first question and I programmed like this, one for the correct answer and one for the wrong answer.

CONDITION: If Q1 equals the (correct answer)
ACTION:  Set A1 to (numeric points you want to give for each question)

CONDITION: Else if             If Q1 not equals the (correct answer)
ACTION:  Set A1 to 0 (numeric points you want to give for each question)

Step 2 replicates Step 1 except for replacing the Q.

Step 3 is my stop. The Ready to Send column is my bouncer. It only lets the quizzes that finished out the door.

CONDITION: If Total Score is not empty
ACTION:  Set Ready to Send to Yes

Step 4 Gathers the total score and sends out an email.

CONDITION: If Ready to Send not equals No
ACTION:  Pause for 0 days, 0 hours, 1 minutes
                        Email Quiz results.

I put a pause in because the calculation was taking too long, so the email went out with 0’s. The pause lets the total score calculate and populate the email with the proper score.

NOTE: The more questions you have the more Steps like Step 1 you’ll have. Also remember to make sure the calculation in the Total score column reflects this.

Make sure to test your work flow in the list to make sure it all works. Once you are certain it is down, you can move on to make the interface a bit better.

I have noticed that End Users really get confused when populating lists and even more so when there are extras items that they need not worry about. In our example they are going to get Scores and Ready to send as extra inputs. So to get rid of these and make the interface more familiar we are going to publish the quiz as a data view web part.

Build a Quiz Page

In SharePoint Create a New Web Parts Page and put it in a library of your choice. Then open it in SharePoint Designer. Click into one of the web parts and then click Data View/Insert Data View.

Click on the list you created for your quiz and click the down arrow to Show Data. Hold the CTRL button down and select only Student Name field and the Questions. Click on the Insert Selected Fields button and select Single Item Form.

This will populate the Web Part with only the Student’s name input and all the questions on your quiz without showing the previous quizzes taken or the extra list columns. Save the page in SPD and launch your web part page to test it.

Again this is not a perfect replacement of LMSs or other online quizzing applications, but it is a quick and easy tool to keep your content in your control.

Michael Hinkley - AuthorAuthor: Michael Hinckley

After ten plus years of developing and managing e learning projects I have found myself in the unique position to drive the innovation at an enterprise level.

My current title is Instructor at Citibank, although training delivery is one aspect of what I do, most of my job and interests included knowledge and content management and how it can be effectively distributed. This path has led me to SharePoint technologies as the medium to deliver customized “just in time training” and knowledge solutions.

When I am not tinkering under my MOSS sites hoods and bothering the IT department regarding admin level permissions you’ll find me either at home relaxing with wonderful my wife and three children or at my boxing gym trying to improve my “sweet science” despite the science of gravity and advancing years.

 

]]>
http://www.endusersharepoint.com/2009/06/09/how-to-replace-the-learning-management-system-with-sharepoint/feed/ 7
Case Study: Dynamic Charts in SharePoint, No Code Required! http://www.endusersharepoint.com/2009/06/05/case-study-dynamic-charts-in-sharepoint-no-code-required/ http://www.endusersharepoint.com/2009/06/05/case-study-dynamic-charts-in-sharepoint-no-code-required/#comments Fri, 05 Jun 2009 15:51:08 +0000 Natasha http://www.endusersharepoint.com/?p=1733 Note from Mark Miller: Niamh Kenny has used some of the articles here at EndUserSharePoint.com to develop an in-house solution for charts and graphs. She was kind enough to send this overview as a case study of her project.

Niamh KennyGuest Author: Niamh Kenny, London United Kingdom

Niamh is a SharePoint Administrator at Camden & Islington Foundation Trust, London. She has five years of experience working with Microsoft Sharepoint, this includes implementation, training, and customisation.

Case Study – Dynamic Charts in SharePoint: No Code Required!

I have a team site with a Tasks list where all the tasks assigned to the IT Projects Team are kept. They needed a way to report on their tasks etc in a more visual style than the out-of-the-box views!

First I connected the task list to Visio as per diagram below which was great as it gave a really good look to the tasks list and outstanding items.

Dynamic Charts

I use your site a lot to get insights/ideas for quick and easy (generally) solutions to problems or ideas I have had. I also use my test environment to test out and show to end users/managers for approval prior to posting live. I have been following the JQuery sessions for a while. I thought it would be great to have a Pie Chart visually representing the task list, initially for outstanding items with the provision that it could be extended for all types of views, depending on what information the team is either likely to need or want.

So I created the view in the task list, using a calculated column that gave a single line of text that counted either the number of days overdue, how many days were left or whether the task was completed or not and entered that information.  I also made this column count how many of each there were.

Dynamic Charts

Dynamic Charts

I had already found the post http://www.endusersharepoint.com/?p=1537 then created a new page and added the task list as a web part then added the content editor web part including the code for the pie chart. I had previously downloaded the JQuery file from the links and have added that to my top-level site collection that all subsequent sites have access to and can reference.

The final result for this page is here (I haven’t hidden the web-part yet as I think it’s quite good for people to be able to see the actual data too but that may change depending on user requirements).  

Dynamic Charts

I am currently trying to get the multiple charts to work on another page, but am still creating views etc on the task list for that to work.

I have created a Preview Pane for a Calendar using another JQuery from your site and that has gone down as a treat with users as well.  (And took all of 5 minutes too)

Dynamic Charts

]]>
http://www.endusersharepoint.com/2009/06/05/case-study-dynamic-charts-in-sharepoint-no-code-required/feed/ 12
Taming the Elusive “Calculated Column” – Date and Time – Part 4 http://www.endusersharepoint.com/2009/06/02/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-4/ http://www.endusersharepoint.com/2009/06/02/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-4/#comments Tue, 02 Jun 2009 12:08:34 +0000 Dessie Lunsford http://www.endusersharepoint.com/?p=1698 Now that we’ve gone through 4 test cases that show the calculations being performed, let’s go back to our “Plain English” translation and see how much cleaner it becomes.

Deciphering the Puzzle (v2.0)

Our condition:

If the “End Date” (number) is less than the “Start Date” (number), AND if the “Start Date” (number) minus the “End Date” (number) is greater than 1, do the following:

Count the number of days that separate the “Start Date” from the “End Date” then add 1 to that number.  From that number, subtract the number of days that separate the “Start Date” from the “End Date” plus 1, divided by 7, rounded down to the nearest 1 number, then multiplied by 2.  From this number, subtract 2 from the final result to take in account for weekend days to produce our final result.

If either or both, of the two parts of the condition are not “TRUE”, do the following:

Count the number of days that separate the “Start Date” from the “End Date” then add 1 to that number.  From that number, subtract the number of days that separate the “Start Date” from the “End Date” plus 1, divided by 7, rounded down to the nearest 1, then multiplied by 2 to produce our final result.

Still a little complex, but much easier to comprehend.

Summary

Sometimes, the obvious answer to a solution may be obscured by itself (as confusing as that very statement might sound, try going back to my original wording of the formula used in this series and attempt to read it aloud in a single breath – I can do it in 3 – barely, and I wasn’t sure what I had just read by doing it so quickly).

“DATEDIF” seems like the obvious choice for our problem, but since it’s rather literal in what it does, it counts each and every day in between two dates, including weekends, which is not exactly what we want.

In Excel, this is almost trivial because of how easy it is to come up with the solution, we just use “NETWORKDAYS”, plug in the start and end dates and voilà, we have an accurate count that magically excludes weekend days (again, another of the numerous functions they did not include in SharePoint for some reason). 

The unfortunate side of this is that even if you run the “Evaluate Formula” command within Excel on the “NETWORKDAYS” function, you can’t see each and every step being performed in the calculation to see just how it skips the weekend days in the end result.  This function obscures itself by not showing you “how” it does what it does (far too many Excel functions have this same limitation unfortunately).   But, given the functions available (within Excel, and SharePoint), we can attempt to “duplicate” what it’s doing by using several functions together that will “logically” return the exact same result, just through a much longer process.

Hopefully all of this wasn’t too confusing – it’s a hard formula to decipher, but as with all formulas, the best approach is to break it into chunks, determine what each chunk does independently, then merge them all back together and run through the entire thing with a series of test data.
Till next time…

- Dessie

Dessie LunsfordDessie Lunsford
Points-of-Sharing

]]>
http://www.endusersharepoint.com/2009/06/02/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-4/feed/ 0
Taming the Elusive “Calculated Column” – Date and Time – Part 3 http://www.endusersharepoint.com/2009/06/01/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-3/ http://www.endusersharepoint.com/2009/06/01/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-3/#comments Mon, 01 Jun 2009 12:07:08 +0000 Dessie Lunsford http://www.endusersharepoint.com/?p=1697 Test, Test, and Retest (then add 1 for good measure)

We’ll be using 4 different sets of dates in order to be able to test for the full range of possibilities with our formula:

Test #1: “Same Day”
Start Date: 5/12/2009
End Date: 5/12/2009

Test #2: “No Weekend”
Start Date: 5/18/2009
End Date: 5/22/2009

Test #3: “With Weekend”
Start Date: 5/22/2009
End Date: 5/26/2009

Test #4: “Multiple Weekends”
Start Date: 5/11/2009
End Date: 5/25/2009

We’ll plug in each of the test dates above, and run through them one by one.

Test #1: “Same Day”
Start Date: 5/12/2009
End Date: 5/12/2009

Functions:

WEEKDAY([Start Date],2) = WEEKDAY(“5/12/2009”,2) = Tuesday = 2
WEEKDAY([End Date],2) = WEEKDAY(“5/12/2009”,2) = Tuesday = 2
DATEDIF(“5/12/2009”, “5/12/2009”,"D") = 0

Our “Condition” part of the “IF” becomes:

IF(AND(2<2,(2-2)>1)

Breaking this apart, our two conditions to check for:

Is 2 less than 2? (FALSE)
Is 2 minus 2 greater than 1? (FALSE)

Since both of these are False (remember, in an “AND” function both conditions must be True for the entire condition to be True), we move to the “FALSE” side of the “IF”, which by replacing its functions with our values, becomes:

(((DATEDIF(“5/12/2009”, “5/12/2009”,"D")+1))-(FLOOR((DATEDIF(“5/12/2009”, “5/12/2009”,"D")+1)/7,1)*2))

Completing some of the functions:

(((0+1))-(FLOOR((0+1)/7,1)*2))

Cleaning it up some:

(1-(FLOOR(1/7,1)*2))

And again:

(1-(FLOOR(0.14286,1)*2))

One more time:

(1-(0*2))

Finishing off the math:

(1-(0*2)) = (1-0) = 1

Result = 1 since the “Start Date” and “End Date” are both on the same day, 1 day has elapsed.

Date and Time

Test #2: “No Weekend” (both dates in same week)
Start Date: 5/18/2009
End Date: 5/22/2009

Functions:

WEEKDAY([Start Date],2) = WEEKDAY(“5/18/2009”,2) = Monday = 1
WEEKDAY([End Date],2) = WEEKDAY(“5/22/2009”,2) = Friday = 5
DATEDIF(“5/18/2009”, “5/22/2009”,"D") = 4

Our “Condition” part of the “IF” becomes:

IF(AND(5<1,(1-5)>1)

Breaking this apart, our two conditions to check for:

Is 5 less than 1? (FALSE)
Is 1 minus 5 greater than 1? (FALSE)

Since both of these are False, we move to the “FALSE” side of the “IF”, which by replacing its functions with our values, becomes:

(((DATEDIF(“5/18/2009”, “5/22/2009”,"D")+1))-(FLOOR((DATEDIF(“5/18/2009”, “5/22/2009”,"D")+1)/7,1)*2)))

Completing some of the functions:

(((4+1))-(FLOOR((4+1)/7,1)*2)))

Cleaning it up some:

(5-(FLOOR(5/7,1)*2))

And again:

(5-(FLOOR(0.71428,1)*2))

One more time:

(5-(0*2))

Finishing off the math:

(5)-(0*2)) = (5-0) = 5

Result = 5 since the “Start Date” is on Monday and “End Date” is on the Friday of the same week, 5 days have elapsed.

Date and Time

Test #3: “With Weekend” (weekend days in between start and end dates)
Start Date: 5/22/2009
End Date: 5/26/2009

Functions:

WEEKDAY([Start Date],2) = WEEKDAY(“5/22/2009”,2) = Friday = 5
WEEKDAY([End Date],2) = WEEKDAY(“5/26/2009”,2) = Tuesday = 2
DATEDIF(“5/22/2009”, “5/26/2009”,"D") = 4

Our “Condition” part of the “IF” becomes:

IF(AND(2<5,(5-2)>1)

Breaking this apart, our two conditions to check for:

Is 2 less than 5? (TRUE)
Is 5 minus 2 greater than 1? (TRUE)

Since both of these are True (in the “AND” function, all parts must evaluate to True in order for the entire check to be True), we move to the “TRUE” side of the “IF”, which by replacing its functions with our values, becomes:

(((DATEDIF(“5/22/2009”, “5/26/2009”,"D")+1))-(FLOOR((DATEDIF(“5/22/2009”, “5/26/2009”,"D")+1)/7,1)*2)-2)

Completing some of the functions:

(((4+1))-(FLOOR((4+1)/7,1)*2)-2)

Cleaning it up some:

(5-(FLOOR(5/7,1)*2)-2)

And again:

(5-(FLOOR(0.71428,1)*2)-2)

One more time:

(5-(0*2)-2)

Finishing off the math:

(5-0-2) = 3

Result = 3 since the “Start Date” is on Friday and “End Date” is on the Tuesday of the following week, 5 days have elapsed with 2 of them being weekend days, so we subtract 2 from the total giving us a final result of 3 (counting Friday, Monday, and Tuesday).

Date and Time

Test #4: “Multiple Weekends” (more than one set of weekends)
Start Date: 5/11/2009
End Date: 5/25/2009

Functions:

WEEKDAY([Start Date],2) = WEEKDAY(“5/11/2009”,2) = Monday = 1
WEEKDAY([End Date],2) = WEEKDAY(“5/25/2009”,2) = Monday = 1
DATEDIF(“5/11/2009”, “5/25/2009”,"D") = 14

Our “Condition” part of the “IF” becomes:

IF(AND(1<1,(1-1)>1)

Breaking this apart, our two conditions to check for:

Is 1 less than 1? (FALSE)
Is 0 greater than 1? (FALSE)

Since both of these are False, we move to the “FALSE” side of the “IF”, which by replacing its functions with our values, becomes:
(((DATEDIF(“5/11/2009”, “5/25/2009”,"D")+1))-(FLOOR((DATEDIF(“5/11/2009”, “5/25/2009”,"D")+1)/7,1)*2)))

Completing some of the functions:

(((14+1))-(FLOOR((14+1)/7,1)*2)))

Cleaning it up some:

(15-(FLOOR(15/7,1)*2)

And again:

(15-(FLOOR(2.14285,1)*2)

One more time:

(15-(2*2))

Finishing off the math:

(15-(2*2)) = (15-4) = 11

Result = 11 since the “Start Date” is on a Monday and “End Date” is also on a Monday, but two weeks later, we count the total number of days between the two (15) then subtract the number of weekend days (4) giving us a total of 11 days elapsed.

Date and Time

In the next part of this series, we’ll be wrapping things up with a quick revisit to our (now more understandable) formula, and summary.

Till then…

- Dessie

Dessie LunsfordDessie Lunsford
Points-of-Sharing

]]>
http://www.endusersharepoint.com/2009/06/01/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-3/feed/ 1
Taming the Elusive “Calculated Column” – Date and Time – Part 2 http://www.endusersharepoint.com/2009/05/29/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-2/ http://www.endusersharepoint.com/2009/05/29/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-2/#comments Fri, 29 May 2009 12:06:32 +0000 Dessie Lunsford http://www.endusersharepoint.com/?p=1696 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

]]>
http://www.endusersharepoint.com/2009/05/29/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-2/feed/ 0
Taming the Elusive “Calculated Column” – Date and Time – Part 1 http://www.endusersharepoint.com/2009/05/28/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-1/ http://www.endusersharepoint.com/2009/05/28/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-1/#comments Thu, 28 May 2009 14:05:35 +0000 Dessie Lunsford http://www.endusersharepoint.com/?p=1693 Overview

I wanted to start off the series on the “Date and Time” set of functions by examining a specific problem that many folks have posted questions about – that is, how do you calculate the difference (number of days) between two dates, while excluding weekends? (Iain on the STP forums asked this recently, which sparked my interest in writing this)

At first glance, this seems like an obvious candidate for the “DATEDIF” function since by definition, it “Calculates the number of days, months, or years between two dates”.  The problem with this approach however, is that by itself, it will count all days, including those specific ones we don’t want (weekends).
If, on the other hand, we were just working in Excel, we could take advantage of “NETWORKDAYS” since it will return the number of ‘whole’ workdays between two dates, but alas, we don’t have that one available within SharePoint*

*One of my most wanted features of SharePoint version-next (or next): make list functionality exactly the same as Excel spreadsheets.  If you’re going to bother to model them after spreadsheets by including functions and abilities to perform calculations, you might as well go all the way and transform them into real working sheets.  This to me screams “Office Live”!!  We already have the ability to export to a spreadsheet with a data connection, so how about extending this a bit further by adding in the ability of exporting a spreadsheet built in Excel to a new list?  Imagine creating the functionality you want within Excel, using all the power that comes with it, then choosing “Export to new SharePoint List” where it asks you to enter in the site address (or choose from a list of available connections), the name of the list, then “Export”…and done.  You’d now have a new list based on your spreadsheet with the benefit of already having an active data connection setup so you can administer the list either through the list itself, or through the original spreadsheet.  Any new columns, calculations (using the full-range of what’s available in Excel), or updates would be saved to both – in real time.  Imagine the possibilities…

Since we can’t perform the “exact” same calculations we could otherwise do in Excel, we need to recreate the functionality performed in such a manner that can be compatible with SharePoint.

Let’s take a look at the formula that will accomplish this for us then examine it to see how its logic performs the date calculations.

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)))

On the surface, this does appear a bit daunting, given the amount of parentheses and functions, so let’s break it down into its core functions and examine what each does.

We have 12 functions:
1 “IF”
1 “AND”
4 ”WEEKDAY”
4 “DATEDIF”
2 “FLOOR”

We’ve already covered the “IF” and “AND” functions in detail during the series on “Logic”, so let’s look at the other three.

The Functions

WEEKDAY: returns the day of the week corresponding to a date.

Using the optional "Return_type" value in addition, dictates what day of the week to start with and it’s associated number (weeks will start on either Sunday or Monday):

1 (or omitted) = Sunday through Saturday (Sun=1, Mon=2, Tue=3, Wed=4, Thu=5, Fri=6, Sat=7)
2 = Monday through Sunday (Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6, Sun=7)
3 = Monday through Sunday (Mon=0, Tue=1, Wed=2, Thu=3, Fri=4, Sat=5, Sun=6)

The “Return_type” attribute is used to pick what day of the week is your starting day (either Sunday or Monday) and what numbering scheme you’d like to use to count by (7 numbers total – you can either start with a 1 and count to 7, or start with a 0 and count to 6 – either way, it still amounts to 7 numbers total).

The syntax of the function is:
=WEEKDAY(Date,Return_type)

Using the example of a “Date/Time” column with a date of “5/19/2008”, we’d have a value displayed of “2” because that particular day was a “Tuesday”.   We could use any of the date formats for this, so the actual “Date/Time” column doesn’t necessarily need to be a “Date/Time” type, it could be just a regular text box with a date test in it, or some calculation resulting in the serial number for the date (“39952” is the serial number for that specific date and would return the exact same result).

Date and Time

Date and Time

DATEDIF: calculates the number of days, months or years between two dates.

The type of information returned is indicated by the "Unit" specified.

Syntax: DATEDIF(StartDate,EndDate,Unit)

Unit:
"Y" = number of years
"M" = number of months
"D" = number of days
"MD" = number of days ignoring the months and years
"YM" = number of months ignoring days and years
"YD" = number of days ignoring years

Using two dates (“Date/Time” columns, or any value represented in a date format), we can count the number of days between them:

Date1 = 5/11/2009
Date2 = 5/15/2009

Formula:

=DATEDIF(Date1,Date2,"D")

Result displayed = 4 (we’re using the “D” Unit to count days only)

This is accurate because there are 4 days difference between the two and SharePoint considers the end of a day to be at 11:59 p.m. (Date1 is a Monday, Date2 is a Friday: start counting on Tuesday: Tue, Wed, Thu, Fri = 4 days total).

Date and Time

FLOOR (part of the “Math and Trigonometry” set of functions, but will be used in our formula): rounds number down towards zero to the nearest multiple of significance ("significance" being the amount you want to round down to. Examples: using "1" would round down the number to the nearest whole number, ".1" would round down to the nearest 10th, "-5" would round down to the nearest negative five, etc.).

Syntax:

FLOOR(number,significance)

Using a “Number” column (could be an actual “Number” type, or a text value since SharePoint will automatically convert a string value representing a number into an actual number), we can see the simple “rounding” down performed:

Number = 15.1

Formula:

=FLOOR(Number,1)

Result displayed = 15 (since 15 is the nearest “1” number down)

Date and Time

Next time, we’ll continue on by “attempting” to decipher the formula.

Till then…

- Dessie

Dessie LunsfordDessie Lunsford
Points-of-Sharing

]]>
http://www.endusersharepoint.com/2009/05/28/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-date-and-time-part-1/feed/ 4
Taming the Elusive “Calculated Column” – Logic – Part 10 http://www.endusersharepoint.com/2009/05/13/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-logic-part-10/ http://www.endusersharepoint.com/2009/05/13/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-logic-part-10/#comments Wed, 13 May 2009 14:00:45 +0000 Dessie Lunsford http://www.endusersharepoint.com/?p=1609 Our Final List

In SharePoint create a list called “Employee Paychecks”.

Calculated Column Part 10

Create a column called “Employee”, make it a “Choice” type and add in the two choices of “Yes” and “No” as “Radio Buttons”. 

Calculated Column Part 10

NOTE – I chose the “Choice” column rather than the “Yes/No” checkbox option because the “Yes/No” type doesn’t have a clear display as to what it really is (it doesn’t actually display the text “Yes” or “No” and can be confusing).  Using a “Choice” type instead, will clearly display just what the column is for and will make it more readable for the users.

Create three additional columns for the 3 types of hours all in the same fashion as the “Employee” column (“Choice” type, “Yes” “No” choices, “Radio Buttons”).

Column titles: “Regular Hours”, “Vacation”, “Sick Leave

Calculated Column Part 10

Since we’re using “Choice” columns in our list, we’ll need to add in a little bit more to our formula in order to test for the values that they’ll be returning.

Formula so far:

=IF(AND([Employee], OR([Regular Hours],[Vacation],[Sick Leave])),"Issue Paycheck","Do Not Issue Paycheck")

Change to:

=IF(AND([Employee]="Yes",OR([Regular Hours]="Yes",[Vacation]="Yes",[Sick Leave]="Yes")),"Issue Paycheck","Do Not Issue Paycheck")

This will allow us to check the return “text” value of each column to see if it’s either “Yes” or “No”.

Create our final column called “Issue Paycheck” make it a “Calculated” type with a return type of “Text” and enter in the modified formula.

=IF(AND([Employee]="Yes",OR([Regular Hours]="Yes",[Vacation]="Yes",[Sick Leave]="Yes")),"Issue Paycheck","Do Not Issue Paycheck")

Calculated Column Part 10

Once you have all columns created, enter in a series of items on the list using the same combinations of values from the “Truth Table”.

Calculated Column Part 10

NOTE – I’ve also added in a couple extra items to show that it will handle empty values as well (since empty does not equal “Yes”, the result will be false).

Conclusion

If we had taken the same direction as in the previous series (detailing the “IF”) to test for each possible combination of values with multiple nested “IF” functions, the formula would have been considerable longer, and would have worked, but would have been much less efficient. 

By using the “OR” and “AND” functions to test for combinations of values at the same time, we’ve not only lessened the development time need to create the formula, we’ve also made debugging and testing much easier (always a good thing).

Also, for those that may be wondering why we didn’t just use a single “Choice” column to house each of the options for hours, the reason is because SharePoint cannot handle “Multiple Select” values.  Since there isn’t an “Array” function to deal with dynamic data types (those that aren’t consistent in length), a calculated column will not be able to reference a column that (can) return more than a single value.  We could use a single Choice column, but it would defeat the logic we’re trying to accomplish since the calculation wouldn’t be able to see “more than one” choice…it would only see one.

Till next time…

- Dessie

Dessie LunsfordDessie Lunsford
Points-of-Sharing

]]>
http://www.endusersharepoint.com/2009/05/13/taming-the-elusive-%e2%80%9ccalculated-column%e2%80%9d-logic-part-10/feed/ 2