web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / SortByColumns not work...
Power Apps
Answered

SortByColumns not working when using a variable

(0) ShareShare
ReportReport
Posted on by 51

Have an issue can't seem to solve. Have a data table MonthTable with 12 months of sales figure and want to display as trailing 12 months eg: if we are in April then sort starting with April (last year) thru March (this year). Table has 2 columns MonthNumber and Sales.

 

I have a lookup table to get the sort order based on current month. Works when hard coded, but does not using variable.

 

On Visible screen set the variable, to test I hard coded it for April ie: month 4

UpdateContext({SortString:"[4,5,6,7,8,9,10,11,12,1,2,3]"});
 
then on table Items use
SortByColumns(MonthTable, "MonthNumber", SortString)
does not work but
SortByColumns(MonthTable, "MonthNumber", [4,5,6,7,8,9,10,11,12,1,2,3])
does work!!🤔
 
Any ideas?
Thanks
 
 
Categories:
  • Verified answer
    mdevaney Profile Picture
    29,993 Moderator on at
    @rmc
    You can’t sort a string. Don’t put your table between quotes in the OnVisible property then everything will work just fine.

    UpdateContext({SortString:[4,5,6,7,8,9,10,11,12,1,2,3]});

    —-
    Please Accept as Solution if this post answered your question so others may find it more quickly. If you found this post helpful consider giving it a Thumbs Up.
  • WarrenBelz Profile Picture
    156,926 Most Valuable Professional on at

    Please disregard - @mdevaney has answered before I posted

     

     

  • rmc Profile Picture
    51 on at

     

    Thankyou for the quick reply. That solved that issue perfectly!

     

    However the goal is to have this sort automatically based on current month. So I put all the 12 sort strings (SortOrderMonths) in a table (SortText) and tried this

    UpdateContext({SortString: LookUp(SortText,MonthNumber=Month(Now()),SortOrderMonths)});
     
    which did not work.I also tried putting this directly in Items of the table
     
    SortByColumns(MonthTable, "MonthNumber", LookUp(SortText,MonthNumber=Month(Now()),SortOrderMonths))
    which also did not work.
     
    Stumped!
     
     

     

     
  • mdevaney Profile Picture
    29,993 Moderator on at

    @rmc 

    Suggest you post your additional question in a new thread and close this one.  I am not necessarily sure I have the answer here so its best to start a new thread.

     

    ---
    Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 220 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 194 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 150

Last 30 days Overall leaderboard