Solved

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

Posted on 2011-02-14
19
6,364 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now