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

Announcements
Join us in NYC Sept 4th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
jacek_k
Contributor III
Contributor III

Calucate sum with specific condition

Sample data:

LOAD * INLINE [
date, index, value
1.01.2022, a, 100
1.01.2022, b, 50
2.02.2022, b, 100
];

 

I want to calculate sum(value) but
if index = a and index = b per exist same day sum only index = a (a have more priority, ignore b)
But if b only exist per same day sum(value) if index = b if only index a exist sum(value)

I need script flag to use it in set analysis. 


Labels (1)
11 Replies
sidhiq91
Specialist II
Specialist II

@jacek_k  could you please elaborate a little bit more clearly, i am sorry i am not able to understand.

jacek_k
Contributor III
Contributor III
Author

I want create flag 1 or 0. Example below:

Date, ID, Flag
1.01.2020, A, 0

1.01.2020 B, 1

If the same date and ID = A and B put 1 in B row, put 0 in A row. 


MayilVahanan

Hi

Are you looking like this?

LOAD *, If(date = Peek(date), 0, 1) as flag INLINE [
date, index, value
1.01.2022, a, 100
1.01.2022, b, 50
2.02.2022, b, 100
];


MayilVahanan_0-1659429794550.png

 

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.
jacek_k
Contributor III
Contributor III
Author

test:
LOAD * INLINE [
date, index, value
1.01.2022, a, 100
1.01.2022, b, 50
2.01.2022, b, 100
2.01.2022,a,20
1.01.2022,b,30
3.01.2022,a,100
4.01.2022,b,75

];

Load *,
if(date = peek(date),0,1) as flag
Resident test;
drop table test;

jacek_k_1-1659530159962.png

 

So, I add more dates and the results are incorrect. 
Please look on 1.01.2022 only index a = 1, b = must be 0
Also 2.01.2022 only a = 1, b must be 0.

So if a and b exist per same day put 1 to a, 0 to b.
if exist only one date b put 1
if exist only one date a put 1

 

MayilVahanan

HI

If the date is not in order, then try like below


test:
LOAD * INLINE [
date, index, value
1.01.2022, a, 100
1.01.2022, b, 50
2.01.2022, b, 100
2.01.2022,a,20
1.01.2022,b,30
3.01.2022,a,100
4.01.2022,b,75
];

Final:

LOAD *, If(date = Peek(date), 0, 1) as flag Resident test order by date;

DROP Table test;

MayilVahanan_0-1659570107497.png

 

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.
jacek_k
Contributor III
Contributor III
Author

Hi, please look on 2.01.2022 because index a has more priority flag must by 1.

1.01.2022 a and b =put 1 on a - correct
3.01.2022 a only - 1 flag correct
4.01.2022 b only - 1 flag correct

2.01.2022 a and b exist - put flag only a

MayilVahanan

Hi

if the order is different, you can add order by with both fields

 

LOAD *, If(date = Peek(date), 0, 1) as flag Resident test order by date, index;

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.
jacek_k
Contributor III
Contributor III
Author

I add more records:

test:
LOAD * INLINE [
date, index, value
1.01.2022, a, 100
1.01.2022, b, 50
2.01.2022, b, 100
1.01.2022, a, 30
2.01.2022,a,20
1.01.2022,b,30
3.01.2022,a,100
4.01.2022,b,75
];

Final:

LOAD *, If(date = Peek(date), 0, 1) as flag Resident test order by date,index;

DROP Table test;

jacek_k_0-1659684079971.png

As you can see above all 1.01.2022 index = a flag must be 1.

 

 

MayilVahanan

HI

Try like below

test:
LOAD * INLINE [
date, index, value
1.01.2022, a, 100
1.01.2022, b, 50
2.01.2022, b, 100
1.01.2022, a, 30
2.01.2022,a,20
1.01.2022,b,30
3.01.2022,a,100
4.01.2022,b,75
];

join
LOAD date, FirstSortedValue(distinct index, -value) as index, 1 as flag Resident test Group by date;

 

MayilVahanan_0-1659871902785.png

 

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.