Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
cheryl
Contributor III
Contributor III

Check if a value already exists in another field in the same table

Hi,

I want to check if the value in the field ProjectID (in the current record) already exists in another field p.ParentID in the same table. I load all the data from a qvd file.

Project:
LOAD Distinct
p.billable,
p.ParentID,
p.ProjectID as ProjectID,
if(p.billable='True' and exists(p.ParentID,p.ProjectID),'TRUE','FALSE') as Is_parent,
FROM [lib://QVD-xxx.qvd]
(qvd);

 

However I got results like this (graph as an example):

Some of them has the correct results (Is_parent is TRUE when ProjectID is existing in ParentID, see yellow highlighted).

Some of them gave wrong results (Even when ProjectID is existing in ParentID, Is_parent is FALSE, see green highlighted)

cheryl_1-1645525127568.png

This is very weird to me. Does anybody know why this happened? 

Or is there any other solution to my problem?

Thanks very much!

Cheryl

 

 

Labels (4)
1 Solution

Accepted Solutions
tresesco
MVP
MVP

Load the parent field before this load separately, something like:

AllParents:
LOAD  p.ParentID as ParentAll FROM [lib://QVD-xxx.qvd] (qvd);

Project:
LOAD Distinct
p.billable,
p.ParentID,
p.ProjectID as ProjectID,
if(p.billable='True' and exists(ParentAll,p.ProjectID),'TRUE','FALSE') as Is_parent,
FROM [lib://QVD-xxx.qvd]
(qvd);

View solution in original post

6 Replies
tresesco
MVP
MVP

Hello Cheryl,

Are you considering the 'load order' while showing this data in table and trying to  comprehend? 

cheryl
Contributor III
Contributor III
Author

Hi Tresesco,

I am not quite clear what you mean here. I used the above-mentioned script and I would like to search for all IDs in ParentID field regardless of load order. When showing the data in the table above, I just use another field to put them together.

Thanks,

Cheryl

tresesco
MVP
MVP

Let's take up the first row from your table. As per your analysis - it is 'wrong' because the value 'P-61201' is present in the parent field. However, this is very much possible that this particular record is being loaded before any of the other records that has this value in p.ParentID field. That is means exists() would check if a value has been loaded so far or not. Therefore, if a value in the p.ProjectID comes Nth record, and it appears in (N+x)th row in p.ParentID you get false for that record; simply because for the Nth record that value is still not there in target column.

Not sure, if I could make this a bit clearer now or not.

cheryl
Contributor III
Contributor III
Author

Hi Tresesco,

Thanks for your explanation! I understand now that it is caused by the load order.

However do you have any idea how to achieve my goal: search whether ProjectID exists in p.ParentID regardless of the load order?

Thanks!

Cheryl

tresesco
MVP
MVP

Load the parent field before this load separately, something like:

AllParents:
LOAD  p.ParentID as ParentAll FROM [lib://QVD-xxx.qvd] (qvd);

Project:
LOAD Distinct
p.billable,
p.ParentID,
p.ProjectID as ProjectID,
if(p.billable='True' and exists(ParentAll,p.ProjectID),'TRUE','FALSE') as Is_parent,
FROM [lib://QVD-xxx.qvd]
(qvd);

cheryl
Contributor III
Contributor III
Author

This solved my problem! Thanks very much!