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

Announcements
Want to see what's currently in public preview? We've pulled it all together in one place. Check it out here
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

First and Second non-nulls

I currently have 5 free-text fields in my source data and I want to reduce these down to two fields. The idea being Name1 contains the first non-null value, and Name2 contains the second non-null.

Using nested if statements, I can get Name1 populated, but I am becoming stumped over Name2.

RowIdField1Field2Field3Field4Field5
1Text1Text2nullnullText5
2nullText2Text3nullnull
3Text1nullnullnullText5

So in the example above, I would expect the following results:

RowIdName1Name2
1Text1Text2
2Text2Text3
3Text1Text5

Has anyone got any ideas how to code this in the script?

Thanks

Labels (1)
1 Solution

Accepted Solutions
flipside
Partner - Specialist II
Partner - Specialist II

Hi Shoebox,

Try this ...

Data:

Load RowId, Subfield(NameList,';',1) as Name1, Subfield(NameList,';',2) as Name2;

LOAD RowId,

     If(IsNull(Field1),'',Field1 & ';') &

     If(IsNull(Field2),'',Field2 & ';') &

     If(IsNull(Field3),'',Field3 & ';') &

     If(IsNull(Field4),'',Field4 & ';') &

     If(IsNull(Field5),'',Field5 & ';')  as NameList

FROM

[Data.xls]

(biff, embedded labels, table is Sheet1$);

flipside

View solution in original post

6 Replies
Not applicable
Author

Can u explain how you build the second table?

Anonymous
Not applicable
Author

The second table would be a left join of fields Name1 and Name2 back to the first table, with Fields1-5 then being dropped.

Not applicable
Author

Check if this is what you want:

http://community.qlik.com/thread/9351

TMF

Anonymous
Not applicable
Author

Thanks, it didn't quite do what I was after, but it did sow a seed of an idea which has worked for me. Although I'm not sure how efficient it is.

Load the data:

KeyField1Field2Field3Field4Field5
Claim1Text1Text2nullnullText5
Claim2nullText2Text3nullnull
Claim3Text1nullnullnullText5

Create a temporary table with all non-null values in a single field and use the RowNo() function:

KeyTempNameRowID
Claim1Text11
Claim1Text22
Claim1Text53
Claim2Text24
Claim2Text35
Claim3Text16
Claim3Text57

Create a minimum RowID per Key as Name1ID:

KeyTempNameRowIDName1ID
Claim1Text111
Claim1Text221
Claim1Text531
Claim2Text244
Claim2Text354
Claim3Text166
Claim3Text576

Create a minimum RowID per Key, where RowID<>Name1ID as Name2ID:

KeyTempNameRowIDName1IDName2ID
Claim1Text1112
Claim1Text2212
Claim1Text5312
Claim2Text2445
Claim2Text3545
Claim3Text1667
Claim3Text5767

Left join temporary table back to Data where RowID=Name1ID and again for RowID-Name2ID

RowIdField1Field2Field3Field4Field5Name1Name2
1Text1Text2nullnullText5Text1Text2
2nullText2Text3nullnullText2Text3
3Text1nullnullnullText5Text1Text5

Drop the temporary table and Fields1-5:

KeyName1Name2
Claim1Text1Text2
Claim2Text2Text3
Claim3Text1Text5

Phew! As I say, it works, but it's a little messy.

flipside
Partner - Specialist II
Partner - Specialist II

Hi Shoebox,

Try this ...

Data:

Load RowId, Subfield(NameList,';',1) as Name1, Subfield(NameList,';',2) as Name2;

LOAD RowId,

     If(IsNull(Field1),'',Field1 & ';') &

     If(IsNull(Field2),'',Field2 & ';') &

     If(IsNull(Field3),'',Field3 & ';') &

     If(IsNull(Field4),'',Field4 & ';') &

     If(IsNull(Field5),'',Field5 & ';')  as NameList

FROM

[Data.xls]

(biff, embedded labels, table is Sheet1$);

flipside

Anonymous
Not applicable
Author

Excellent. Thank you. That's much tidier.