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

Announcements
Qlik Connect 2026 Agenda Now Available: Explore Sessions
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

TJavaRow with condition

Hi,

I have this example:

"reste"=Voix/(supp+1)

for example, for code1: "number" to distribute =2

1) for "rest" = 584 / (2+1)=194.66 
    for "rest" = 840 / (3+1) =210 
    for "rest" = 863 / (3+1) =215.75 we assign a "Number"=1 -> Supp = Supp+1 ( because 215,75 is the max)

2) for "rest" = 584 / (2+1)=194.66 
    for "rest" = 840 / (3+1) =210 we assign a "Number"=1 -> Supp = Supp+1 ( because 210 is the max)

    for "rest" = 863 / (3+1+1) =172.6

result:

code|Supp

   1   |   2

   1   |  4  (3+1)

   1    |  4 (3+1)  

and we repete for each group of code the same procedure precedent.

I try this code but il doesn't works0683p000009M5EK.png0683p000009M5EP.png

 

Labels (3)
6 Replies
Anonymous
Not applicable
Author

Can you share the code as text please? It is easier to try out that way 🙂

Anonymous
Not applicable
Author

output_row.code = input_row.code;
output_row.number = input_row.number;
output_row.VOIX = input_row.VOIX;
output_row.supp = input_row.supp;
output_row.rest = input_row.rest;


String code = input_row.code;
int num = input_row.number;
//double reste =Double.valueOf(input_row.VOIX/(input_row.supp+1+1));

if( code.contains(context.current_code) ) {
context.to_distribute -= 1 ;
output_row.supp += ( 0 > context.to_distribute ? 0 : 1 );
context.reste= Double.valueOf(input_row.VOIX/(input_row.supp+1+1));

} else if (context.reste >= input_row.rest) {
context.current_code = input_row.code;
context.to_distribute = input_row.number - 1;
context.reste= Double.valueOf(input_row.VOIX/(input_row.supp+1+1));

output_row.supp += ( 0 > context.to_distribute ? 0 : 1 );
output_row.rest=context.reste;
}

Anonymous
Not applicable
Author

The main problem you have is that you are assigning the output_row values at the very beginning. These are not normal variables. You should only assign these once, once you have calculated your values. You are also trying to increment output_row values, this is not ideal either.

 

Your IF conditions look a little confusing, but that could just be the logic you want. What if neither of the conditions are true? Do you require an "else"?

 

I have adjusted your code so that it will work. The logic may not be quite correct (as explained briefly above), but this will change the values when the conditions are met.

 

//output_row.code = input_row.code;
//output_row.number = input_row.number;
//output_row.VOIX = input_row.VOIX;
//output_row.supp = input_row.supp;
//output_row.rest = input_row.rest;

int VOIX = input_row.VOIX;
int supp = input_row.supp;
double rest = input_row.rest;

String code = input_row.code;
int number = input_row.number;

if( code.contains(context.current_code) ) {
	context.to_distribute -= 1 ;
	context.reste= ((double)VOIX)/(((double)supp)+1.0+1.0);
	supp += ( 0 > context.to_distribute ? 0 : 1 );
	output_row.supp = supp;
	output_row.code = code;
	output_row.number = number;
	output_row.VOIX = VOIX;
	output_row.rest=context.reste;

} else if (context.reste >= rest) {
	context.current_code = code;
	context.to_distribute = number - 1;
	context.reste= ((double)VOIX)/(((double)supp)+1.0+1.0);
	supp += ( 0 > context.to_distribute ? 0 : 1 );
	output_row.supp = supp;
	output_row.rest=context.reste;
	output_row.code = code;
	output_row.number = number;
	output_row.VOIX = VOIX;

}
Anonymous
Not applicable
Author

But my problem is that I can not apply the logic of my example. how can I distribute "number" each time on the value of the maximum "rest"? is that this java code is enough or I have to apply a loop?

Anonymous
Not applicable
Author

I'm afraid I do not understand the logic. Your example does not give enough information as to why each of the equations take place? Maybe a simple example of an input table and expected output table would help? You have to remember that we do not have the business requirement or your understanding of what you are trying to achieve. A lot of the time I can infer from input to output (spotting the logical flow), but I just don't see it here I am afraid.

Anonymous
Not applicable
Author

the input is :
code|number|rest|supp
1 |2 |150 |4
1 |2 |125 |1
1 |2 |110 |1
2 |3 |145 |0
2 |3 |130 |3
2 |3 |50 |1
2 |3 |10 |4

The output of the first transformation is :
1 |2 |150 |5
1 |2 |125 |2
1 |2 |110 |1
2 |3 |145 |1
2 |3 |130 |4
2 |3 |50 |2
2 |3 |10 |4

And you said : 

"reste"=Voix/(supp+1)

what is "Voix" ?

You added :
for example, for code1: "number" to distribute =2

1) for "rest" = 584 / (2+1)=194.66 // where is 2 from ? is it because number=2 ?
for "rest" = 840 / (3+1) =210 // where is 3 from ?
for "rest" = 863 / (3+1) =215.75 // where is 3 from ?