Qlik Community

Ask a Question

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 
Search instead for 
Did you mean: 
Creator
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
Specialist II

you can create 3 variables:

1) vTotalProfit: sum(profit)

2) vTotalRevenue: sum(revenue)

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


and re-write your expression like:

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
Creator

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

Specialist II
Specialist II

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

Creator
Creator

i am using in chart expression

Specialist II
Specialist II

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

Creator
Creator

nested in same expression.

Specialist II
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
Master III

Hi,

I think You don't need IF()

Maybe

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

Regards,

Antonio

Specialist
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))