Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
krishna20
Specialist II
Specialist II

If Condition is not working as expected in script or expression

Hi Folks,

Very strange, simple If condition is not working as expected. Whenever I used in the text box, it shows null and in the script, It's throwing the error as invalid expression. Can anyone suggest me why it behaves like this?

If condition  I am loading at load statement in the script by taking resident.

IF([Type]='Champion',0,IF(Sum([Sales])<=(Sum([LP])*Sum([Quantity])),(Sum([ Net Price])*Sum([Discount])))) as Test

The expected output is, Type=Champion is not there in my current data, It should goes to else part to Second If condition.

Sum(Sales) =1158 and Sum(LP)* Sum(Quantity) =1158 so, I need Sum(Net Price)* Sum(Discount) amount.

Thanks

Krishna

Labels (3)
2 Solutions

Accepted Solutions
forte
Partner - Creator
Partner - Creator

Hi @krishna20 

If you need aggregation functions ( as sum, count etc) loading from a table (resident via in your case), you need to use group by clause to tell Qlik for wich dimension you want calculate.

That's why you are receiving such "invalid expression" script error.

Hope it helps

Regards

View solution in original post

krishna20
Specialist II
Specialist II
Author

Hi Anil,

Yes, I need the green part to be calculated. I fixed this issue by taking all above expression to backend and group by with all the dimensions. Don't know why it was not calculating at front end even though the second condition satisfies. Thanks for your inputs and @forte .

Thanks 

Krishna

View solution in original post

12 Replies
Anil_Babu_Samineni

Hi, You have include Else part for both fail cases like

IF([Type]='Champion',0,IF(Sum([Sales])<=(Sum([LP])*Sum([Quantity])),(Sum([ Net Price])*Sum([Discount])), (Sum([ Net Price])*Sum([Discount])))) as Test

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
krishna20
Specialist II
Specialist II
Author

Hi Anil,

Thanks for your reply.

I cannot do that because, I am having another condition for else. The if condition is failing at stage 2 itself.

My Actual expression is

If([ Type]='Champion',0,
if(Sum([Sales])<=(Sum([LP])*Sum([Quantity])),(Sum([ Net Price])*Sum([Discount]))
, (Sum([Sales])-(Sum([Promo LP])*Sum([Quantity])))/(Sum([Sales])*Sum([Net Price]))))

forte
Partner - Creator
Partner - Creator

Hi @krishna20 

If you need aggregation functions ( as sum, count etc) loading from a table (resident via in your case), you need to use group by clause to tell Qlik for wich dimension you want calculate.

That's why you are receiving such "invalid expression" script error.

Hope it helps

Regards

krishna20
Specialist II
Specialist II
Author

Anil_Babu_Samineni

If that is the case, How come it is multiplying with LP?

(Sum([LP])*Sum([Quantity]))

I believe, There is no script error during reload. If so, try this?

IF(Sum([Sales])<=Sum([Quantity]),(Sum([ Net Price])*Sum([Discount]), 'Else Condition If needed')

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
krishna20
Specialist II
Specialist II
Author

Hi Anil,

It is my requirement and condition should satisfies with Sum([Sales])<=(Sum([LP])*Sum([Quantity]). We cannot ignore Sum(LP)*Sum(Quantity). 

Anil_Babu_Samineni

The reason I asked because of your explanation (In that there is nothing LP)

Anyway, What are you getting using this

IF(Sum([Sales])<=(Sum(LP)*Sum([Quantity])),(Sum([ Net Price])*Sum([Discount]), 'Else Condition If needed')

Can you please demonstrate the similar issue in sample applicaiton which may help quicker.

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
krishna20
Specialist II
Specialist II
Author

Hi Anil,

Unable to share sample app due to data privacy, I am attaching snippet from my actual data.

IF([Type]='Champion',0,IF(Sum([Sales])<=(Sum([LP])*Sum([Quantity])),(Sum([ Net Price])*Sum([Discount])))) 

krishna20_1-1604831719439.png

Just elaborating my expression, I have Type = 'Volume' not Champion. So, first condition fails and goes to else condition. Sum(Sales)=135,205 and LP* Quantity= 135,205. Second condition satisfies and should go to Netprice * Discount=14,499. But, It's displaying '-'.

I am not getting, how it is working in this situation 😞

 

 

chrismarlow
Specialist II
Specialist II

Hi,

This could be a total red herring, but noting you have a space at the start of [ Net Price], is that actually in your fields or an issue when copying over? Do you get an error in the UI when you see '-' in text box?

I think in the original post you mentioned using in the load script - if you did not have appropriate grouping you would get Invalid Expression error, that might be worth investigating.

From experience I think you may also need to look at the order you sum in, depending how you aggregate, so if Discount were a percentage say I think I would expect to see Sum([Net Price]*[Discount]) rather than Sum([Net Price])*Sum([Discount]) ... but there may be some quirks in your data model that are not obvious.

Cheers,

Chris.