# QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

Announcements
If you are a Qlik Insight Bot user, join this collaborative group: JOIN GROUP
Not applicable

## set analysis with sum(if()) in the script

Dear all,

I have a set analayis in a chart which is computing:

if the AVG of the months 1 to 3 is bigger than 1.1*AVG of months 4 to 6, the AVG of months 1 to 3 is taken into consideration, otherwise the AVG of months 1 to 6.

the set analysis looks like follow:

`if((Sum({\$<BUCKET={">=-3 <=-1"}>} BucketQty)/3) > 1.01*(Sum({\$<BUCKET={">=-6 <=-4"}>} BucketQty)/3),          (Sum({\$<BUCKET={">=-3 <=-1"}>} BucketQty)/3),                    (Sum({\$<BUCKET={">=-6 <=-1"}>} BucketQty)/6))`

Now,I am trying to do this in-script for different reasons.

I tried the approach with LET, i.e.

`LET vTrend=if((Sum({\$<BUCKET={">=-3 <=-1"}>} BucketQty)/3) > [...]`

I tried the approach of SUM(If(condition, value))

but still not working

`if(Sum(if( BUCKET>=(-3) and BUCKET<=(-1)), BucketQty/3) > 1.01*(Sum(if(BUCKET>=(-6) and BUCKET<=(-4)), BucketQty/3)),Sum(if(BUCKET>=-3 and BUCKET <=-1), BucketQty/3),Sum(BUCKET>=-6 and BUCKET <=-1), BucketQty/6) as CalcTrend;`

Is it thus possible to achieve that set anallysis above mentioned, in the script?

Many thanks

7 Replies
Highlighted
Not applicable

## Re: set analysis with sum(if()) in the script

Hi,

you have to use this syntax in script langiage

(if..then, elseif..then, else end if)

and not:

if(condition,'','')

Hope it helps

Regards

Giampiero

Highlighted
Not applicable

## Re: set analysis with sum(if()) in the script

Sorry,

if you're calculating for fields you're right.

Highlighted
Not applicable

## Re: set analysis with sum(if()) in the script

 giampiero.cina wrote:Sorry,if you're calculating for fields you're right.

nice

but then it is still not working!

forgot to mention, this table is made of thousands of rows, where each one of them is an item.

For each item there are 12 buckets, i basically to have a further field being the weighted average described as above.

Highlighted
Not applicable

## Re: set analysis with sum(if()) in the script

Hi,

Have you tried  in this way :

if (if(BUCKET=>=-3 and BUCKET<=-1,sum(BucketQty)/3) > if(BUCKET=>=-6 and BUCKET<=-4,sum(BucketQty)/3) * 1.01, if(BUCKET=>=-3 and BUCKET<=-1,sum(BucketQty)/3), if(BUCKET=>=-6 and BUCKET<=-1,sum(BucketQty)/6)) as CalcTrend;

Hope it helps

Regards

Giampiero

Highlighted
Not applicable

## Re: set analysis with sum(if()) in the script

I needed to change a bit your script to have the colours appear in the script.

(namely, from

`BUCKET=>=-3`

to

`BUCKET>=-3`

and so on...)

`Invalid expressionBucketDemand:LOAD *,if (if(BUCKET>=-3 and BUCKET<=-1,sum(BucketQty)/3) > if(BUCKET>=-6 and BUCKET<=-4,sum(BucketQty)/3) * 1.01,if(BUCKET>=-3 and BUCKET<=-1,sum(BucketQty)/3),if(BUCKET>=-6 and BUCKET<=-1,sum(BucketQty)/6)) as CalcTrendRESIDENT BucketDemandTemp`

so still not good....

Highlighted
Not applicable

## Re: set analysis with sum(if()) in the script

Hi,

could you post a qvw to see how to help you ?

Regards

Giampiero

Highlighted
Not applicable

## Re: set analysis with sum(if()) in the script

Hi,

I think the problem now is not the IF command but using the Sum function you have to use the group by.

I attach a simple example to understand:

FieldA,

Sum(FieldB) as SumFieldB,

FieldC

FROM

(ooxml, embedded labels, table is test)

group by Id,FieldA,FieldC;

group by is obligatory.