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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Sort gallery by closes...
Power Apps
Unanswered

Sort gallery by closest location after search (is it possible?)

(0) ShareShare
ReportReport
Posted on by 30

Hello, 

 

My app lists retail stores under our brand, currently my gallery filter uses Search and SortbyColumns. I would like to sort the gallery list by stores that are closest to the user (the device location). I was able to create a label that displays the KM distance in the gallery list item (Code #1). My trouble is getting the gallery filter to sort by that distance. I've tried nesting the "AddColumns" function to try and append the distance to the main table (Table1), the table comes from a static Excel data source which I think might be the problem. 

 

Any help will be greatly appreciated, even a completely different approach to acheive this. 

 

Thanks, Nick

 

 

Code #1 - working km distance label inside gallery list items

ACOS(Cos(Radians(90-DeviceLat))*Cos(Radians(90-ThisItem.Latitude))+Sin(Radians(90-DeviceLat))*Sin(Radians(90-ThisItem.Latitude))*Cos(Radians(DeviceLong-ThisItem.Longitude)))*6371

Code #2 - current working gallery filter

SortByColumns(Search(Table1,TextInput1.Text,"Address","City","Mall Name","Location / Dealer Name (Primary)","DBM/AM"),"Location / Dealer Name (Primary)")

Code #3 - not-working gallery filter where I try to sort gallery list items by distance instead of dealer/location name

SortByColumns(AddColumns('Table1',"Distance",Acos(Cos(Radians(90-DeviceLat))*Cos(Radians(90-Latitude))+Sin(Radians(90-DeviceLat))*Sin(Radians(90-Latitude))*Cos(Radians(DeviceLong-Longitude)))*6371),Search(Table1,TextInput1.Text,"Address","City","Mall Name","Location / Dealer Name (Primary)","DBM/AM"),"Distance",Ascending)
Categories:
I have the same question (0)
  • NickK Profile Picture
    30 on at

    just follow up (bump). 

     

    Has anyone else tried doing something like this or have a solution?

     

    Thanks so much, Nick

  • Verified answer
    v-xida-msft Profile Picture
    on at

    Hi @NickK,

    Based on the Code # 3 formula that you provided, I think there is something wrong with it.

    Please take a try to modify your formula within the Items property of the Gallery control as below:

     

    SortByColumns(
     Search(
     AddColumns(
    Table1,
    "Distance",
    ACOS(Cos(Radians(90-DeviceLat))*Cos(Radians(90-ThisItem.Latitude))+Sin(Radians(90-DeviceLat))*Sin(Radians(90-ThisItem.Latitude))*Cos(Radians(DeviceLong-ThisItem.Longitude)))*6371
    ), TextInput1.Text, "Address","City","Mall Name","Location / Dealer Name (Primary)","DBM/AM" ), "Distance",
    Ascending )

    Or

    Set the OnVisible property of the first screen of your app to following:

     

    ClearCollect(
     RecordsCollection,
     AddColumns(
     Table1, 
     "Distance",
     ACOS(Cos(Radians(90-DeviceLat))*Cos(Radians(90-ThisItem.Latitude))+Sin(Radians(90-DeviceLat))*Sin(Radians(90-ThisItem.Latitude))*Cos(Radians(DeviceLong-ThisItem.Longitude)))*6371
     )
    )

    Set the Items property of the Gallery control to following:

     

    SortByColumns(
     Search(RecordsCollection, TextInput1.Text,"Address","City","Mall Name","Location / Dealer Name (Primary)","DBM/AM,
     "Distance",
     Ascending
    )

    check if the issue is solved.

    If the issue still exists, please consider take a try to save your Excel file (contains the Excel table) into your OneDrive folder, then re-create a connection to the Excel table from your app, try above formula again to check if the issue is solved.

     

    Best regards,

    Kris

     

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard