Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
gauthamchilled
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
micheledenardi
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))

Michele De Nardi
If a post helps to resolve your issue, please accept it as a Solution.
gauthamchilled
Creator
Creator
Author

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

micheledenardi
Specialist II
Specialist II

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

Michele De Nardi
If a post helps to resolve your issue, please accept it as a Solution.
gauthamchilled
Creator
Creator
Author

i am using in chart expression

micheledenardi
Specialist II
Specialist II

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

Michele De Nardi
If a post helps to resolve your issue, please accept it as a Solution.
gauthamchilled
Creator
Creator
Author

nested in same expression.

micheledenardi
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.

Michele De Nardi
If a post helps to resolve your issue, please accept it as a Solution.
antoniotiman
Master III
Master III

Hi,

I think You don't need IF()

Maybe

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

Regards,

Antonio

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