• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12162
  • Last Modified:

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

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
creativehues
Asked:
creativehues
  • 12
  • 7
2 Solutions
 
sjklein42Commented:
=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
 
sjklein42Commented:

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
 
creativehuesAuthor Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
creativehuesAuthor Commented:
:(  That didn't work, not sure what is causing the syntax error.
0
 
sjklein42Commented:
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
 
creativehuesAuthor Commented:
:(   The formula contains a syntax error or is not supported.
0
 
creativehuesAuthor Commented:
Do we need to organize this statement like Kartic's solution?
0
 
sjklein42Commented:
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
 
creativehuesAuthor Commented:
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
 
creativehuesAuthor Commented:
I figured out a solution using another calculated column and =text([Room number],"0")

0
 
creativehuesAuthor Commented:
If I have more than 7 nested if statements - how do I make this work?
0
 
sjklein42Commented:
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
 
creativehuesAuthor Commented:
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
 
sjklein42Commented:
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
 
creativehuesAuthor Commented:
I guess it makes sense I will try this and let you know.
0
 
creativehuesAuthor Commented:
I got it. Whew!  This rocks thanks again!
0
 
creativehuesAuthor Commented:
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
 
sjklein42Commented:

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
 
creativehuesAuthor Commented:
Thanks for continued efforts to find my solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 12
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now