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

Need help with a crystal formula

I have this formula that only works with the first line. When adding the second line I get an error

This works with no errors
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY01} then {PRICING.BVBREAKPRICE01} 

Open in new window


When adding the second line I get (see image below):
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY01} then {PRICING.BVBREAKPRICE01} and
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY02} then {PRICING.BVBREAKPRICE02}

Open in new window


a boolean is required
This is the final formula that I need

if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY01} then {PRICING.BVBREAKPRICE01} and
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY02} then {PRICING.BVBREAKPRICE02} and
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY03} then {PRICING.BVBREAKPRICE03} and
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY04} then {PRICING.BVBREAKPRICE04} and
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY05} then {PRICING.BVBREAKPRICE05} and
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY06} then {PRICING.BVBREAKPRICE06} and
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY07} then {PRICING.BVBREAKPRICE07} and
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY08} then {PRICING.BVBREAKPRICE08} and
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY09} then {PRICING.BVBREAKPRICE09}

Open in new window

0
Gerhardpet
Asked:
Gerhardpet
  • 6
  • 5
  • 5
  • +1
2 Solutions
 
13ShadowCommented:
Change all your 'and' s to else.
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY01} then {PRICING.BVBREAKPRICE01} else
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY02} then {PRICING.BVBREAKPRICE02} else
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY03} then {PRICING.BVBREAKPRICE03} else
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY04} then {PRICING.BVBREAKPRICE04} else
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY05} then {PRICING.BVBREAKPRICE05} else
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY06} then {PRICING.BVBREAKPRICE06} else
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY07} then {PRICING.BVBREAKPRICE07} else
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY08} then {PRICING.BVBREAKPRICE08} else
if {SALES_ORDER_DETAIL.BVORDQTY} <= {PRICING.BVBREAKQTY09} then {PRICING.BVBREAKPRICE09}
0
 
Kurt ReinhardtSr. Business Intelligence Consultant/ArchitectCommented:
Personally, when I have a finite list of possible combinations, I like to use a SWITCH function, which is the functional equivalent of an IF-THEN-ELSE that has no default "ELSE" value at the end.  Also, if I'm going to have one value repeated constantly, I like to replace it with a variable to make things easier to read.  Here's an example:

LOCAL NUMBERVAR QTY := {SALES_ORDER_DETAIL.BVORDQTY};

SWITCH
(
QTY <= {PRICING.BVBREAKQTY01},{PRICING.BVBREAKPRICE01},
QTY <= {PRICING.BVBREAKQTY02},{PRICING.BVBREAKPRICE02},
QTY <= {PRICING.BVBREAKQTY03},{PRICING.BVBREAKPRICE03},
QTY <= {PRICING.BVBREAKQTY04},{PRICING.BVBREAKPRICE04},
QTY <= {PRICING.BVBREAKQTY05},{PRICING.BVBREAKPRICE05},
QTY <= {PRICING.BVBREAKQTY06},{PRICING.BVBREAKPRICE06},
QTY <= {PRICING.BVBREAKQTY07},{PRICING.BVBREAKPRICE07},
QTY <= {PRICING.BVBREAKQTY08},{PRICING.BVBREAKPRICE08},
QTY <= {PRICING.BVBREAKQTY09},{PRICING.BVBREAKPRICE09}
)

Open in new window

0
 
GerhardpetAuthor Commented:
Ok changing to else works but my formula does not work.

@ rhinok, I like your idea better but I have a hard time figuring out how to use the <= and >=

Let's assume my QTY = 25. In this case my cost price should be $250

Here is my table for the BVBREAKQTY (Quantity) and BVBREAKPRICE (Cost price)

qty price
How would I phrase my formula?
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
mlmccCommented:
Try

LOCAL NUMBERVAR QTY := {SALES_ORDER_DETAIL.BVORDQTY};

SWITCH
(
QTY <= {PRICING.BVBREAKQTY01},{PRICING.BVBREAKPRICE01},
QTY > {PRICING.BVBREAKQTY01} AND QTY <= {PRICING.BVBREAKQTY02},{PRICING.BVBREAKPRICE02},
QTY > {PRICING.BVBREAKQTY02} AND QTY <= {PRICING.BVBREAKQTY03},{PRICING.BVBREAKPRICE03},
QTY > {PRICING.BVBREAKQTY03} AND QTY <= {PRICING.BVBREAKQTY04},{PRICING.BVBREAKPRICE04},
QTY > {PRICING.BVBREAKQTY04} AND QTY <= {PRICING.BVBREAKQTY05},{PRICING.BVBREAKPRICE05},
QTY > {PRICING.BVBREAKQTY05} AND QTY <= {PRICING.BVBREAKQTY06},{PRICING.BVBREAKPRICE06},
QTY > {PRICING.BVBREAKQTY06} AND QTY <= {PRICING.BVBREAKQTY07},{PRICING.BVBREAKPRICE07},
QTY > {PRICING.BVBREAKQTY07} AND QTY <= {PRICING.BVBREAKQTY08},{PRICING.BVBREAKPRICE08},
QTY > {PRICING.BVBREAKQTY08} AND QTY <= {PRICING.BVBREAKQTY09},{PRICING.BVBREAKPRICE09}
) 

Open in new window


mlmcc
0
 
GerhardpetAuthor Commented:
It is very close to working

When QTY = 42 I get 180 but it should be 190
When I have QTY >= 51 then I get 0.00 but it should be 170
0
 
Kurt ReinhardtSr. Business Intelligence Consultant/ArchitectCommented:
Based on your example, try this:

LOCAL NUMBERVAR QTY := {SALES_ORDER_DETAIL.BVORDQTY};

SWITCH
(
QTY < {PRICING.BVBREAKQTY02},{PRICING.BVBREAKPRICE01},
QTY >= {PRICING.BVBREAKQTY02} AND QTY < {PRICING.BVBREAKQTY03},{PRICING.BVBREAKPRICE02},
QTY >= {PRICING.BVBREAKQTY03} AND QTY < {PRICING.BVBREAKQTY04},{PRICING.BVBREAKPRICE03},
QTY >= {PRICING.BVBREAKQTY04} AND QTY < {PRICING.BVBREAKQTY05},{PRICING.BVBREAKPRICE04},
QTY >= {PRICING.BVBREAKQTY05} AND QTY < {PRICING.BVBREAKQTY06},{PRICING.BVBREAKPRICE05},
QTY >= {PRICING.BVBREAKQTY06} AND QTY < {PRICING.BVBREAKQTY07},{PRICING.BVBREAKPRICE06},
QTY >= {PRICING.BVBREAKQTY07} AND QTY < {PRICING.BVBREAKQTY08},{PRICING.BVBREAKPRICE07},
QTY >= {PRICING.BVBREAKQTY08} AND QTY < {PRICING.BVBREAKQTY09},{PRICING.BVBREAKPRICE08},
QTY >= {PRICING.BVBREAKQTY09},{PRICING.BVBREAKPRICE09}
) 

Open in new window

0
 
13ShadowCommented:
Just try this:

if {SALES_ORDER_DETAIL.BVORDQTY} > {PRICING.BVBREAKQTY08} then {PRICING.BVBREAKPRICE09} else
if {SALES_ORDER_DETAIL.BVORDQTY} > {PRICING.BVBREAKQTY07} then {PRICING.BVBREAKPRICE08} else
if {SALES_ORDER_DETAIL.BVORDQTY} > {PRICING.BVBREAKQTY06} then {PRICING.BVBREAKPRICE07} else
if {SALES_ORDER_DETAIL.BVORDQTY} > {PRICING.BVBREAKQTY05} then {PRICING.BVBREAKPRICE06} else
if {SALES_ORDER_DETAIL.BVORDQTY} > {PRICING.BVBREAKQTY04} then {PRICING.BVBREAKPRICE05} else
if {SALES_ORDER_DETAIL.BVORDQTY} > {PRICING.BVBREAKQTY03} then {PRICING.BVBREAKPRICE04} else
if {SALES_ORDER_DETAIL.BVORDQTY} > {PRICING.BVBREAKQTY02} then {PRICING.BVBREAKPRICE03} else
if {SALES_ORDER_DETAIL.BVORDQTY} > {PRICING.BVBREAKQTY01} then {PRICING.BVBREAKPRICE02} else
{PRICING.BVBREAKPRICE01}
0
 
Kurt ReinhardtSr. Business Intelligence Consultant/ArchitectCommented:
From the OP:
When QTY = 42 I get 180 but it should be 190
if {SALES_ORDER_DETAIL.BVORDQTY} > {PRICING.BVBREAKQTY07} then {PRICING.BVBREAKPRICE08} else

Open in new window

Using the formula above, an Order Qty of 42 would still result in 180 when it should be 190.  

In order to return Break Price 7, based on the scenario provided by the OP, the Order Qty needs to be >= Break Qty 7 and < Break Qty 8.
0
 
GerhardpetAuthor Commented:
@13Shadow
I get the same results with your formula except for >50 does works.

I'm confused...I know enough about writing formulas to get myself in trouble, which is the case here.

@ rhinok I'll appreciate if you could help with the finish formula.
0
 
13ShadowCommented:
Oh yeah I didn't see that comment.
The original post was asking for a solution to the formula failing with errors.
My solution was a valid one correcting the syntax error due to the invalid use of and.
0
 
13ShadowCommented:
Try this:
if {SALES_ORDER_DETAIL.BVORDQTY} >= {PRICING.BVBREAKQTY09} then {PRICING.BVBREAKPRICE09} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY02} then {PRICING.BVBREAKPRICE01} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY03} then {PRICING.BVBREAKPRICE02} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY04} then {PRICING.BVBREAKPRICE03} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY05} then {PRICING.BVBREAKPRICE04} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY06} then {PRICING.BVBREAKPRICE05} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY07} then {PRICING.BVBREAKPRICE06} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY08} then {PRICING.BVBREAKPRICE07} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY09} then {PRICING.BVBREAKPRICE08}
0
 
Kurt ReinhardtSr. Business Intelligence Consultant/ArchitectCommented:
My last code sample accounts for your scenario.  13Shadow's last formula should work, too.  It basically does the same thing as mine, except it relies on implicit predicate order instead of explicitly declaring ranges.

FYI - it doesn't really matter if you use the IF-THEN-ELSE or the SWITCH (or even a SELECT) in this case.  They'll all accomplish the same thing.  I personally just prefer the "cleanliness" of one vs. the other.
0
 
GerhardpetAuthor Commented:
@13Shadow
Your formula works now but one more thing that I did not see coming

In most cased only the first 3 lines have qty break pricing. Now my formula for QTY 25 looks at BVBREAKQTY09 and print 0.00 because BVBREAKQTY09 = 0 for qty

Here is what it looks like in the live data
qty break
0
 
Kurt ReinhardtSr. Business Intelligence Consultant/ArchitectCommented:
So, in most cases, there are only three breaks?  Is the maximum number of breaks always going to be nine or can it be even more? In the example above, you would want to show $256 as the price then the ordered QTY was >= 25, right?  In your example above, you explicitly list "0" as values for unpopulated breaks.  Will that always be the case or will those fields be NULL?


Assuming there is a maximum of nine possible breaks AND "unused" breaks are populated with zeroes, try the following:

//The following variable is to evaluate and assign the order quantity
LOCAL NUMBERVAR OQTY := {SALES_ORDER_DETAIL.BVORDQTY};

//The following variables are to evaluate and assign the various possible break quantities
LOCAL NUMBERVAR BQTY2:= {PRICING.BVBREAKQTY02};
LOCAL NUMBERVAR BQTY3:= {PRICING.BVBREAKQTY03};
LOCAL NUMBERVAR BQTY4:= {PRICING.BVBREAKQTY04};
LOCAL NUMBERVAR BQTY5:= {PRICING.BVBREAKQTY05};
LOCAL NUMBERVAR BQTY6:= {PRICING.BVBREAKQTY06};
LOCAL NUMBERVAR BQTY7:= {PRICING.BVBREAKQTY07};
LOCAL NUMBERVAR BQTY8:= {PRICING.BVBREAKQTY08};
LOCAL NUMBERVAR BQTY9:= {PRICING.BVBREAKQTY09};

SWITCH
(
OQTY < BQTY2 OR BQTY2 = 0,{PRICING.BVBREAKPRICE01},
OQTY >= BQTY2 AND (OQTY < BQTY3 OR BQTY3 = 0),{PRICING.BVBREAKPRICE02},
OQTY >= BQTY3 AND (OQTY < BQTY4 OR BQTY4 = 0),{PRICING.BVBREAKPRICE03},
OQTY >= BQTY4 AND (OQTY < BQTY5 OR BQTY5 = 0),{PRICING.BVBREAKPRICE04},
OQTY >= BQTY5 AND (OQTY < BQTY6 OR BQTY6 = 0),{PRICING.BVBREAKPRICE05},
OQTY >= BQTY6 AND (OQTY < BQTY7 OR BQTY7 = 0),{PRICING.BVBREAKPRICE06},
OQTY >= BQTY7 AND (OQTY < BQTY8 OR BQTY8 = 0),{PRICING.BVBREAKPRICE07},
OQTY >= BQTY8 AND (OQTY < BQTY9 OR BQTY9 = 0),{PRICING.BVBREAKPRICE08},
OQTY >= BQTY9,{PRICING.BVBREAKPRICE09}
)

Open in new window

You don't necessarily need all the variables, but it makes the code a littler easier to read, since you won't have the field names replicated so many times.
0
 
13ShadowCommented:
Try this:
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY02} or {PRICING.BVBREAKQTY02} = 0 then {PRICING.BVBREAKPRICE01} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY03} or {PRICING.BVBREAKQTY03} = 0 then {PRICING.BVBREAKPRICE02} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY04} or {PRICING.BVBREAKQTY04} = 0 then {PRICING.BVBREAKPRICE03} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY05} or {PRICING.BVBREAKQTY05} = 0 then {PRICING.BVBREAKPRICE04} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY06} or {PRICING.BVBREAKQTY06} = 0 then {PRICING.BVBREAKPRICE05} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY07} or {PRICING.BVBREAKQTY07} = 0 then {PRICING.BVBREAKPRICE06} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY08} or {PRICING.BVBREAKQTY08} = 0 then {PRICING.BVBREAKPRICE07} else
if {SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY09} or {PRICING.BVBREAKQTY09} = 0 then {PRICING.BVBREAKPRICE08} else
if {SALES_ORDER_DETAIL.BVORDQTY} >= {PRICING.BVBREAKQTY09} then {PRICING.BVBREAKPRICE09}
0
 
Kurt ReinhardtSr. Business Intelligence Consultant/ArchitectCommented:
Here's the SWITCH equivalent of the above statement (including variables).

//The following variable is to evaluate and assign the order quantity
LOCAL NUMBERVAR OQTY := {SALES_ORDER_DETAIL.BVORDQTY};

//The following variables are to evaluate and assign the various possible break quantities
LOCAL NUMBERVAR BQTY2:= {PRICING.BVBREAKQTY02};
LOCAL NUMBERVAR BQTY3:= {PRICING.BVBREAKQTY03};
LOCAL NUMBERVAR BQTY4:= {PRICING.BVBREAKQTY04};
LOCAL NUMBERVAR BQTY5:= {PRICING.BVBREAKQTY05};
LOCAL NUMBERVAR BQTY6:= {PRICING.BVBREAKQTY06};
LOCAL NUMBERVAR BQTY7:= {PRICING.BVBREAKQTY07};
LOCAL NUMBERVAR BQTY8:= {PRICING.BVBREAKQTY08};
LOCAL NUMBERVAR BQTY9:= {PRICING.BVBREAKQTY09};

SWITCH
(
OQTY < BQTY2 OR BQTY2 = 0,{PRICING.BVBREAKPRICE01},
OQTY < BQTY3 OR BQTY3 = 0,{PRICING.BVBREAKPRICE02},
OQTY < BQTY4 OR BQTY4 = 0,{PRICING.BVBREAKPRICE03},
OQTY < BQTY5 OR BQTY5 = 0,{PRICING.BVBREAKPRICE04},
OQTY < BQTY6 OR BQTY6 = 0,{PRICING.BVBREAKPRICE05},
OQTY < BQTY7 OR BQTY7 = 0,{PRICING.BVBREAKPRICE06},
OQTY < BQTY8 OR BQTY8 = 0,{PRICING.BVBREAKPRICE07},
OQTY < BQTY9 OR BQTY9 = 0,{PRICING.BVBREAKPRICE08},
OQTY >= BQTY9,{PRICING.BVBREAKPRICE09}
)

Open in new window

Here's the same thing, minus all variables:
SWITCH
(
{SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY02} OR {PRICING.BVBREAKQTY02} = 0,{PRICING.BVBREAKPRICE01},
{SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY03} OR {PRICING.BVBREAKQTY03} = 0,{PRICING.BVBREAKPRICE02},
{SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY04} OR {PRICING.BVBREAKQTY04} = 0,{PRICING.BVBREAKPRICE03},
{SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY05} OR {PRICING.BVBREAKQTY05} = 0,{PRICING.BVBREAKPRICE04},
{SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY06} OR {PRICING.BVBREAKQTY06} = 0,{PRICING.BVBREAKPRICE05},
{SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY07} OR {PRICING.BVBREAKQTY07} = 0,{PRICING.BVBREAKPRICE06},
{SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY08} OR {PRICING.BVBREAKQTY08} = 0,{PRICING.BVBREAKPRICE07},
{SALES_ORDER_DETAIL.BVORDQTY} < {PRICING.BVBREAKQTY09} OR {PRICING.BVBREAKQTY09} = 0,{PRICING.BVBREAKPRICE08},
{SALES_ORDER_DETAIL.BVORDQTY} >= {PRICING.BVBREAKQTY09},{PRICING.BVBREAKPRICE09}
)

Open in new window

Either of these could be swapped for 13Shadow's latest code, but doing so would be purely a matter of preference.  All three statements do the exact same thing. I'm simply just demonstrating differences in syntax at this point.   If any of these statements give you the results you want, great!
0
 
GerhardpetAuthor Commented:
@rhinok
You are right both yours and @13Shadow are working.

Thank you to both of you  for your help.

To do these formulas is way beyond me...

Best for the new year!
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

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 6
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now