# New to QlikView

If you’re new to QlikView, start with this Discussion Board and get up-to-speed quickly.

Announcements
Qlik Highlights 2020 Giveaway! Watch, reply and have a chance to win a \$200 Amazon Gift Card! Watch Video
cancel
Showing results for
Did you mean:
Creator

## how to simplify this if statement

hi all

i have this condition, regardless of revenue if profit is positive number i have to show +ve margin otherwise negative number

how can I achieve this?

if sum(profit) < 0 and sum(revenue) < 0, -(sum(profit)/sum(revenue)

if sum(profit) < 0 and sum(revenue) > 0, -(sum(profit)/sum(revenue)

if sum(profit)> 0 and sum(revenue) < 0, (sum(profit)/sum(revenue)

if sum(profit)> 0 and sum(revenue) > 0, (sum(profit)/sum(revenue)

can you simplify the above if statement?

Regards

Gautham

10 Replies
Specialist II

you can create 3 variables:

1) vTotalProfit: sum(profit)

2) vTotalRevenue: sum(revenue)

3) vProfit/Revenue: (sum(profit)/sum(revenue)

if(\$(vTotalProfit) < 0 and \$(vTotalRevenue)<0, -\$(vProfit/Revenue))

if(\$(vTotalProfit) < 0 and \$(vTotalRevenue) > 0, -\$(vProfit/Revenue))

if(\$(vTotalProfit) > 0 and \$(vTotalRevenue) < 0, \$(vProfit/Revenue))

if(\$(vTotalProfit) > 0 and \$(vTotalRevenue) > 0, \$(vProfit/Revenue))

Creator

is there a way to simplify this if statement??? with respect to performance

Specialist II

Are your if statements nested or do you use them in separate charts  ?

Creator

i am using in chart expression

Specialist II

Yes, but you are using it in different expressions or nested in the same expression ?

Creator

nested in same expression.

Specialist II

could be this ?

if(

sum(profit) < 0 and sum(revenue) <> 0, -(sum(profit)/sum(revenue))

,

if(sum(profit)> 0 and sum(revenue) <> 0, (sum(profit)/sum(revenue))

)

)

but if you want to improve your document performance you have to use variable or do the sum() during load.

Master III

Hi,

I think You don't need IF()

Maybe

Margin --> Sum(profit)/Fabs(Sum(revenue))

Regards,

Antonio

Specialist

Hello, Gautham!

This is little bit shorter:

if((sum(profit)+sum(revenue))<0 or (sum(profit)<0 and sum(revenue)>0),-(sum(profit)/sum(revenue))

if((sum(profit)+sum(revenue))>0 or (sum(profit)>0 and sum(revenue)<0),(sum(profit)/sum(revenue))

Tags