Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
pawe84
Creator
Creator

How filter all records which contribute nearly 95% of total sum?

Hi everyone,

 

I have a table with sales data like this, the table is already ordered by sales desc:

 

0683p000009M9BF.png


Now I only need the invoice numbers which contribute nearly 95% of total sales.

In this example total sales=7927 , 95% sales = 7530.65


Now we can see that only the sum of invoice number 101 and 100 (7400) are very close to 7530.65.

If I have several thousand records how I can filter those qualified records?

 

Do I have to use modules like t_aggregate and t_sort?

 

Thanks in advance for any hints.

Labels (3)
1 Solution

Accepted Solutions
TRF
Champion II
Champion II

Do you want to stop the process as soon as a row hits the target?

If so, change the expression for the currentSum variable to:

Var.currentSum + row5.sales

Don't change anything else.

It should work. 

View solution in original post

5 Replies
TRF
Champion II
Champion II

If the objective is to get the n high sales to reach the target of 95% of the total sales, here is a simple solution:

0683p000009M93b.png

Bith tFixedFlowInput are here to produce the sample data set.

tJava and tLogRow are here to display target (95% of all sales) and final result.

With 1rst subjob, compute the total sales with tAggregatRow then the target stored into a global variable:

0683p000009M9Cr.png 

 

0683p000009M9Cw.png

For 2nd subjob, order rows based on the sales field in descending order then in tMap, add each sales value as soon as the result is less or equal to the target (else, ignore the current row and get the next one and so on):

0683p000009M9D6.png

Here is the result:

0683p000009M9AX.png

If the objective is to get the better solution to reach 95% of the sales value, this is a complete different story as you have to try all possible combinations...

pawe84
Creator
Creator
Author

Hi thank's,

this solution is nearly perfect.

But I don't need the invoiceno 105, because after sales has been sorted descending and it should check only 

if the current sum >= target sum depending of the order.

 

In this case  invoiceno 103 would be the next but the sum (sales) of 101+100+103 > 7530.65

That means only 101 and 100 are qualified.

 

I think I have to modify the variable currentSum, isn't it?

 

 

TRF
Champion II
Champion II

Do you want to stop the process as soon as a row hits the target?

If so, change the expression for the currentSum variable to:

Var.currentSum + row5.sales

Don't change anything else.

It should work. 

pawe84
Creator
Creator
Author

Thanks, that's exacly what I was looking for.
TRF
Champion II
Champion II

Great!

Don't forget to mark your case as solved (Kudos also accepted as a bonus).