Solved

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

Posted on 2011-02-14
19
7,136 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

A question that is asked often, is how to generate sequential numbers in InfoPath Forms. The best way to achieve this is to use a SQL database, along with a stored procedure and a web service to connect Forms Services to the DB. The first thing t…
The Scenario: Let’s say you have a quote worksheet in Excel that you use to work up sales figures and such for your clients. You utilize SharePoint to manage and keep track of these documents. You would like values from your worksheet to populate Sh…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 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