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

Tagging max number by Id

Hi Everyone,

I want to tag the ID with the max of number visit, but QV give me the error 'Invalid expression' .
So, what i have tried is this:


SCRIPT1

Tab:

LOAD Id,

     number_visit,

     max(number_visit) as Max_number_visit,

     if(number_visit < max(number_visit), 0, 'X') as Max_number_Tag

from Tab1

SCRIPT2

Tab:

LOAD Id,

     number_visit,

     max(number_visit) as Max_number_visit,

     if(max(number_visit),'X', 0) as No_visit_Tag

from Tab1

Both the above  scripts i have tried are not working for me!

My Result should lool like this:

ID
number_visit
No_visit_tag
A10
A20
A3X
B10
B20
B30
B4X
C10
C2X

PLEASE HELP!

7 Replies
whiteline
Master II
Master II

Hi.

It's a bit strange that you're trying to use Max() aggregation function without 'group by' statement.

Another thing that you use 'from' statement instead of 'resident'.

What is your goal MaxNumber or Tag ?

left join(Tab1)

LOAD

     Id,

     max(number_visit) as Max_number_visit

Resident Tab1

Group by  Id;

or

left join(Tab1)

LOAD

     Id,

     max(number_visit) as number_visit,

     'X' as No_visit_tag

Resident Tab1

Group by  Id;

Not applicable
Author

thanks for the reply whiteline  but i have used group by at the end also. I want to tag the visit number where it is maximum. So i know this is the last visit

whiteline
Master II
Master II

Have you tried second load provided above ?

Not applicable
Author

the second load will only provide me with the field 'No_visit_tag' with all the  'X' in that

whiteline
Master II
Master II

The second load will create a table:

A 3 X

B 4 X

C 2 X

and join it to your original table using two keys (id and number_visit), so that you'll have:

A     1 -

A     2 -

A     3     X

B     1 -

B     2 -

B     3 -

B     4     X

C     1 -

C     2     X

Not applicable
Author

Hi whiteline,

our goal is to tag all the ID's with the max Visit number.

and  by creating a list box and select  X from the list box to have  just all Id's where number_visit is maximum.

Can you give me detail script example what you mean because I have tried your idea above and still not working.

I tried:

Tab1:

LOAD

     Id,

     number_visit,

from Tab1;

left join

LOAD

Id,

if(max(number_visit), 'X') as Max_number_Tag

Resident Tab1

group by Id;

Our table now is look like this:

IDnumber_visitMax_number_Tag
A1X
A2X
A3X
B1X
B2X
C1X
C2X
C3X

But the correct table should look like the solution table from above.

whiteline
Master II
Master II

You're joining using Id as a key.

My idea is completely different, join using Id and number_visit as keys:

left join(Tab1)

LOAD

     Id,

     max(number_visit) as number_visit,

     'X' as No_visit_tag

Resident Tab1

Group by  Id;