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

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
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Don't miss the call this month on June 16th - 8a PDT

Top Solution Authors
Top Kudoed Authors
Users online (18,809)