cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
pekspro
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
mdevaney
Super User
Super User

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

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 😕

 

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

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Users online (4,063)