Solved

How to use multiple if statements in a SharePoint calculated column?

Posted on 2011-02-14
19
7,575 Views
Last Modified: 2012-05-11
I have a SharePoint 2007 list. In it, I have a "Room Numbers" column (type: single line of text**)
I would like to group this list based on several "zones".

For example, for all list items that contain a room number between 1100-1800 group them as "1100-1800".  Likewise for all list items that contain a room number between 2100-2800 group them as "2100-2800".  This will continue to include more "zones".

I thought the best way to create these "zones" is to use a calculated column ("Zone" with a formula that includes multiple if statements.  I can't determine if SharePoint 2007 allows this.  

Here is what I have so far as a forumula in my "Zone" calculated column:

=IF(OR([Room number]>1100,[Room number]<1900),"1100-1800","")


What is the correct syntax to create additional ranges?
Is there a way to add multiple if statements or is there a better way to do this?




**I realize the column type really should be number,  but I wanted an "out of the box" way to display the four digit numbers without a comma between the first and second digits.
0
Comment
Question by:creativehues
  • 12
  • 7
19 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34892335
=IF(OR([Room number]>=1100,[Room number]<1900),"1100-1800",
	(OR([Room number]>=2100,[Room number]<2900),"2100-2800",
	(OR([Room number]>=3100,[Room number]<3900),"3100-3800",
	(OR([Room number]>=4100,[Room number]<4900),"4100-4800",
	("")))))

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34892398

Sorry, previous reply was bad.

Isn't there a boolean "AND" operator (maybe &&)?  If so, you could use:

=IF(([Room number]>=1100 && [Room number]<1900),"1100-1800",
	IF(([Room number]>=2100 && [Room number]<2900),"2100-2800",
	  IF(([Room number]>=3100 && [Room number]<3900),"3100-3800",
	    IF(([Room number]>=4100 && [Room number]<4900),"4100-4800",
	(""))))) 

Open in new window


Anyway, the trick is to nest IFs, deeper and deeper.  Then at the very end, close all the parens.
0
 

Author Comment

by:creativehues
ID: 34892548
I will try your code suggestion and let you know if it works later in the next 16 hours. Thanks for your quick response.
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:creativehues
ID: 34896851
:(  That didn't work, not sure what is causing the syntax error.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34897046
Try this.  Using AND instead of &&.

=IF((([Room number]>=1100) AND ([Room number]<1900)),"1100-1800",
	IF((([Room number]>=2100) AND ([Room number]<2900)),"2100-2800",
	  IF((([Room number]>=3100) AND ([Room number]<3900)),"3100-3800",
	    IF((([Room number]>=4100) AND ([Room number]<4900)),"4100-4800",
	("")))))  
  

Open in new window

0
 

Author Comment

by:creativehues
ID: 34897573
:(   The formula contains a syntax error or is not supported.
0
 

Author Comment

by:creativehues
ID: 34897659
Do we need to organize this statement like Kartic's solution?
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34897683
Simpler version - let's see what this does.  Please double-check that I have matched all my parens.

=IF( ([Room number]<1900), "1100-1800",
  IF( ([Room number]<2900), "2100-2800",
   IF( ([Room number]<3900), "3100-3800",
    IF( ([Room number]<4900), "4100-4800",
     ("") ))))  

Open in new window

0
 

Author Comment

by:creativehues
ID: 34897950
This works! But only if I change the type to  number for the room number column. I have to figure out how not to display these room numbers with a comma between the first and second digits.
0
 

Assisted Solution

by:creativehues
creativehues earned 0 total points
ID: 34897977
I figured out a solution using another calculated column and =text([Room number],"0")

0
 

Author Comment

by:creativehues
ID: 34898103
If I have more than 7 nested if statements - how do I make this work?
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34898115
Good going.  Thanks.

And by the way, I think that the correct "AND" syntax is like this:

        IF( AND( ([Room number]>=3100), ([Room number]<3900) ), "3100-3800",

(stupid language, IMHO)
0
 

Author Comment

by:creativehues
ID: 34898235
Can you help me use incorporate the workaround if I have more than 7 nested if statements?

I see this solution

I guess if I follow this process (creating a second calculate column and referencing it within the first group of if statementsI  I should be able to figure it out.  

I will let you know if I get stuck.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34898269
Crazy limit, but here's a good trick to get around it.

The idea is to have multiple nested blocks of 7 options each, concatenated.  The blocks that don't "apply" need to evaluate to empty strings.  So you can chain as many of these IF blocks together and all but one of them evaluate to empty strings.  So the end result is the one matching entry, whichever IF block it happens to be in.

Simple, but a little hard to describe.  Does this make sense to you?

=IF( ([Room number]<1900), "1100-1800",
  IF( ([Room number]<2900), "2100-2800",
   IF( ([Room number]<3900), "3100-3800",
    IF( ([Room number]<4900), "4100-4800",
     ("") ))))   
& 
IF( ([Room number]<4900), "",
 IF( ([Room number]<5900), "5100-1800",
  IF( ([Room number]<6900), "6100-2800",
   IF( ([Room number]<7900), "7100-3800",
     ("") ))))   
&
...

Open in new window

0
 

Author Comment

by:creativehues
ID: 34898302
I guess it makes sense I will try this and let you know.
0
 

Author Comment

by:creativehues
ID: 34898388
I got it. Whew!  This rocks thanks again!
0
 

Author Comment

by:creativehues
ID: 34901334
I'm not sure if I can still ask this or if I should start another question, but I need to know the correct syntax if the range of "room numbers" is non-sequential.

For example if I want to identify room numbers between 3600 and 3999 AND 4100 through 4299, but not numbers between 4000 and 4099.

Here is my start:
=IF(OR(([Room number]>=3600),([Room number]<3999)),"Bldgs. 36-39")

how do I add the the additional range?


0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34901475

This code fragment shows what one such IF branch would like.

This makes it a little more complicated if you have multiple IF blocks like we discussed earlier (more than 7 options) since you'll need to be especially careful to be sure all the "empty string" cases work right.

IF( OR( AND(([Room number]>=3600),([Room number]<=3999)),
         AND(([Room number]>=4100),([Room number]<=4299)) ), "Bldgs. 36-39",
...
)

Open in new window

0
 

Author Closing Comment

by:creativehues
ID: 34936413
Thanks for continued efforts to find my solution.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you create your solutions on SharePoint sooner or later you will come upon a request to set  permissions of the item depending on some of the item's meta-data - the author, people assigned as approvers, divisions, categories etc. The most natu…
Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question