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: 
Not applicable

New calculated field in Load Script (2 tables 2 conditions)

Hello Qlikview Community

I am loading Posted invoices and Electronic XML invoices from Oracle.

In the process I realized that Credit Memos were not handled as expected as they sometimes is negative and sometimes positive values in amount in the XML version of the Invoice.

Basically I need to test if INVOICE_TYPE is CREDIT and AMOUNT => 0 then * -1 otherwise * 1 in the load script and create a new field with the calculated value (AMOUNT_CORR) in the XML_INVOICE Table.

INVOICE:

INVOICE_IDINVOICE_TYPE
1000CREDIT
2000INVOICE
3000CREDIT

XML_INVOICE

INVOICE_IDLINEAMOUNTITEM_ID
100011011
200011015
200021517
30001-2015
30002-1011

I tried something like the below script, but cant seem to work it out properly. I saw a similar request on the community but the user only had 1 condition (INVOICE_TYPE) and I have to test 2 conditions before calculating.

INVOICE:

Load

     INVOICE_ID

     INVOICE_TYPE

FROM ...;

TYPEMAP:

MAPPING LOAD DISTINCT

     INVOICE_ID as A

     INVOICE_TYPE as B,

Resident INVOICE;

XML_INVOICE

LOAD

     INVOICE_ID

     if(applymap('TYPEMAP', INVOICE_ID)='CREDIT' AND AMOUNT => 0, -1, 1) * AMOUNT as AMOUNT_CORR

FROM ...;

Any suggestions would be greatly appreciated

Thanks

Anders

1 Solution

Accepted Solutions
maxgro
MVP
MVP

if you use Alessandro Saccone epression the result is what you want, see below image and script using Alessandro expression

TYPEMAP:

mapping LOAD * INLINE [

INVOICE_ID, INVOICE_TYPE

1000, CREDIT

2000, INVOICE

3000, CREDIT

];

XML_INVOICE:

load

  *,

  if(applymap('TYPEMAP', INVOICE_ID)='CREDIT' AND AMOUNT >= 0, -1* AMOUNT, AMOUNT) as AMOUNT_CORR

  ;

LOAD * INLINE [

INVOICE_ID, LINE, AMOUNT, ITEM_ID

1000, 1, 10, 11

2000, 1, 10, 15

2000, 2, 15, 17

3000, 1, -20, 15

3000, 2, -10, 11

];

@.jpg

View solution in original post

11 Replies
Not applicable
Author

Hi Anders

to use applyMap you have to be sure that output field will deliver a unique value,

means INVOICE_ID can have only 1 INVOICE_TYPE at a time

is it the case? Because according to your example, invoice_id is related to many line and each line could potentially be invoiced, cancelled, credited, puased or whatever...

Best regards

Chris

alexandros17
Partner - Champion III
Partner - Champion III

Try this

if(applymap('TYPEMAP', INVOICE_ID)='CREDIT' AND AMOUNT >= 0, -1* AMOUNT, AMOUNT) as AMOUNT_CORR

Not applicable
Author

And (i forgot)  are you sure type are always uppercase?

don't hesitate to use upper or lower in mapping table and applymap

Chris

Not applicable
Author

Result expected:

A XML_INVOICE TABLE looking like this:

INVOICE _IDLINEAMOUNTITEM_IDAMOUNT_CORR
100011011-10
20001101510
20002151715
30001-2015-20
30002-1011-10
Not applicable
Author

Hi Chris

You're absolutely correct. INVOICE_ID will not deliver a unique value.

A new field consisting of INVOICE_ID + INVOICE_LINE_ID could that be an option?

INVOICE_ID is either CREDIT or INVOICE. Lines will not be different types.

Kind regards

Anders

Not applicable
Author

Hi Anders

I think you need "AND AMOUNT >= 0" instead of "AND AMOUNT => 0"

Lukasz

Not applicable
Author

I get this error

fejl.JPG.jpg

maxgro
MVP
MVP

if you use Alessandro Saccone epression the result is what you want, see below image and script using Alessandro expression

TYPEMAP:

mapping LOAD * INLINE [

INVOICE_ID, INVOICE_TYPE

1000, CREDIT

2000, INVOICE

3000, CREDIT

];

XML_INVOICE:

load

  *,

  if(applymap('TYPEMAP', INVOICE_ID)='CREDIT' AND AMOUNT >= 0, -1* AMOUNT, AMOUNT) as AMOUNT_CORR

  ;

LOAD * INLINE [

INVOICE_ID, LINE, AMOUNT, ITEM_ID

1000, 1, 10, 11

2000, 1, 10, 15

2000, 2, 15, 17

3000, 1, -20, 15

3000, 2, -10, 11

];

@.jpg

Not applicable
Author

Hi Massimo

Your answer worked out the problem for me.

At least on my little example, the exciting part is how it will handle 2 million invoices and 4 million lines.

This was my first question on the community and I am amazed.

Thank you very much. You were all very helpful.

Kind regards

Anders