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

How to get an X number from the user and load a rendom X rows from data?

Hi Everyone ,

Lets say I have a table with 3 columns : Id, name , country - with 100 different rows.

I need to get from the user (using an Input box object) some number  , lets say he will put the value: 20

I want that after the user will insert 20 , the table on the GUI will show only 20 random rows from the table .

I don't want him to see the first 20 rows, I want him to see 20 random rows from the 100 rows.

I know how to use the Input box object to take the number from the user, but I don't know how to show the random rows.

can someone help me ?

thanks,

Moni

10 Replies
Agis-Kalogiannis
Employee
Employee

It would be much easier to show the first, largest or smallest N rows by using dimension limits.

Do you actually need to show 20 random rows?

This needs some, not so simple scripting.

ALK

Not applicable
Author

Hi Agis,

yes I need to show 20 random rows from the table.

I thought of adding the table :Recno()

and then use the rand() function and somehow find 20 rows

I just don't know how to do it ...

thanks

Moni

datanibbler
Champion
Champion

Hi,

you'd need to use the rand() function (which will return a random number inbetween 0 and 1) in a loop from 1 to 20 (to stick with the example) and use those to create a string (with semicolons so you can use it in a MATCH() function) with 20 random numbers - if you have 100 rows, you'll have to multiply the output of rand() with 100 so you get 20 random line_numbers.

In the script, you load the file and in the lOAD you just add a >> rowNo() as line_ID <<

=> Then you have to use some set_analysis to display the 20 rows with those exact line_IDs.

Does your user have the client installed and a Named_CAL so they can reload the script from the GUI? That would make it a bit easier.

HTH

Not applicable
Author

Hi DataNibbler,

yes my user have a client on his computer.

I will try to do what you said and see if I understand it.

thanks,

Moni

datanibbler
Champion
Champion

Hi,

the Client alone will not do for what I mean - reloading the script from the GUI is possible only with a Named_CAL, not with a Document_CAL.

Try to build that loop (should be possible with autogenerate) first and come back, we can see from there.

Best regards,

DataNibbler

swuehl
MVP
MVP

Maybe like this?

Not applicable
Author

wow thanks Swuehl,

seems like it is a good example

swuehl
MVP
MVP

Using RAND() function will not check for collisions in value generation, so it can happen that you'll get duplicate IDs selected (more likely with a lower number of table rows and higher number of X), i.e. the resulting table may be some lines smaller than expected. 

Not applicable
Author

hI Swuehl,

you are right I can see now that I get less lines then I expected, how can I handle that ?

I got to a point that to every row have its own ID (with the recno() as LineNr  ),

how can I make sure that there will not be collisions in value generation?

thanks a lot

Moni