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

Sorting by Expression

Hi All

I have a list box showing Weekly dates(from-to), week number in the month and month of the week.

2015-01-26_07-59-13.png

I have written a sort expression to sort by week number "mid(Weeks,12,1) ". I would now like to have a second condition in the sort expression to will sort first by week number, then months in a cronological order. How can I do this?

I would also like my selected dates to show at the top of the list box as they currently at the bottom which I need to scroll down to see.

2015-01-26_08-12-20.png

1 Solution

Accepted Solutions
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

You could also generate a dummy table in the order you want and then use Load Order in the sort.

WeekSort:

LOAD

Num(Day(RangeMax(weekstart(RE_DAY),monthstart(RE_DAY))),'00')    

&'-'&  

Num(Day(RangeMin(weekend(RE_DAY),monthend(RE_DAY))),'00')  

&' Week '&

If(( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1) < 0,( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1)+52, ( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1)) &' Of '& Month(RE_DAY) as Weeks

;

LOAD MakeDate(2012)+RecNo() as RE_DAY AutoGenerate 2000;

// Do the rest of the script...

DROP TABLE WeekSort;  // Drop dummy table at script end

-Rob

View solution in original post

10 Replies
jagan
Luminary Alumni
Luminary Alumni

Hi,

If you have a date field and then use the Dual() for setting the Sorting.

LOAD

*,

DUal(Weeks, WeekEnd(Date)) AS Weeks

FROM DataSource;

Now in sort tab select Numeric.  Hope this helps you.

Regards,

Jagan.

Not applicable
Author

Hi Jagan

Thanks for your reply

Please my script for Weeks:

Num(Day(RangeMax(weekstart(RE_DAY),monthstart(RE_DAY))),'00')      
&'-'&    
Num(Day(RangeMin(weekend(RE_DAY),monthend(RE_DAY))),'00')   
&' Week '&
If(( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1) < 0,( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1)+52, ( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1)) &' Of '& Month(RE_DAY) as Weeks;

How would I apply the Dual fuction here?

alexandros17
Partner - Champion III
Partner - Champion III

When I use Week and Month fields I always use the following format:

201501 ... 201552 for weeks

and

201501 ... 201512 for months,

these formats ghive you natural (numeric) sorting

Starting from a date do as follows:

for Weeks: WeekYear(Date)*100 + Week(Date);

for Months: Year(Date)*100 + Month(Date);

Hope this helps

Not applicable
Author

Hi Jagan

I think you solution could help me but I'm just strucggling to apply it to my code as it is a bit complex. Can you please assist with that. Please see the script in my previous comment

jagan
Luminary Alumni
Luminary Alumni

Hi,

Try this

LOAD

*,

Dual(

Num(Day(RangeMax(weekstart(RE_DAY),monthstart(RE_DAY))),'00')      
&'-'&    
Num(Day(RangeMin(weekend(RE_DAY),monthend(RE_DAY))),'00')   
&' Week '&
If(( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1) < 0,( week(WeekStart(RE_DAY)) -week(MonthStart(RE_DAY))+1)+52, ( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1)) &' Of '& Month(RE_DAY), WeekStart(RE_DAY)) AS Week


Regards,

Jagan.




Not applicable
Author

Hi Jagan

Thank you very much for your reply...just one more question.

I'm pulling the data from a SQL table

Can I still perform a Load *, with a preceding load?

jagan
Luminary Alumni
Luminary Alumni

Hi,

Use preceding load like below

LOAD

*,

Dual(

Num(Day(RangeMax(weekstart(RE_DAY),monthstart(RE_DAY))),'00')      
&'-'&    
Num(Day(RangeMin(weekend(RE_DAY),monthend(RE_DAY))),'00')   
&' Week '&
If(( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1) < 0,( week(WeekStart(RE_DAY)) -week(MonthStart(RE_DAY))+1)+52, ( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1)) &' Of '& Month(RE_DAY), WeekStart(RE_DAY)) AS Week;

SELECT

*

FROM TableName;


Regards,

Jagan.


Not applicable
Author

Hi Jagan

Thanks for the solution. It works perfectly...except now the Dual function is messing up my dates.

I now have a date of 2012/01/01 showing in the list box as "26-31 Week 5 Of Dec". It is suppose to be "01-01 Week 1 Of Jan"

Before adding Dual

2015-01-27_18-42-52.png

After adding Dual

2015-01-27_18-39-50.png

How can I fix this?

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

You could also generate a dummy table in the order you want and then use Load Order in the sort.

WeekSort:

LOAD

Num(Day(RangeMax(weekstart(RE_DAY),monthstart(RE_DAY))),'00')    

&'-'&  

Num(Day(RangeMin(weekend(RE_DAY),monthend(RE_DAY))),'00')  

&' Week '&

If(( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1) < 0,( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1)+52, ( week(WeekStart(RE_DAY)) - week(MonthStart(RE_DAY))+1)) &' Of '& Month(RE_DAY) as Weeks

;

LOAD MakeDate(2012)+RecNo() as RE_DAY AutoGenerate 2000;

// Do the rest of the script...

DROP TABLE WeekSort;  // Drop dummy table at script end

-Rob