[Solved] Nested conditionals with OR

Discuss the spreadsheet application
Post Reply
sashcramp
Posts: 10
Joined: Thu Oct 28, 2021 12:50 am

[Solved] Nested conditionals with OR

Post by sashcramp »

Hi,
I'm having great difficulty nesting these conditionals. The first conditional works on its own but when I try to add the second I get Err 504 or 509. I am a beginner, so no doubt I'm making some elementary error. Can anyone point it out out to me, please ?

=IF(OR(C214={"Boots";"pocklington's bakery";"ebay"});"Paypal";"SOURCE?";IF(OR(C214={"amazon";"tesco"});"1476";"SOURCE?")) gives Err 504

=IF(OR(C214={"Boots";"pocklington's bakery";"ebay"});"Paypal";"SOURCE?");IF(OR(C214={"amazon";"tesco"});"1476";"SOURCE?") gives Err 509

=IF(OR(C214={"Boots";"pocklington's bakery";"ebay"});"Paypal";"SOURCE?" works OK
Last edited by Hagar Delest on Thu Oct 28, 2021 9:37 pm, edited 1 time in total.
Reason: Tagged [Solved].
Apache Open Office 4.1.3
AOO413m1(Build:9783) - Rev. 1761381
2016-09-23 02:39:34 (Fri, 23 Sep 2016)
MAC OS Catalina 10.15.7.
User avatar
RusselB
Moderator
Posts: 6646
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

Re: Nested conditionals with OR

Post by RusselB »

Welcome to the Forums.
I've never seen the OR function used in this manner before, but I think I understand the logic of how it should be working.
The problem you are running into is not with the OR, but with the IF.
The IF has 3 parameters. The condition that has to match, the response if it does match (aka TRUE), and the response if it does not (aka FALSE)
In the sample that works, the 1st parameter is fulfilled by the OR function. The 2nd parameter is the term "Paypal", and the 3rd parameter is the term "SOURCE?"
There is no option in Calc for a 4th parameter, which is where you are having your problem.
A rewrite (untested) could look like

Code: Select all

=if(or(c214={Boots";"pockington's bakery";"ebay"});"Paypal";if(or(c214={amazon";"tesco"});"1476";"SOURCE?");"SOURCE?")
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Nested conditionals with OR

Post by Zizi64 »

sashcramp,
please upload your ODF type sample file here.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
cwolan
Posts: 129
Joined: Sun Feb 07, 2021 3:44 pm

Re: Nested conditionals with OR

Post by cwolan »

RusselB wrote:I've never seen the OR function used in this manner before, [...]
This manner is also new to me. What does it mean?

Code: Select all

C214={"Boots";"pocklington's bakery";"ebay"}
Is this to check whether cell C214 contains a value from the set {...} ?
If so, it won't not give the expected result. Cf. the screenshot.
Attachments
Check.JPG
OpenOffice 1.1.5 – 4.1.15
LibreOffice 3.3.0.4 – 7.6.6
Windows 7,10,11 64-bit
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Nested conditionals with OR

Post by Zizi64 »

Please upload a real sample spreadsheet document here.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
cwolan
Posts: 129
Joined: Sun Feb 07, 2021 3:44 pm

Re: Nested conditionals with OR

Post by cwolan »

Here you go, though it is a rudimentary test.
The OP should give his/her view and provide the file you've asked for.
Attachments
testFILE.ods
(8.19 KiB) Downloaded 127 times
OpenOffice 1.1.5 – 4.1.15
LibreOffice 3.3.0.4 – 7.6.6
Windows 7,10,11 64-bit
User avatar
robleyd
Moderator
Posts: 5086
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Nested conditionals with OR

Post by robleyd »

A read of the offline help on array formulas might be entertaining :-)
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Nested conditionals with OR

Post by Zizi64 »

Here is an another tip:
testFILE_Zizi64.ods
(9.57 KiB) Downloaded 124 times
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
cwolan
Posts: 129
Joined: Sun Feb 07, 2021 3:44 pm

Re: Nested conditionals with OR

Post by cwolan »

robleyd wrote:A read of the offline help on array formulas might be entertaining :)
And a read of the online help on array formulas might be entertaining too.

But joking aside, I just asked a question to be on the safe side.
I remember a post on some forum (long ago) — after analysis, it turned out that the OP "misused" {}.
OpenOffice 1.1.5 – 4.1.15
LibreOffice 3.3.0.4 – 7.6.6
Windows 7,10,11 64-bit
sashcramp
Posts: 10
Joined: Thu Oct 28, 2021 12:50 am

Re: Nested conditionals with OR

Post by sashcramp »

Zizi64 wrote:Please upload a real sample spreadsheet document here.
Thanks. Here is the spreadsheet sample.

My original formula was just a number of nested conditionals that returned the appropriate result in col B for col A. Using this, I must add a whole new conditional each time a new term crops up in A. I was trying the array formula hoping to type in only the new term.
Attachments
sample sheet.ods
(12.96 KiB) Downloaded 118 times
Apache Open Office 4.1.3
AOO413m1(Build:9783) - Rev. 1761381
2016-09-23 02:39:34 (Fri, 23 Sep 2016)
MAC OS Catalina 10.15.7.
sashcramp
Posts: 10
Joined: Thu Oct 28, 2021 12:50 am

Re: Nested conditionals with OR

Post by sashcramp »

cwolan wrote:
RusselB wrote:I've never seen the OR function used in this manner before, [...]
This manner is also new to me. What does it mean?

Code: Select all

C214={"Boots";"pocklington's bakery";"ebay"}
Is this to check whether cell C214 contains a value from the set {...} ?
If so, it won't not give the expected result. Cf. the screenshot.
Yes, I think you're right, there. Even though I thought it was working, I tested it further and it wasn't ! What I'm trying to achieve is "If the cell contains A, B or C then print "Paypal", if the cell contains X,Y or Z then print "1476" else print "SOURCE?".

When a term other than A B C X Y or Z crops up in the cell, I'd like to be able to add just that term to the formula, rather than type a whole nested conditional, as I was previously doing.
Apache Open Office 4.1.3
AOO413m1(Build:9783) - Rev. 1761381
2016-09-23 02:39:34 (Fri, 23 Sep 2016)
MAC OS Catalina 10.15.7.
User avatar
robleyd
Moderator
Posts: 5086
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Nested conditionals with OR

Post by robleyd »

Have you considered a lookup table?
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
sashcramp
Posts: 10
Joined: Thu Oct 28, 2021 12:50 am

Re: Nested conditionals with OR

Post by sashcramp »

RusselB wrote:Welcome to the Forums.
I've never seen the OR function used in this manner before, but I think I understand the logic of how it should be working.
The problem you are running into is not with the OR, but with the IF.
The IF has 3 parameters. The condition that has to match, the response if it does match (aka TRUE), and the response if it does not (aka FALSE)
In the sample that works, the 1st parameter is fulfilled by the OR function. The 2nd parameter is the term "Paypal", and the 3rd parameter is the term "SOURCE?"
There is no option in Calc for a 4th parameter, which is where you are having your problem.
A rewrite (untested) could look like

Code: Select all

=if(or(c214={Boots";"pockington's bakery";"ebay"});"Paypal";if(or(c214={amazon";"tesco"});"1476";"SOURCE?");"SOURCE?")
Thanks. I suspect I'm also making a mistake trying to use OR and an array formula to achieve what I want.
Apache Open Office 4.1.3
AOO413m1(Build:9783) - Rev. 1761381
2016-09-23 02:39:34 (Fri, 23 Sep 2016)
MAC OS Catalina 10.15.7.
sashcramp
Posts: 10
Joined: Thu Oct 28, 2021 12:50 am

Re: Nested conditionals with OR

Post by sashcramp »

robleyd wrote:Have you considered a lookup table?
Thanks for the suggestion but unfortunately I can't sort the searchtable in my particular application.
Apache Open Office 4.1.3
AOO413m1(Build:9783) - Rev. 1761381
2016-09-23 02:39:34 (Fri, 23 Sep 2016)
MAC OS Catalina 10.15.7.
sashcramp
Posts: 10
Joined: Thu Oct 28, 2021 12:50 am

Re: Nested conditionals with OR

Post by sashcramp »

cwolan wrote:
robleyd wrote:A read of the offline help on array formulas might be entertaining :)
And a read of the online help on array formulas might be entertaining too.

But joking aside, I just asked a question to be on the safe side.
I remember a post on some forum (long ago) — after analysis, it turned out that the OP "misused" {}.
I'm beginning to empathise with that OP :lol: I'm probably barking up the wrong tree by trying to use OR with array formulas, anyway. Thanks for the sample sheet, by the way.
Apache Open Office 4.1.3
AOO413m1(Build:9783) - Rev. 1761381
2016-09-23 02:39:34 (Fri, 23 Sep 2016)
MAC OS Catalina 10.15.7.
User avatar
keme
Volunteer
Posts: 3704
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: Nested conditionals with OR

Post by keme »

sashcramp wrote:I'm probably barking up the wrong tree by trying to use OR with array formulas, anyway.
Surprisingly (to me, at least), the use of OR() with array input changes the evaluation mode into array context just fine. It is not wrong. You just need to meticulously plan the evaluation.

The error lies in formula structure. I will elaborate on RussellB's answer, which is essentially correct, baring a small bug.

Structure of the IF function is:
=IF(<expression to be evaluated for zero (FALSE) or nonzero (TRUE)> ; <expression to be returned for TRUE> ; <expression to be returned for FALSE>)

Applying the color coding to your formula:
=IF(OR(C214={"Boots";"pocklington's bakery";"ebay"});"Paypal";"SOURCE?";IF(OR(C214={"amazon";"tesco"});"1476";"SOURCE?"))
The black part at the end constitutes the fourth parameter which RussellB mentions, and which is totally irrelevant to IF syntax and therefore unsupported, thus returning an error.

With your small data set it can be converted as per RussellB's strategy. Alas, he accidentally introduced another fourth parameter, or I may say "inherited" from your other formula suggestion. This slight rephrase should work:
=IF(OR(C214={"Boots";"pocklington's bakery";"ebay"});"Paypal";IF(OR(C214={"amazon";"tesco"});"1476";"SOURCE?"))
The inner IF() constitutes the third parameter to the outer IF().

That said, with larger data sets and occasional changes to payment, inline arrays easily become unmanageable. Also, inline arrays can only contain constant values, no references or calculations, which is often a significant limitation. Use a lookup function instead.
sashcramp wrote:Thanks for the suggestion but unfortunately I can't sort the searchtable in my particular application.
You need exact matches for this, which requires the use of VLOOKUP(), HLOOKUP() or MATCH() with the "sorted" parameter set to false/0. Thus, your table does not have to be sorted.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Nested conditionals with OR

Post by Villeroy »

With cells instead of inline arrays:
=IF(ISNUMBER(MATCH(C214 ; $X$1:$X$99 ; 0)) ; "Paypal" ; IF(ISNUMBER(MATCH(C214 ; $Y$1:$Y$99 ; 0)) ; "1476" ; "SOURCE?"))
where $X$1:$X$99 and $Y$1:$Y$99 are arbitrary columns with the criteria replacing the inline array. MATCH can deal with columns and rows as well.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
sashcramp
Posts: 10
Joined: Thu Oct 28, 2021 12:50 am

Re: Nested conditionals with OR

Post by sashcramp »

keme wrote:
sashcramp wrote:I'm probably barking up the wrong tree by trying to use OR with array formulas, anyway.
Surprisingly (to me, at least), the use of OR() with array input changes the evaluation mode into array context just fine. It is not wrong. You just need to meticulously plan the evaluation.

The error lies in formula structure. I will elaborate on RussellB's answer, which is essentially correct, baring a small bug.

Structure of the IF function is:
=IF(<expression to be evaluated for zero (FALSE) or nonzero (TRUE)> ; <expression to be returned for TRUE> ; <expression to be returned for FALSE>)

Applying the color coding to your formula:
=IF(OR(C214={"Boots";"pocklington's bakery";"ebay"});"Paypal";"SOURCE?";IF(OR(C214={"amazon";"tesco"});"1476";"SOURCE?"))
The black part at the end constitutes the fourth parameter which RussellB mentions, and which is totally irrelevant to IF syntax and therefore unsupported, thus returning an error.



With your small data set it can be converted as per RussellB's strategy. Alas, he accidentally introduced another fourth parameter, or I may say "inherited" from your other formula suggestion. This slight rephrase should work:
=IF(OR(C214={"Boots";"pocklington's bakery";"ebay"});"Paypal";IF(OR(C214={"amazon";"tesco"});"1476";"SOURCE?"))
The inner IF() constitutes the third parameter to the outer IF().

That said, with larger data sets and occasional changes to payment, inline arrays easily become unmanageable. Also, inline arrays can only contain constant values, no references or calculations, which is often a significant limitation. Use a lookup function instead.
sashcramp wrote:Thanks for the suggestion but unfortunately I can't sort the searchtable in my particular application.
You need exact matches for this, which requires the use of VLOOKUP(), HLOOKUP() or MATCH() with the "sorted" parameter set to false/0. Thus, your table does not have to be sorted.
Many thanks! I couldn't detect the bug but I see it now. This works well but, as you say, I'm using a small dataset and I can't add a third payment type due to the limit of 3 parameters. I'll read up on the lookup function. Thanks again.
Apache Open Office 4.1.3
AOO413m1(Build:9783) - Rev. 1761381
2016-09-23 02:39:34 (Fri, 23 Sep 2016)
MAC OS Catalina 10.15.7.
sashcramp
Posts: 10
Joined: Thu Oct 28, 2021 12:50 am

Re: Nested conditionals with OR

Post by sashcramp »

Villeroy wrote:With cells instead of inline arrays:
=IF(ISNUMBER(MATCH(C214 ; $X$1:$X$99 ; 0)) ; "Paypal" ; IF(ISNUMBER(MATCH(C214 ; $Y$1:$Y$99 ; 0)) ; "1476" ; "SOURCE?"))
where $X$1:$X$99 and $Y$1:$Y$99 are arbitrary columns with the criteria replacing the inline array. MATCH can deal with columns and rows as well.
That's interesting, thanks, it would save me altering the formula each time there's a new term in col C !
Apache Open Office 4.1.3
AOO413m1(Build:9783) - Rev. 1761381
2016-09-23 02:39:34 (Fri, 23 Sep 2016)
MAC OS Catalina 10.15.7.
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Nested conditionals with OR

Post by Zizi64 »

it would save me altering the formula each time there's a new term in col C !
Use a Named Range, and use the name in the formula.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
sashcramp
Posts: 10
Joined: Thu Oct 28, 2021 12:50 am

Re: Nested conditionals with OR

Post by sashcramp »

Zizi64 wrote:
it would save me altering the formula each time there's a new term in col C !
Use a Named Range, and use the name in the formula.
Thanks.
Apache Open Office 4.1.3
AOO413m1(Build:9783) - Rev. 1761381
2016-09-23 02:39:34 (Fri, 23 Sep 2016)
MAC OS Catalina 10.15.7.
User avatar
keme
Volunteer
Posts: 3704
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: Nested conditionals with OR

Post by keme »

sashcramp wrote:...
I'll read up on the lookup function. Thanks again.
Please note the difference between "lookup functions" and "the lookup function":
  • The LOOKUP() function does require a sorted search range. This is the original spreadsheet lookup function.
  • The lookup functions VLOOKUP(), HLOOKUP() and MATCH() accept the "sorted" parameter which doubles as a signal for "require exact match", and will work with unsorted search range.
sashcramp
Posts: 10
Joined: Thu Oct 28, 2021 12:50 am

Re: Nested conditionals with OR

Post by sashcramp »

keme wrote:
sashcramp wrote:...
I'll read up on the lookup function. Thanks again.
Please note the difference between "lookup functions" and "the lookup function":
  • The LOOKUP() function does require a sorted search range. This is the original spreadsheet lookup function.
  • The lookup functions VLOOKUP(), HLOOKUP() and MATCH() accept the "sorted" parameter which doubles as a signal for "require exact match", and will work with unsorted search range.

OK, thank you.
Apache Open Office 4.1.3
AOO413m1(Build:9783) - Rev. 1761381
2016-09-23 02:39:34 (Fri, 23 Sep 2016)
MAC OS Catalina 10.15.7.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Nested conditionals with OR

Post by Villeroy »

Zizi64 wrote:Use a Named Range, and use the name in the formula.
This does not solve the probem unless you constantly maintain the named reference. It saves some time and effort because you can maintain the name in one place while using it in many places.

Instead of maintaining references, you expand all references (named and unnamed ones) by cell insertion.
Tools>Options>Calc>General> "Expand references = ON". WIth this option checked the trick works when you insert cells directly below the list.
keme wrote:The LOOKUP() function does require a sorted search range. This is the original spreadsheet lookup function.
LOOKUP is the wrong function anyway because it returns false positives.
=LOOKUP(3;{0;2;4;6}) returns 2 although the searched number 3 is not in the list. Things become totally confusing with text.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply