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

Formula problem

Hello fellow QV users,

I have the following case, see matrix below:

CONTRACTSUB-CONTRACTPRODUCTCODECORRECT CODE
A1mt-10mt-10
A2othermt-10
A3othermt-10
A4othermt-10
A5othermt-10


I have a contract named A and this contract has 5 subcontracts one of these has a productcode 'MT-10' the other sub-contracts are named 'OTHER'. How can I create the result as shown in column CORRECT CODE (every sub-contract should be named (MT-10). Be aware that there are 25 PRODUCTCODES, each contract has one PRODUCTCODE NAME and the other sub- contracts are also named other.

With kind regards,

Aissam

1 Solution

Accepted Solutions
d_pranskus
Partner - Creator III
Partner - Creator III

This is my code.

[Data]:

LOAD CONTRACT,

[SUB-CONTRACT],

PRODUCTCODE,

Recno() AS RecNo

FROM [data QV forum question.xls] (biff, embedded labels, table is Sheet1$)

;

[tmp]:

LOAD

CONTRACT,

PRODUCTCODE,

RecNo AS RecNo1

RESIDENT [Data]

WHERE PRODUCTCODE <> 'Other'

ORDER BY RecNo DESC;

LEFT JOIN (tmp)

LOAD

CONTRACT,

PRODUCTCODE,

RecNo1,

IF(CONTRACT = PEEK(CONTRACT), PEEK(RecNo1)-1, 99999999) AS RecNo2

RESIDENT [tmp];

LEFT JOIN ([Data])

INTERVALMATCH (RecNo, CONTRACT)

LOAD RecNo1, RecNo2, CONTRACT RESIDENT tmp;

LEFT JOIN ([Data])

LOAD

CONTRACT,

RecNo1,

RecNo2,

PRODUCTCODE AS CORRECTPRODUCT

RESIDENT tmp;

DROP TABLE tmp;

DROP FIELDS RecNo, RecNo1, RecNo2;

you should modify the code to meet your last requirement just by adding one load statment and the condition

LOAD

CONTRACT,

SUB-CONTRACT,

PRODUCTCODE,

IF(CONTRACT=SUBCONTRACT AND PRODUCTCODE='Other', SUBPRODUCTCODE, CORRECTCODE) AS CORRECTCODE

RESIDENT [Data];

I Hope it helps.



View solution in original post

11 Replies
d_pranskus
Partner - Creator III
Partner - Creator III

Hi Aissam

Please find the following code bellow. I hope it helps.



[Data]:
LOAD * INLINE [
CONTRACT, SUBCONTRACT, PRODUCTCODE
A, 1, mt-10
A, 2, other
A, 3, other
A, 4, other
A, 5, other
B, 1, mt-20
B, 2, other
B, 3, other
B, 4, other
B, 5, other
C, 1, mt-30
C, 2, other
C, 3, other
C, 4, other
C, 5, other
];

LEFT JOIN (Data)
LOAD DISTINCT
CONTRACT,
PRODUCTCODE AS CORRECTCODE
RESIDENT [Data]
WHERE SUBCONTRACT = 1;


spsrk_84
Creator III
Creator III

Hi,

Use the previous and peek functions to create the Correcr Column which is required.Create the column with a condition like if previous Contract Name is similar then copy the same Product Code column to Correct Code.

Regards,

Ajay

Not applicable
Author

Hello guys,

The inline function is not a possibility because we have at least 500.000 Contracts :-).

Ajay;

I don't understand the Peek function?

regards,

Aissam

d_pranskus
Partner - Creator III
Partner - Creator III

Hi

The inline function was used just to introduce the data, to have something to work with. Please see the second sentence. It finds a correct product code for every contract where subcontract number is 1. If it does not help you please attach some excerpt of your data exported to excel file and I try to figure it out.

Cheers

Darius

Not applicable
Author

Hi Darius,

The Contract and subcontract are both unique. Attached you will find an excel file with some data.

Regards,

Aissam

Not applicable
Author

hi,

you can use the peek function to create the following code

load

if(subcontract='other',peek(correctcode),subcontract) as correctcode;

from .....;

this should work fine

thanks

Not applicable
Author

Hello Tauqueer,

Thanks for your reply. Let's make it a step complicater. We also have contracts that have one subcontract (CONTRACT AND SUBCONTRACT are the same). These contracts have a PRODUCTCODE 'OTHER'. We also have a SUBPRODUCTCODE, in these case it would be 'SPAREPARTS'. I would like to create the following formula.

If the result of forumula (if(subcontract='other',peek(correctcode),subcontract) as correctcode;) = 'other' then look in field SUBPRODUCTCODE. Below I have created a matrix where you can see what I mean. The CORRECT CODE for CONTRACT 09W5000 = SPAREPARTS and not 'OTHER' See matrix below row 5.

CONTRACTSUB-CONTRACTPRODUCTCODESUBPRODUCTCODECORRECT CODE
C09W361009W3611MT-10MT-10MT-10
C09W361009W3612OTHERTANKMT-10
C09W361009W3613OTHERSPAREPARTSMT-10
C09W361009W3614OTHERHYDROLICMT-10
09W500009W5000OTHERSPAREPARTSSPAREPARTS


Hope this is clear for you :-),

Regards,

Aissam

d_pranskus
Partner - Creator III
Partner - Creator III

This is my code.

[Data]:

LOAD CONTRACT,

[SUB-CONTRACT],

PRODUCTCODE,

Recno() AS RecNo

FROM [data QV forum question.xls] (biff, embedded labels, table is Sheet1$)

;

[tmp]:

LOAD

CONTRACT,

PRODUCTCODE,

RecNo AS RecNo1

RESIDENT [Data]

WHERE PRODUCTCODE <> 'Other'

ORDER BY RecNo DESC;

LEFT JOIN (tmp)

LOAD

CONTRACT,

PRODUCTCODE,

RecNo1,

IF(CONTRACT = PEEK(CONTRACT), PEEK(RecNo1)-1, 99999999) AS RecNo2

RESIDENT [tmp];

LEFT JOIN ([Data])

INTERVALMATCH (RecNo, CONTRACT)

LOAD RecNo1, RecNo2, CONTRACT RESIDENT tmp;

LEFT JOIN ([Data])

LOAD

CONTRACT,

RecNo1,

RecNo2,

PRODUCTCODE AS CORRECTPRODUCT

RESIDENT tmp;

DROP TABLE tmp;

DROP FIELDS RecNo, RecNo1, RecNo2;

you should modify the code to meet your last requirement just by adding one load statment and the condition

LOAD

CONTRACT,

SUB-CONTRACT,

PRODUCTCODE,

IF(CONTRACT=SUBCONTRACT AND PRODUCTCODE='Other', SUBPRODUCTCODE, CORRECTCODE) AS CORRECTCODE

RESIDENT [Data];

I Hope it helps.



Not applicable
Author

hi,

you can then modify your code to work like this....

load

if(productcode='other' and subcontract<>contract,peek(correctcode),if(subcontract=contract,subproductcode,productcode)) as correctcode;

from .....;

this should work fine

thanks