Skip to main content
Announcements
Qlik Introduces a New Era of Visualization! READ ALL ABOUT IT
cancel
Showing results for 
Search instead for 
Did you mean: 
tnunes
Partner - Contributor II
Partner - Contributor II

Sum deeper than filter

Hi community, 

I need to create a sum with a specific critéria. 

Table example:

A | B | C | D

1 | 1 | 4 | 3

1 | 2 | 7 | 2

1 | 3 | 7 | 20

Result needed:

A | R

1 | 6

Result is calculated with the following condition: if (C-D > 0) then C-D else 0, so the "aux" table should be : 

A | B | C - D with condition

1 | 1 | 1

1 | 2 | 5

1 | 3 | 0

And I need to sum.

Labels (1)
5 Replies
Lech_Miszkiewicz
Partner Ambassador/MVP
Partner Ambassador/MVP

HI @tnunes 

does your test if (C-D > 0) then C-D else 0 have to be done on each row level separatelly? 

If yes - your bet is to do it in load script during data load as that will yield much better performance:

If(C-D > 0, C-D, 0) as E

then in UI you just do Sum(E)

cheers Lech, 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 to the problem.
TauseefKhan
Creator III
Creator III

Hello @tnunes 

Result needed:     
TauseefKhan_0-1715917627260.png

Script:

Table:
LOAD * INLINE [
A, B, C, D
1, 1, 4, 3
1, 2, 7, 2
1, 3, 7, 20
];

// Create calculated column
TempTable:
LOAD *,
If(C-D > 0, C-D, 0) as [C-D_with_condition]
RESIDENT Table;

// Create new table with calculated column
ResultTable:
LOAD A,
Sum([C-D_with_condition]) as R
RESIDENT TempTable
GROUP BY A;

**If the Above solution is correct/appropriate replies as "solution". Please LIKE threads if the provided solution is helpful to.**

Vegar
MVP
MVP

An alternative to the if() statement suggested by others is to use rangemax(), like this.

Table:

LOAD *, rangemax(C-D, 0) as R INLINE [

A, B, C, D

1, 1, 4, 3

1, 2, 7, 2

1, 3, 7, 20

];

tnunes
Partner - Contributor II
Partner - Contributor II
Author

Hi, 

Thanks for all the replies. 

I think using this in data load I will not have the result that I want since it depends on all filters. 

I am trying to use something like "Sum(Aggr(Sum()))" , so Sum all the C-D, agreggate and Sum all to get A result.

Firefly_cam
Partner - Contributor III
Partner - Contributor III

Load * inline [
A | B | C | D

1 | 1 | 4 | 3

1 | 2 | 7 | 2

1 | 3 | 7 | 20] (delimiter is '|');

 

Measure would be:

Sum((C-D)*fabs(C>D))

 Screenshot 2024-05-17 at 22.10.16.png

Regards, Roman