Skip to main content

Notifications

Power Apps - Building Power Apps
Suggested answer

Delegation on functions with switch

Posted on by 4
Hi I am creating an app that is used across multiple countries. The app uses Sharepoint as data source but the set of lists changes with the user region. Up to this moment I used something a bite brute-forcish like this (just as an example):
 
Count(Filter(Switch(varRegion, "Region1", List1, "Region2", List2, "Region3", List3, "Region4", List4), field1 =  var1 &&  field2 = var2).ID)
 
What I noticed is that the Switch function with such an approach breaks down delegation on SP, so it works fine with lists with just over 1000 lines, and breaks down on list with over 4k lines. The functions work fine if I split them to not use Switch or If after Filter - however with that the code grows quite significantly with no apparent need. Any way to do it better? Can I somehow store datasource as variable within the tool (it's not environmental so env variable will not work I think)
  • jpespena Profile Picture
    jpespena 294 on at
    Delegation on functions with switch
    Hi @IAmIntractable! Which is why I suggested to store data to collections because as Matthew Devaney said in the article I shared below, functions can only return as many records as delegation limit allows which is 2000. If your list go beyond 2000 records, it will only return the first 2000 records but there are ways to store all your 2000+ records into a collection as shown in the article.
     
    But I also think Warren's suggestion solve the problem as well.
  • WarrenBelz Profile Picture
    WarrenBelz 143,591 on at
    Delegation on functions with switch
    IAmIntractable - that is not true in the case of the initial posted code as a Switch/If statement inside a Filter renders it non-delegable (hence the suggested structure I posted)
  • IAmIntractable Profile Picture
    IAmIntractable 164 on at
    Delegation on functions with switch
    @jpespena 2000 does not apply to the number of rows in the list, its the number of rows returned. As long as the rows in the resultset is <=2000 then no issues.
  • Suggested answer
    SaiRT14 Profile Picture
    SaiRT14 1,334 on at
    Delegation on functions with switch
    check out the following:
    • Use a collection to dynamically store and select data sources.
    • Consider preprocessing large datasets with Power Automate.
    • Optimize filters for delegation compliance to handle larger datasets.
    • Avoid heavy Switch or If logic directly in Filter.
  • WarrenBelz Profile Picture
    WarrenBelz 143,591 on at
    Delegation on functions with switch
    You are asking the data source to deal with dynamic source list possibilities within the filter and it will not do this. Try this as it will only send the correct single delegable query (you also need to get the Count out of the Filter sequence).
    With(
       {
          _Data:
          Switch(
             varRegion, 
             "Region1",
             Filter(
                List1,
                field1 = var1 && field2 = var2
             ),
             "Region2",
             Filter(
                List2,
                field1 = var1 && field2 = var2
             ),
             "Region3",
             Filter(
                List3,
                field1 = var1 && field2 = var2
             ),
             "Region4",
             Filter(
                List4,
                field1 = var1 && field2 = var2
             )
          ),
          CountRows(_Data)
       )
    )
     
    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    MVP (Business Applications)     Visit my blog Practical Power Apps    Buy me a coffee

     
  • Suggested answer
    jpespena Profile Picture
    jpespena 294 on at
    Delegation on functions with switch
    Hi,
     
    If your lists may grow more than 2000 lines, I suggest you store the values in a collection to manage the delegation issue.
     
    Here's a great article by Matthew Devaney on different ways on how you can store lists into collections that can exceed more than 2000 lines:

    https://www.matthewdevaney.com/create-power-apps-collections-over-2000-rows-with-these-4-tricks/

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

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #7 Community Profile Tips…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,591

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 64,090

Leaderboard