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

Create New Field on Script based on calculation

Say I have a table like below and in the attachment, but with millions of household IDs. I'd like to create a new field, called 'n1 Viewership - High/Medium/Low', based on the calculation of time spent of Network n1. If the average time spent is on the top 1/3, return to the value of 'High' of the new field, middle 1/3 'Medium' and bottom 1/3 'Low'. If no viewership of the n1 network, then 'NA'. How do I do it? Thanks in advance!

HHNetworkTime spent
hh1n120
hh2n110
hh3n230
hh4n115
hh5n233
hh6n214
hh7n122
hh8n250
hh9n123
hh10n144
hh11n25
hh12n212
hh13n155
hh14n142
hh15n133
hh16n112
hh17n230
1 Reply
Not applicable
Author

Please try like below:

TEMP:

LOAD HH, NetWork, TimeSpent FROM SOURCE;

// Calculate Avg Value: Sum ([Time spent])/Count (DISTINCT HH)

T_AVG:

LOAD COUNT(DISTINCT HH) AS HH_CNT, Sum([Time Spent]) AS SUM_TIME_SPENT

Resident TEMP Where HH='n1';

Let vAvg = num(peek('SUM_TIME_SPENT')/peek('HH_CNT'),'###0.00') ;

Drop Table T_AVG;

FINAL:

LOAD *,

IF(Network='n1' , IF([Time Spent] > ($(vAvg)*2)/3 , 'High' , IF([Time Spent] < $(vAvg)/3 ,'Low', 'Medium' ) ) , 'NA') AS N1_VIEWERSHIP

Resident TEMP;

DROP Table TEMP;