Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Answered

Dynamic SortByColumns

(0) ShareShare
ReportReport
Posted on by 51

I have a table of sales by month number and want to display that as trailing 12 months eg: If we are in April (month number 4) the display sales staring month 4 (April) thru month 3 (March).

 

I put all the format strings in another table SortText by month and dynamically lookup that string using

 

LookUp(SortText,MonthNumber=Month(Now()),SortOrderMonths) which returns [ 4,5,6,7,8,9,10,11,12,1,2,3 ]

 

this statement does not work and no error given

SortByColumns(SalesTable, "MonthNumber", LookUp(SortText,MonthNumber=Month(Now()),SortOrderMonths))

 

If you hardcode it like this
SortByColumns(SalesTable, "MonthNumber",[ 4,5,6,7,8,9,10,11,12,1,2,3 ])

then it works perfectly!

If anyone has any ideas, I can't see why it is not working.

 

Thanks!

  • PowerAddict Profile Picture
    7,314 Most Valuable Professional on at
    Re: Dynamic SortByColumns

    Glad I could help! Good luck with the rest of the app! Feel free to reach out in case of any questions!

     

    Thanks!

    Hardit Bhatia

    Blog | Twitter | LinkedIn | Facebook | YouTube  |  Email

  • rmc Profile Picture
    51 on at
    Re: Dynamic SortByColumns

    Thanks Hardit!

     

    Worked perfectly. Would never of thought to use Switch statement here.

     

    Mark

  • Verified answer
    PowerAddict Profile Picture
    7,314 Most Valuable Professional on at
    Re: Dynamic SortByColumns

    The problem is when you are doing a LookUp using the table that has your sortorders, it returns a text and that's why it doesn't work. 

     

    You might want to try something like this:

    SortByColumns(SalesTable, "MonthNumber", Switch(Month,4, [ 4,5,6,7,8,9,10,11,12,1,2,3 ], 5, [5,6,7,8,9,10,11,12,1,2,3,4))

     

    Let me know if this helps. 

     

    ---
    If you like this reply, please give kudos (Thumbs Up). And if this solves your problem, please mark this reply as a solution by selecting Accept as Solution. This makes it easier for others to find answers to similar questions. 

     

    Thanks!

    Hardit Bhatia

    Blog | Twitter | LinkedIn | Facebook | YouTube  |  Email

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

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 770 Most Valuable Professional

#2
stampcoin Profile Picture

stampcoin 494

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 399