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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / LastN from selected value
Power Apps
Unanswered

LastN from selected value

(0) ShareShare
ReportReport
Posted on by 4

Hello PowerApps Community, this is my first time posting here :).

 

I have created a collection showing the last 7 values/entries of two columns (Solved & CW). This is correct but I want to show the last 7 values/entries from the selected CW (Calendar Week). I have a dropdown menu where one can select the CW (called dd_cw). For example, you select CW 9 from the dropdown, then the collection should show CW 3, 4, 5, 6, 7, 8, 9. If CW 8 is selected it should show 2, 3, 4, 5, 6, 7, 8. How can I achieve that? I appreciate any help!

 

My Code:

Clear(colTest);
ForAll(
 LastN(KPI_ServiceDesk, 7),
 Collect(colTest,
 {Solved: ThisRecord.Solved,
 CW: ThisRecord.CW}
));

 

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

    @ekalem 

    To start, your formula has the ForAll backward. You are trying to use it like a ForLoop in some development language - which PowerApps is not.  It is a function that returns a table of records based on your iteration table and record schema.

    It is more efficient to use the function as intended and will provide better performance.

     

    If you want it based on your dropdown, then, assuming your dropdown has the value for the amount you want, your Formula would be:

     

    ClearCollect(colTest,
     ForAll(LastN(KPI_ServiceDesk, Value(yourDropdown.Selected.Value)),
     {Solved: Solved,
     CW: CW}
     )
    );

     

     

    I hope this is helpful for you.

  • SagarVIS96 Profile Picture
    212 on at

    Hey 

    @ekalem 
    You can try the following line of code I think it will help you:

    ClearCollect(
     colTest,
     ShowColumns(LastN(
     Filter(
     colCW,
     CW = ComboBox2.Selected.Result
     ),
     7
     ),"Solved")
    )

    Please click "Accept Solution" if it helps you to solve your problem.
    Thanks

  • ekalem Profile Picture
    4 on at

    Thanks for the quick answer! But wouldn't that show the last x entries? If the dropdown is selected with 8 for example, would it not show the last 8 values? The problem is that it needs to show the last 7 values all the time but only from the point (CW) that is selected on the dropdown. So for example, if the dropdown (dd_cw) is selected to 9, it should show the last 7 entries from CW 9 (CW3, CW4, CW5, CW6, CW7, CW8, CW9).

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @ekalem 

    Yes, I was basing it a little off of the formula you had to start with.

    If you need to base it off of the number, then the formula would be:

    ClearCollect(colTest,
     ForAll(
     LastN(
     Filter(
     Sort(KPI_ServiceDesk, CW), 
     CW <= Value(yourDropdown.Selected.Value)
     ),
     7
     ),
     {Solved: Solved,
     CW: CW}
     )
    );

     

    I am not clear if your CW field is a numeric field or a text field.  The above assumes numeric.  If not, then a change needs to be made.

     

     

  • ekalem Profile Picture
    4 on at

    I really appreciate your help! Yes, the CW field is numeric. I tried your code (on a "refresh" button -> onSelect). It seems like it creates a collection. But the collection does not have the values inside of it. Only the labels (CW & Solved). I tried to change the dropdown selection but it didn't help.

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @ekalem 

    Well, not really sure of the purpose of the collection, but the formula should return a table of items that can be used on your controls, or duplicated in a collection if you need that for some reason.

     

    To test things out, I would recommend using a gallery and set the Items property to:

    ForAll(
     LastN(
     Sort(
     Filter(KPI_ServiceDesk, 
     CW <= Value(yourDropdown.Selected.Value)
     ),
     CW
     ),
     7
     ),
     {Solved: Solved,
     CW: CW}
    )
    

    Add a label to the gallery and set the Text to: ThisItem.CW

     

    You'll be able to see it functioning a little better this way.

     

    Also, I did change the formula a little only to make it a little more performant - it is wiser to filter, then sort, rather than sort and then filter. 

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 721 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 320 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard