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 / Odd behavior using Fir...
Power Apps
Answered

Odd behavior using First & Filter together - getting *second* record not first unless wait long time

(1) ShareShare
ReportReport
Posted on by 1,011

The code I'm using consistently returns the next most recent entry in a SharePoint list as opposed to the most recent one. I built out a whole screen to test this so I could see everything going on. This allowed me to determine definitively that the filter part of the function is what causes this behavior. With the filter included it fails to find the very last qualifying record. Take the filter out and it works perfectly (except I need to have the filter of course).

 

Sequence is run code > add a record to SP > run code again in a minute or two and see failure. 

 

I'm at a loss... how can a documented function behave so intermittently? How are we supposed to build reliable code blocks when the simpliest little piece of code only sometimes returns the last record?

 

The only other thing that helps is waiting a LONG time (like 15 minutes). Then it works the first time but executing again even a few minutes later it fails the first time again. 

 

The target list is brand new, containing only plain text, number and date columns. Explicitly setting the ID using LookUp easily allows me to return the correct record, just not when I find it based on the filter condition. 

 

So to clarify, with the filter in place in the example below, I expect to get record ID: 5 but I consistently get ID: 6. If I execute the code again, I then get the correct result (5) which appears to demonstrate that  it is not the terms of the filter but that the last record is somehow not in a state that allows it to be returned in the result set the first time the code executes (even making all the records have filter parameters that qualify does not fix further demonstrating this).

 

Does not work

Events 

IDEventClass TaskID  
8Task983I want
7Task983I get
6Task983 
5Task983 

//find most recent previous entry matching conditions Refresh(Events); Set(varPreviousEvent, First( Sort( Filter( Events, EventClass = varCurrentEventClass, TaskID = varTask.ID), ID, Descending) ) );

 

Works

Events 

IDEventClass TaskID  
8Task983I want & I get
7Task983 
6Task983 
5Task983 

Refresh(Events); Set(varPreviousEvent, First( Sort( //Filter( Events, //EventClass = varCurrentEventClass, TaskID = varTask.ID), ID, Descending) ) );

 

Some things that didn't work:

  • Substituting actual values instead of variables in filter.
  • Collecting the list first, then filtering the collection (collection didn't include the very last result so didn't matter).
  • Refreshing before, after, before & after.
  • Switching the order of the Sort & Filter statements.
  • Running the same block of code twice in succession.
  • Using only one filter statement, tried both separately.

Update: I just recreated another SP list and a new app and was EASILY able to recreate this issue... code only works the *second* time you click the button unless you wait several minuts. I need to use this code block on the fly to find and update records and can't have it only work sometimes, when it feels like it. I really don't know what to do... I need this simple function in dozens of places around my app. Is this a bug? 

Categories:
I have the same question (0)
  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    Hi @PhilD,

    I was going to see if I could reproduce your issue on my local machine.  I have one quick question so that my test is similar to yours: how many records do you have in your list? 

  • PhilD Profile Picture
    1,011 on at

    Thanks so much @Drrickryp

     

    The target list in this case started with about 100 but is now up to around 500 from my testing.

     

    Delegation does not appear to be impacting this as I tried it with the app limit set well over the total records and to 1 with the same results.

  • PhilD Profile Picture
    1,011 on at

    Loading items into a gallery seems to work better but it is still fails about every fifth time, probably based on how fast I add the record then go back and click the button. 

     

    The code in PowerApps does not seem to always progress in a linear fashion as documented. I mean in theory I should be able to patch a new record > refresh datasource > then find that record by First(Filter(Sort but this straight up does not work. I'm catching all my errors to a table and only get etag errors if I try multiple operations without the refresh (which really slows everything down but that's another story).

     

    I suspect this would work much better using a SQL source as the latency may be coming from the SP list but it is too late to make this switch now so I am kind of stuck I guess. 

     

    Loading into gallery first

     Refresh(Events); 
     Set(varPreviousEvent,
     First( 
     Filter(
     Sort(
     GalleryAllRecentEvents.AllItems,
     ID, Descending),
     EventClass = varCurrentEventClass,
     TaskID = varTask.ID)
     )
     );
  • PhilD Profile Picture
    1,011 on at

    I don't get it...

     

    I just sat here and added records to the SharePoint list, refreshed the list sorted by ID and noted that the highest ID was 665. I then waited 30 seconds and went to a button on my app and pressed it with this exact code and watched varOutputOperation1 update from 660 to 663. I pressed it again and watched it update to 665. This is by far the simplest part of my whole app and it doesn't work! How can I rely on a PowerApp full of complex code if a dead simple formula like this one can not work as expected? 

     

     Set(varOutputOperation1,
     First( 
     Filter(
     Sort(
     Events,
     ID, Descending),
     EventClass = varCurrentEventClass,
     TaskID = varTask.ID)
     )
     )
  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    Hi @PhilD,

    I'm not sure about your datasource and other variables but since you are using ID as the filter and it's unique. Why bother with the first, filter and sort.

    Set(varPreviousEvent,
     Lookup(GalleryAllRecentEvents.AllItems, 
     EventClass = varCurrentEventClass,
     TaskID = varTask.ID)
     )

     Lookup() is delegatable in Sharepoint and all of the work will be done on the server side.  It ought to be fast.  Same would go throughout your app where ever you are using First(Filter(Sort)))

  • PhilD Profile Picture
    1,011 on at

    Hmm... so you are saying that LookUp returns the highest ID by default so all I need are the filter arguments?

    The filter had seemed to be the problem but this certainly seems to be worth a shot... let me see if this changes things and THANKS! I'm really tearing my hair out on this on 😕

  • PhilD Profile Picture
    1,011 on at

    There must be something wrong with either my app or my list.

     

    Returns the first (lowest ID) record meeting both conditions

     

     Set(varNewPreviousEvent,
     LookUp(Events, 
     EventClass = varCurrentEventClass && TaskID = varTask.ID
     )
     )

     

    Behaves exactly like what was happening before, add records > click > no change > click again > right result

     Set(varNewPreviousEvent,
     LookUp(Sort(Events,ID, Descending), 
     EventClass = varCurrentEventClass && TaskID = varTask.ID
     )
     )

     

  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    Hi @PhilD,

    Lookup returns the first record in the specified datasource that matches its equation ie. returns a true value.  ID's should be unique so you don't need to filter them at all or sort them descending unless you are over the delegation limit. (You wouldn't find it otherwise).   To return a specific field from the found record, say for a label in a gallery, you would use the third argument of the Lookup function. ie Lookup(datasource,ID=someIDvalue,fieldname) 

    ref: https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/function-filter-lookup 

  • PhilD Profile Picture
    1,011 on at

    Thanks, I do understand how this works and it works perfectly the SECOND time the button is clicked.

     

    I need to filter by the IDs because it is part of a much larger and more complex operation. The IDs I'm filtering by are not the auto incremented IDs of the target list but rather are IDs written to these previous events when they occurred. The objective is to find the last matching event and write the number of minutes since its creation and the current time. All of this is easy but it is based on finding the correct entry (not just the last one) the FIRST time around. This little block of code (which should work) will be part of a larger series of steps and I won't be around to click the button twice.

  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    @PhilD,

     

    I have tried some similar stuff on my side and used a different approach and it seemed to work ok.  Basicly, I took a Sharepoint dataset without any Choice, Lookup or Person columns and let Powerapps create the App from the list. It was about 300 or so items. I then made a collection from the list (colsplist,Splist) and used that as the items in the gallery.  I modified the Edit form and View form screens still had the Datasource was the original Splist but I changed the Item property from Gallery1.Selected to Lookup(Splist, ID=Gallery1.Selected.ID); you need to do that because the datasource of the gallery is no longer identical to the datasource in the Edit form.   In the OnSuccess property of the EditForm, I put in ClearCollect(colsplist,Splist).  When I went back to the Gallery screen, the item was correct.  

    I'm not saying that this simple solution would work in your case but using collections whereever possible could cut out the latency issue you are having in your app.  You would need to step back and think over whether it is possible to do that. You will always have to write your changes to the underlying Sharepoint list but use ClearCollect to recreate the collection after you have made the change. 

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 379 Most Valuable Professional

#2
11manish Profile Picture

11manish 203 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 128 Super User 2026 Season 2

Last 30 days Overall leaderboard