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

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
razvan_brais
Creator III
Creator III

Total sum of multiple ID`s

Hy guys. Hope you can help me with one problem.

I want to mark some id`s with 'A' or 'P' depending on the sum of all ID`s.

For example i have this values

IDValue
110
2-20
35
4-10

I would like to calculate the total sum . S =10+ (-20) + 5 + (-10) = -15 and if  S >=0 then Flag = 'A ' else Flag = 'P';

The final table should look something like this :

IDValueFlag
110P
2

-20

P
35P
4-10P

The flag of each ID is P because the sum of all values is lower than 0.

I tried with this script :

load

ID,

if(Value>0,'A','P') as Flag

;

load

ACCT ,

sum(Value) as Value

Resident Table1

Where Wildmatch(ID,'1','2','3','4');

But it`s not ok.

Can you please help me with some suggestions?

Thank you

1 Solution

Accepted Solutions
sunny_talwar

But why an extra load when it can be done in two?

Table:

LOAD *,

  If(Match(ID, 1, 2, 3, 4), 'Needed') as NeededStuff;

LOAD * INLINE [

    ID, Value

    1, 10

    2, -20

    3, 5

    4, -10

    5, -100

    6, 20

    7, 35

    8, 40

    9, 30

];


Left Join (Table)

LOAD NeededStuff,

    If(Sum(Value) >= 0, 'A', 'P') as Flag

Resident Table

Where NeededStuff = 'Needed'

Group By NeededStuff;


Capture.PNG

View solution in original post

18 Replies
Anil_Babu_Samineni

May be this?

Sample:

LOAD * Inline [

ID, Value

1, 10

2, -20

3, 5

4, -10

];

Left Join

LOAD If(Sum(Value)>0, 'A','P') as Flag

Resident Sample;

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
sunny_talwar

Try this

Table:

LOAD ID,

     VALUE

From ....;

Left Join (Table)

LOAD If(Sum(VALUE) < 0, 'A', 'P') as Flag

Resident Table;

timpoismans
Specialist
Specialist

Hello

A possible solution:

     If(Sum(total Value)>0,'A','P')

This will calculate the total sum of all Values and evaluate the If condition based on the result.
This is a measure in a table though, and not a part of the script.

Total Value.PNG

razvan_brais
Creator III
Creator III
Author

This is what i`m looking for , but i need to calculate in script , not as an expression in interface.

sunny_talwar

Have you tried this already?

timpoismans
Specialist
Specialist

Sunny's script should work from what I can tell then as a solution.

razvan_brais
Creator III
Creator III
Author

Sorry that I haven`t been completely clear.

I want to calculate the sum only for specific ID`s and mark thos specific ID`s with A or P.

IDValueFlag
110P
2

-20

P
35P
4-10P
5-100
620
735
840
930

So the sum should be calculated only for ID`s from 1 to 4 , and mark only those 4 ID`s , the rest should remain blank.

With Sunny`s method all ID`s from the table will get the Flag 'P' .

sunny_talwar

Try this

Table:

LOAD ID,

    If(Match(ID, 1, 2, 3, 4), 'Needed') as NeededStuff,

    VALUE

From ....;

Left Join (Table)

LOAD NeededStuff,

    If(Sum(VALUE) < 0, 'A', 'P') as Flag

Resident Table

Where NeededStuff = 'Needed'

Group By NeededStuff;

timpoismans
Specialist
Specialist

Based on Sunny's script:

Table:

LOAD

     ID,

     If(Match(ID, 1, 2, 3, 4), 'Needed') as NeededStuff,

     Value

From ....;

Left Join (Test)

LOAD

If(Sum(Value) > 0, 'A', 'P') as Flag

Resident Test

Where NeededStuff = 'Needed';

Test2:

Load

     ID,

     Value,

     If(NeededStuff='Needed',Flag,Null()) as Flag

Resident Test;

Drop Table Test;