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 / Reuse code inside a da...
Power Apps
Answered

Reuse code inside a data operation

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I have this piece of code to pull data from a SharePoint and put it into a collection called 'SpendData'. The collection is made by taking a list of retailers and adding two columns, 'SearchAmount' and 'iMediaAmount' and giving these columns values based on another list, 'Data'. The reason why I don't create the collection based on 'Data' alone is because new retailers may not be included in the list.

ClearCollect(SpendData,ShowColumns(AddColumns(RetailerList,
 "SearchAmount",
 0 + If(Approved,LookUp(Data,Retailer=RetailerList[@Title]&&Title=CategoryV).CurrentAmount,LookUp(Data,Retailer=RetailerList[@Title]&&Title=CategoryV).PendingAmount),
 "iMediaAmount",
 0 + If(Approved,LookUp(Data,Retailer=RetailerList[@Title]&&Title=CategoryV).iMedia_x002f_OtherCurrent ,LookUp(Data,Retailer=RetailerList[@Title]&&Title=CategoryV).iMedia_x002f_OtherPending)),"Title","SearchAmount","iMediaAmount")
 

I have two main issues with this code:
-In the 'If' statements, I repeat the lookup for each condition. Is there any way that I can do the lookup and have the if statement simply decide which attribute to pull.

- The code runs slow, sometimes taking 13 seconds to load which is a little annoying. Is there anything I can do to speed this up?

 

Is there anything I can do to make this more concise and/or faster? Thank you for your help.

Categories:
I have the same question (0)
  • Verified answer
    WarrenBelz Profile Picture
    156,425 Most Valuable Professional on at

    Hi @Anonymous ,

    I cannot test this (and free-typed so watch commas, brackets etc), but this structure may speed it up - I assume Data is under 2,000 items (as the lookup would not work anyway if it was)

    With(
     {
     wData:
     RenameColumns(
     Data,
     "Title",
     "TitleD"
     )
     },
     ClearCollect(
     SpendData,
     ShowColumns(
     AddColumns(
     RetailerList,
     With(
     {
     wDataVal:
     LookUp(
     wData,
     Retailer=Title && 
     TitleD=CategoryV
     )
     },
     "SearchAmount",
     If(
     Approved,
     wDataVal.CurrentAmount,
     wDataVal.PendingAmount
     ),
     "iMediaAmount",
     If(
     Approved,
     wDataVal.iMedia_x002f_OtherCurrent,
     wDataVal.iMedia_x002f_OtherPending
     )
     )
     ),
     "Title",
     "SearchAmount",
     "iMediaAmount"
     )
     )
    )

     

    Please click Accept as solution 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 Thumbs Up.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    It's not letting me nest the 'With' function how you have it. It throws errors saying that the addColumns function only has two arguments, when it requires three.

  • WarrenBelz Profile Picture
    156,425 Most Valuable Professional on at

    @Anonymous ,

    OK - you can still repeat all of that (the main purpose was to avoid that), but the main thing was the With() at the top which causes the Lookup at the bottom to be done "locally" rather than back to the data source.

     

    Please click Accept as solution 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 Thumbs Up.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    But wouldn't that have to load the whole data source, taking longer? The data I'm pulling does not make up the entire data source. Maybe this would be solved with a filter?

  • WarrenBelz Profile Picture
    156,425 Most Valuable Professional on at

    @Anonymous ,

    How big is the data source? I believe the issue is the Lookup going back to the data each time (your question was about performance). A smaller data source does not take long to pull back once and then the lookup is done locally - although you still have one data call (your original code involves going back to two data sources on each lookup).

     

    Please click Accept as solution 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 Thumbs Up.

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

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 345 Most Valuable Professional

#2
11manish Profile Picture

11manish 207 Super User 2026 Season 2

#3
Mohsin Ali Profile Picture

Mohsin Ali 177

Last 30 days Overall leaderboard