Create a single dimension from multiple fields - qlikview

So essentially I want to create a single dimension by using their respective column/filename, say a Listbox called Asset, to make a selection on this laptop, desktops, server, and tablet.
Many thanks.
enter image description here

What you'd want is the CrossTable prefix (see the Qlik Help here). It allows you to "pivot" a table so that 1 record with multiple columns becomes multiple records with just 2 columns (1 column for field name, 1 column for field value).
So given your table, which we'll call [Data]:
Code
# of laptop
# of desktop
# of servers
# of tablet
d1
0
1
0
1
a2
23
3
0
0
a3
12
5
0
0
f1
0
14
0
0
e3
0
12
0
0
z2
0
5
1
0
...you can use the following Qlik script in the Data Load Editor to get the desired output:
[Pivoted]:
CrossTable ([Device], [DeviceCount], 1) Load * Resident [Data];
Drop Table [Data];
[New Data]:
NoConcatenate Load
[Code]
, Capitalize(SubField([Device], ' ', -1)) as [Device]
, [DeviceCount]
Resident [Pivoted];
Drop Table [Pivoted];
That should give you this result:
Code
Device
DeviceCount
a2
Desktop
3
a2
Laptop
23
a2
Servers
0
a2
Tablet
0
a3
Desktop
5
a3
Laptop
12
a3
Servers
0
a3
Tablet
0
d1
Desktop
1
d1
Laptop
0
d1
Servers
0
d1
Tablet
1
e3
Desktop
12
e3
Laptop
0
e3
Servers
0
e3
Tablet
0
f1
Desktop
14
f1
Laptop
0
f1
Servers
0
f1
Tablet
0
z2
Desktop
5
z2
Laptop
0
z2
Servers
1
z2
Tablet
0

Related

Find the third field if two fields have non zero values

My dataset -
A B C
abc 0 12
ert 0 45
ghj 14 0
kli 56 78
qas 0 0
I want to find the values of A for which values of B and C together are non-zero.
Expected output-
A B C
kli 56 78
I tried-
aggr(
sum({<[B]={"<>0"},[C]={"<>0"}>}A)
,[B],[C])
Depends where you are doing this, in the Data Load or through Set Analysis on the front, but really this will work on both the load editor and a table.
if("B" <> 0 and "C" <> 0, 'Non-Zero Value', 'Zero Value')
Example of what I created

Bring data frame rows in same order as second data frame

I got two data frames:
features:
id feat1 feat2 feat3
r0 a1 1 1 1
r1 b3 4 NA NA
r2 q7 8 1 1
labels:
id target
r0 b3 1
r1 q7 0
r2 a1 1
They contain both the same id's but in different order. How can I bring either of them into the order of the other one? Is there a way without sorting both of them into an alphabetic order? Like that I only need to alter one of them?
Thanks a lot for any comments!

Assigning Score based on Order Sequence in pandas

Following are the dataframes I have
score_df
col1_id col2_id score
1 2 10
5 6 20
records_df
date col_id
D1 6
D2 4
D3 1
D4 2
D5 5
D6 7
I would like to compute a score based on the following criteria:
When 2 occurs after 1 the score should be assigned 10 or when 1 occurs after 2, score should be assigned 10.
i.e when (1,2) gives a score 10 .. (2,1) also get the same score 10.
considering (1,2) . When 1 occurs first time we dont assign a score. We flag the row and wait for 2 to occur. When 2 occurs in the column we give the score 10.
considering (2,1). When 2 comes first. We assign value 0 and wait for 1 to occur. When 1 occurs, we give the score 10.
So, for the first time - dont assign the score and wait for the corresponding event to occur and then assign the score
So, my result dataframe should look something like this
result
date col_id score
D1 6 0 -- Eventhough 6 is there in score list, it occured for first time. So 0
D2 4 0 -- 4 is not even there in list
D3 1 0 -- 1 occurred for first time . So 0
D4 2 10 -- 1 occurred previously. 2 occurred now.. we can assign 10.
D5 5 20 -- 6 occurred previously. we can assign 20
D6 7 0 -- 7 is not in the list
I have around 100k rows in both score_df and record_df. Looping and assigning score is taking the time. Can someone help with logic without looping the entire dataframe?
From what i understand , you can try melt for unpivotting and then merge. keeping the index from the melted df , we check where the index is duplicated , and then return score from the merge else 0.
m = score_df.reset_index().melt(['index','uid','score'],
var_name='col_name',value_name='col_id')
final = records_df.merge(m.drop('col_name',1),on=['uid','col_id'],how='left')
c = final.duplicated(['index']) & final['index'].notna()
final = final.drop('index',1).assign(score=lambda x: x['score'].where(c,0))
print(final)
uid date col_id score
0 123 D1 6 0.0
1 123 D2 4 0.0
2 123 D3 1 0.0
3 123 D4 2 10.0
4 123 D5 5 20.0
5 123 D6 7 0.0

sum column based on values from another column, grouping by a third column

I am but lost in this one. Seems simple but just cant get to the answer. And i have a feeling i might be embarrassed by the answer. Ok here it goes. Am trying to do this in WebMatrix 3.0, i believe its SQL Express. Not sure about that.
Table Dlivry
tID cID Boxes Delivered
1 01 5 1
2 03 7 1
3 01 2 0
4 01 3 1
5 03 5 1
6 05 4 1
7 05 10 0
what i want is
Client Delivered NotDelivered
01 8 2
03 12 0
05 4 10
What i have done so far
SELECT D1.cid AS Client,
Sum(D1.boxes) AS Delivered,
Sum(D2.boxes) AS NotDelivered
FROM dlivr D1,
dlivry D2
WHERE D1.delivered = 1
AND D2.delivered = 0
AND D1.cid = D2.cid
GROUP BY cid
Ok there it is. Am ready to get embarrassed and i am ready to learn.
Appreciate your help.
Since you're using an inner join, you'll only get rows for CID that have both delivered and undelivered boxes. So you won't get any row for CID = 3. Try this:
SELECT CID,
SUM(CASE WHEN Delivered = 1 THEN boxes ELSE 0 END) AS Delivered,
SUM(CASE WHEN Delivered = 0 THEN boxes ELSE 0 END) AS NotDelivered
FROM Dlivr
GROUP BY CID

SPSS using value of one cell to call another cell

Below is some data:
Test Day1 Day2 Score
A 1 2 100
B 1 3 62
C 3 4 90
D 2 4 20
E 4 5 80
I am trying to take the values from column 'day' and 'day2' and use them to select the row number for the column score. For example for Test A I would like to find the sum of 100 and 62 because that is the values of the first and second rows of score. Test B I would like to find the sum of 100, 62 and 90.
Does anyone have any ideas on how to go about doing this? I am looking to use something similar to the indirect function in Excel? Thank You
The trick is to convert variable "Score" as a row. Could not think of an easy way how to avoid SAVE/GET - room for improvements.
file handle tmp
/name = "C:\DATA\Temp".
***.
data list free /Test (a1) Day1 (f8) Day2 (f8) Score (f8).
begin data
A 1 2 100
B 1 3 62
C 3 4 90
D 2 4 20
E 4 5 80
end data.
comp f = 1.
var wid all (12).
save out "tmp\data.sav".
***.
get "tmp\data.sav"
/keep score.
flip.
comp f = 1.
match files
/file "tmp\data.sav"
/table *
/by f
/drop case_lbl.
comp stat = 0.
do rep var = var001 to var005
/k = 1 to 5.
if range(k, Day1, Day2) stat = sum(stat, var).
end rep.
list Test Day1 Day2 Score stat.
The result:
Test Day1 Day2 Score stat
A 1 2 100 162
B 1 3 62 252
C 3 4 90 110
D 2 4 20 172
E 4 5 80 100
Number of cases read: 5 Number of cases listed: 5