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

How to conditionally fetch column name as row value ?

Hi All ,

How to conditionally fetch column name as row value at script level , without using multiple nested if  ?

Source data 

IDLocationM1M2M3M4M5
P1R10.40.7   
P2R10.1   0.8
P3R1   0.60.3
P4R1   0.60.8
P1R2  0.10.3 
P2R2  0.70.0 
P3R20.5   0.4
P4R20.3   0.3
P1R3 0.10.0  
P2R3 0.2 0.3 
P3R3 0.7  0.3
P4R30.9  0.5 

 

OUTPUT 

IDLocationM1M2M3M4M5COLUMN1
P1R10.40.7   M1,M2
P2R10.1   0.8M1,M5
P3R1   0.60.3M4,M5
P4R1   0.60.8M4,M5
P1R2  0.10.3 M3,M4
P2R2  0.70.0 M3,M4
P3R20.5   0.4M1,M5
P4R20.3   0.3M1,M5
P1R3 0.10.0  M2,M3
P2R3 0.2 0.3 M2,M4
P3R3 0.7  0.3M2,M5
P4R30.9  0.5 M1,M4

 

Thank You .

 

Labels (1)
1 Solution

Accepted Solutions
eddie_wagt
Partner - Creator III
Partner - Creator III

Hello @shekhar_analyti,

If you want to do this dynamically, you can opt for some flexibility in the script.

 

Source:
LOAD * INLINE[
ID	Location	M1	M2	M3	M4	M5
P1	R1	0.4	0.7	 	 	 
P2	R1	0.1	 	 	 	0.8
P3	R1	 	 	 	0.6	0.3
P4	R1	 	 	 	0.6	0.8
P1	R2	 	 	0.1	0.3	 
P2	R2	 	 	0.7	0.0	 
P3	R2	0.5	 	 	 	0.4
P4	R2	0.3	 	 	 	0.3
P1	R3	 	0.1	0.0	 	 
P2	R3	 	0.2	 	0.3	 
P3	R3	 	0.7	 	 	0.3
P4	R3	0.9	 	 	0.5	 
]  (delimiter is '	');

LET NoFields = NoOfFields('Source');

For i=3 to $(NoFields)

LET FieldName = FieldName($(i),'Source');

TMP:
LOAD 	ID
,		Location
,		'$(FieldName)' as TMP_COLUMN1
Resident Source
Where $(FieldName)>0;

next;

left join (Source)
LOAD 	ID
,		Location
,		concat(TMP_COLUMN1,',') as COLUMN1
Resident TMP
Group By 	ID
,			Location
;

Drop Table TMP
;

View solution in original post

4 Replies
QFabian
Specialist III
Specialist III

Hi @shekhar_analyti , here an example, it has if, but they are not nested.

 

 

Load
ID, Location,

if(not isnull(M1) or M1 <>'', 'M1,') &
if(not isnull(M2), 'M2,') &
if(not isnull(M3), 'M3,') &
if(not isnull(M4), 'M4,') &
if(not isnull(M5), 'M5,') as M
Resident Data;

QFabian
eddie_wagt
Partner - Creator III
Partner - Creator III

Hello @shekhar_analyti,

If you want to do this dynamically, you can opt for some flexibility in the script.

 

Source:
LOAD * INLINE[
ID	Location	M1	M2	M3	M4	M5
P1	R1	0.4	0.7	 	 	 
P2	R1	0.1	 	 	 	0.8
P3	R1	 	 	 	0.6	0.3
P4	R1	 	 	 	0.6	0.8
P1	R2	 	 	0.1	0.3	 
P2	R2	 	 	0.7	0.0	 
P3	R2	0.5	 	 	 	0.4
P4	R2	0.3	 	 	 	0.3
P1	R3	 	0.1	0.0	 	 
P2	R3	 	0.2	 	0.3	 
P3	R3	 	0.7	 	 	0.3
P4	R3	0.9	 	 	0.5	 
]  (delimiter is '	');

LET NoFields = NoOfFields('Source');

For i=3 to $(NoFields)

LET FieldName = FieldName($(i),'Source');

TMP:
LOAD 	ID
,		Location
,		'$(FieldName)' as TMP_COLUMN1
Resident Source
Where $(FieldName)>0;

next;

left join (Source)
LOAD 	ID
,		Location
,		concat(TMP_COLUMN1,',') as COLUMN1
Resident TMP
Group By 	ID
,			Location
;

Drop Table TMP
;
shekhar_analyti
Specialist
Specialist
Author

Thank You eddie , hope this works fine with huge data . 

shekhar_analyti
Specialist
Specialist
Author

Thank You QFabian for the reply . 

But i was looking to avoid multiple if and hardcoding column names .