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

Announcements
AWS Degraded - You may experience Community slowness, timeouts, or trouble accessing: LATEST HERE
cancel
Showing results for 
Search instead for 
Did you mean: 
tduarte
Partner - Creator II
Partner - Creator II

Problem with sort order

Hi,

I'm trying to sort a table where regardless of the total, when Category is 'Other' then it should be at bottom of the table. The other values should be sorted by total.

Sort-> Sum(Units):

CategoryUnitsSort
Notebooks400400
Tablets300300
Other200200
Desktops100100

Sort-> Sum(Units)*if(Category='Other',-1,1) :

CategoryUnitsSort
Notebooks400400
Tablets300300
Desktops100100
Other200-200

So far so good. Now, the problem is that is some cases the total values are negative which put Other at the top (-200*-1=200) and not bottom.

CategoryUnitsSort
Other-200200
Desktops-100-100
Tablets-300-300
Notebooks-400-400

What would be a possible sort expression that suits both scenarios?

1 Solution

Accepted Solutions
swuehl
MVP
MVP

Maybe use a static sort number for 'Others':

if(Category = 'Other', -99999999, Sum(Units) )

View solution in original post

11 Replies
swuehl
MVP
MVP

Maybe use a static sort number for 'Others':

if(Category = 'Other', -99999999, Sum(Units) )

Not applicable

Sum(Units)*if(Category='Other',-1*sign(Sum(Units)),1)

maybe?

mphekin12
Specialist
Specialist

You can try using the DUAL keyword in your load to assign both a string and a numeric value to your Category.  Then you can set the sort order of Category to the Numeric Value.  I hope this helps!

Not applicable

Hi,

it depends on the cardinality of your data. This algorithm could work is fabs(sum(Units)) >=1:

Sum(Units) * if (Category='Other', (1 / Sum(Units))* -1, 1)

Regards

Sacho

tduarte
Partner - Creator II
Partner - Creator II
Author

Oscar Pizarro wrote:

Sum(Units)*if(Category='Other',-1*sign(Sum(Units)),1)

maybe?

Thanks but it doesn't work for the all negative situation.

tduarte
Partner - Creator II
Partner - Creator II
Author

swuehl wrote:

Maybe use a static sort number for 'Others':

if(Category = 'Other', -99999999, Sum(Units) )

Thanks swehl.

It does the job and it's optimised.

tduarte
Partner - Creator II
Partner - Creator II
Author

mphekin12 wrote:

You can try using the DUAL keyword in your load to assign both a string and a numeric value to your Category.  Then you can set the sort order of Category to the Numeric Value.  I hope this helps!

Not sure if it would aplicable on my case because the values of Category are not static and will change during reloads.

Am I correct?

mphekin12
Specialist
Specialist

You are correct if your Categories are not static. What about if you used a sort flag like this:

if(Category='Other', -1, 0) as SortLastFlag

You can then use this field as a hidden dimension in your table/chart and use this as your primary sort dimension.

tduarte
Partner - Creator II
Partner - Creator II
Author

This what I had before posting this discussion:

Script:

if(Category='Other', -1, 1) as SortCategory;

Sort order expression:

Sum(Units)*SortCategory

But like I explained, there's the issue with the negative totals.

All I want is whenever the value 'Other' is found, it must be last in sort order regardless of its total.