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
Super User

Re: Combining sort, filter and switch

@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
Regular Visitor

Re: Combining sort, filter and switch

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
Super User
Super User

Re: Combining sort, filter and switch

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

Helpful resources

Announcements
Check this Out

Helpful information

Featuring samples like Return to the Workplace and Emergency Response Applications

August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

secondImage

Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

secondImage

Super Users Coming in August

We are excited for the next Super User season.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

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