Solved

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

Posted on 2011-02-14
19
9,288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

630 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