cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Regular Visitor

Combining sort, filter and switch

I would like to show three columns for a database, SiteName, ProjectNumber and City. I’m also wanting to apply a filter on each column and make it possible to sort on each column. I was hoping that an expression like this would work:

 

Sort(
	Filter(
		'[dbo].[Site]'; 
		TextSiteNameFilter.Text in SiteName;         
		TextProjectNumberFilter.Text in ProjectNumber;
		TextCityFilter.Text in City
	);  
	Switch
	(
	    _sortOrder;
	    "SiteName"; 
	        SiteName;
            "ProjectNumber";
	        ProjectNumber;
                City
	);
	If(_sortAscending; Ascending; Descending)   
)

 


But if use that, I get a warning that delegation will not work properly, and I’m also see that I don’t see all data that I’m expecting.

 

Instead, I need to use this expression:

 

 

Switch
(
    _sortOrder;
    "SiteName"; 
    Sort(
        Filter(
          '[dbo].[Site]'; 
          TextSiteNameFilter.Text in SiteName;         
          TextProjectNumberFilter.Text in ProjectNumber;
          TextCityFilter.Text in City
        );  
        SiteName;
        If(_sortAscending; Ascending; Descending)   
    );
    "ProjectNumber";
    Sort(
        Filter(
          '[dbo].[Site]'; 
          TextSiteNameFilter.Text in SiteName;         
          TextProjectNumberFilter.Text in ProjectNumber;
          TextCityFilter.Text in City
        );  
        ProjectNumber;
        If(_sortAscending; Ascending; Descending)   
    );
    
    Sort(
        Filter(
          '[dbo].[Site]'; 
          TextSiteNameFilter.Text in SiteName;         
          TextProjectNumberFilter.Text in ProjectNumber;
          TextCityFilter.Text in City
        );  
        City;
        If(_sortAscending; Ascending; Descending) 
    )
)

 


But it’s very long with a lot of redundant code 😕 Is there a more elegant way to solve this?

3 REPLIES 3
Highlighted
Super User III
Super User III

@pekspro 

I have an example that is similar enough to your app that I think you can figure out how to apply it.  Please take a look at the table below.

PowerApps_SortByColumns_Dropdown.gif

Put this code in the Items property of your Gallery to sort based upon the selected column.

 

SortByColumns(
    colProjects,
    drp_Buildings_SortByColumns.Selected.Value,
    Ascending
)

 

The SORTBYCOLUMNS function allows you to decide on the sorted column by passing text value in the 2nd parameter.  This means you no longer have to use the SWITCH logic function.  SORTBYCOLUMNS can be delegated whereas SWITCH cannot.

 

---
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."

Highlighted

Thanks @mdevaney. I tried this:

 

SortByColumns(
    Filter(
      '[dbo].[Site]'; 
      TextInputSiteFilter.Text in SiteName;         
      TextProjectNumberFilter.Text in ProjectNumber;
      TextCityFilter.Text in City
    );  
    _sortOrder;
    If(_sortAscending; Ascending; Descending)   
)

 

But unfortunately this doesn’t work properly. I get warnings about that delegation will not work, and I see that data is missing. If I replace _sortOrder with “SiteName” it does work and I get no warnings, so I guess it only works with a constant 😕

 

Highlighted

@pekspro 
Interesting... then I suppose there is not a way that I know of.

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

News & Announcements

Community Blog

Stay up tp date on the latest blogs and activities in the community News & Announcements.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Community Highlights

Community Highlights

Check out the Power Platform Community Highlights

Top Solution Authors
Top Kudoed Authors
Users online (9,943)