Skip to main content
Announcements
Introducing a new Enhanced File Management feature in Qlik Cloud! GET THE DETAILS!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to order record first and then add RowNo?

Hi Experts,

    I loaded Excel file into QV and added a RowNo column, but I found the RowNo is based upon the original order of the records in Excel. How can I sort the data in Excel first and then add RowNo column, so that RowNo column have the same order as sorted column?

Longmatch

1 Solution

Accepted Solutions
jeffmartins
Partner - Creator II
Partner - Creator II

Hi longmatch,

You need to use a temporary table before of sorting the table and create the rowno field.

See the attached file. Hope it helps you.

Regards

View solution in original post

4 Replies
MayilVahanan

Hi

I think, use

order by fieldname asc;

Hope that helps

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

Hi Longmatch

You can use the Order By command in the script to sort the loaded data and then use a left join to add a value of RowNo to each line.

An example of the code is:

//1: Load your Excel table

Source:

LOAD Ref,

     Dim1,

     Dim2,

     Dim3

FROM

[..\Desktop\Example Sort.xls]

(biff, embedded labels, table is Sheet1$);

//2: Reload your table using a resident load and sort the data in the order you wish.  In this case I've sorted by Ref in descending order and have qualified the table to distinguish it from the Source table.

Qualify *;

Source_Sorted:

Load *

Resident Source

order by Ref desc;

Unqualify *;

//3: Drop the original Source table to avoid unnecessary duplicate data in memory

Drop table Source;

//4: Join the value of RowNo() to a key field, in this case Ref.  A left join is fine as every row will have a RowNo.

     

Left join(Source_Sorted)

load

Source_Sorted.Ref,

Rowno() as Source_Sorted.ID

Resident Source_Sorted;

Data_Sorted.IDData_Sorted.RefData_Sorted.Dim1Data_Sorted.Dim2Data_Sorted.Dim3
110453050
29244576
38252060
47244050
561003050
65453050
74244576
83252060
92244050
1011003050

Here you can see that the Excel table is sorted in Descending order by Ref and the addition of the RowNo has not changed the order but has correctly indexed each row in ascending order.

Remember to remove any sort orders present in the sort tab of the properties of your table box/chart as they would override the script sort making it look like the script hadn't worked.

Hope that helps.

Steve

jeffmartins
Partner - Creator II
Partner - Creator II

Hi longmatch,

You need to use a temporary table before of sorting the table and create the rowno field.

See the attached file. Hope it helps you.

Regards

Not applicable
Author

Jeffmartins and stevenblowers, thank you for your quick reply. Both of our solutions are working, but jeffmartins's is simpler so I selected his answer as correct. Since only one correct answer can be selected, I had to select stevenblowers' as helpful solution. Thank you again!!!