web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Using a conditional da...
Power Apps
Unanswered

Using a conditional data source in Patch based on dropdown selection

(1) ShareShare
ReportReport
Posted on by 1,207

Is it possible to conditionally supply datasource to a patch statement? I have tried by inserting a switch and also using With but Patch returns an error.

 

I managed to do this with Remove so not sure why it isn't working with Path. I think this would be useful rather than needing to write out multiple patch statements in an IF statement.

 

I even tried putting the record to Patch inside a With and hoping to Patch the ThisRecord value as a way of not repeating the same record to be patched over and over, but that didn't work either.

 

Patch(
 Switch(
 dcLocation.Selected.Value,
 "Loc1",List1,
 "Loc2",List2,
 "Loc3",List3,
 "Loc4",List4,
 List5,
 ),
 gblSelectedRecord,
 {Column1:"new value"}
)

 

This does not display an error...

Remove(
 Switch(
 dcLocation.Selected.Value,
 "Loc1",List1,
 "Loc2",List2,
 "Loc3",List3,
 "Loc4",List4,
 List5,
 ), 
 {ID:gblSelectedRecord.ID}
)

 

Categories:
  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @Digital 

     

    We can reproduce what you are facing.

     

    Power Apps is confused by this manner of expression because the Switch is being implicitly presumed as a Collection. It really hates the Switch to be specified inline as the first parameter of Patch - even if it is abstracted into a Variable. Perhaps it must be abstracted to a Collection explicitly instead in order not to get an error.

     

    So instead try something like this:

     

     

     

    Set(_SwitchVar,Switch(
     Dropdown1.Selected.Value
     ,
     "1",JSONLIST
     ,
     "2",JSONLIST2
     )
    );
    
    ClearCollect(_ovTmp,_SwitchVar);
     
     Patch(
     _ovTmp
     ,
     Defaults(_SwitchVar ),
     {Title:Text(Dropdown1.Selected.Value)}
    )

     

     

     

    ----------------Below is for additional info:

    This answer presumes the following simple test case only, but is probably applicable to your scenario too:

    (presumes this is on Screen1 OnVisible)

     

     

    Set(_SwitchVar,JSONLIST);
    ClearCollect(_ovTmp,_SwitchVar);

     

     



    (also presumes DropDownSample a single column table with "1" "2" and "3" as items).

     

    (also presumes one Dropdown called DropDown1 and one button called Button3. Presumes the code at very top of this post is placed in OnChange of Button3).

     

    It was tested against SharePoint List.  However the above was only tested for not giving an error. It is up to you to check on the rest if it really works.

    Evidence it does not cause any error is shown below:

    vm78934_2.png

     

     

  • Digital Profile Picture
    1,207 on at

    @poweractivate ,

     

    Thanks I'll give that a try. I tested the Remove with the switch and although there was no warning in the editor I got a runtime error so that doesn't work.

     

    No drama, I was just trying to avoid a large block of repeating code. Thanks again. 

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 397 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 354

#3
WarrenBelz Profile Picture

WarrenBelz 232 Most Valuable Professional

Last 30 days Overall leaderboard