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: 
AmCh
Creator
Creator

Calculations in script

Hello all,

I have already imported those fields as an example:

Field_a
9
8
4
2

 

Year
1
2
3
4
5
6

 

My ouput should look like:

Operation: If value (Field_a) >5 then value(Field_b)=value(Field_a)*2*Year else value(Field_b)=value(Field_a)*Year/2 

NB: These operations should be performed in the script and not as a chart expression.

Year/Field _b    
13456
2681012
3...   
4 ...  
5  ... 
6   ...

 

Any help please?

Thanks in advance

7 Replies
mfchmielowski
Creator II
Creator II

Hi

First attempt would be like this:

Fields:
load * inline [
Field_a
9
8
4
2
];

join(Fields)
Years:
load * inline [
Year
1
2
3
4
5
6
];

NoConcatenate
Result:
load Field_a, Year, if(Field_a > 5, Field_a*2*Year, Field_a*Year/2) as Field_b Resident Fields;

drop table Fields;

This code will give you output like this:

mfchmielowski_0-1620243707659.png

But one thing is weird. The output format suggest that you want to have many columns named Field_b - that's impossible.

AmCh
Creator
Creator
Author

Hi,

thank you for your prompt reply.

It works but I have problem with non integers. Multiplication cannot be performed with non integers.

What could be the solution when Field_a would look like:

Field_a

9.81

8.8

4.8114

2.815

 

Thanks in advance.

mfchmielowski
Creator II
Creator II

Hi, what is your source of thouse digits?
I've changed values in my inline load and it works correctly.

AmCh
Creator
Creator
Author

The same with non integers:

Fields:
load * inline [
Field_a
9.8
8.8
4.8
2.8
];

join(Fields)
Years:
load * inline [
Year
1
2
3
4
5
6
];

NoConcatenate
Result:
load Field_a, Year, if(Field_a > 5, Field_a*2*Year, Field_a*Year/2) as Field_b Resident Fields;

drop table Fields;

 With the following output:

AmCh_0-1620588242077.png

 

 

mfchmielowski
Creator II
Creator II

Hmm that's weird. Same loadScript on my qlik gives different results.
When you check the model with ctrl+t what qlikview says about type of Field_a ?

mfchmielowski_0-1620631446823.png

I'm using polish language in qlik so presentation can be different but the $numeric info should be the same 😉

Maybe try to force this field with num#(Field_a) as Field_a in load.

AmCh
Creator
Creator
Author

ascii and text and it didn't work with num# as well.

mfchmielowski
Creator II
Creator II

And what is the field type of field_a in model viewer?