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 ForAll to count ...
Power Apps
Answered

Using ForAll to count qualifying records in a table

(0) ShareShare
ReportReport
Posted on by 373

Hi,

 

In our system which manages equipment, there is a Dataverse table of equipment called Assets, and each item of equipment is assigned an EquipmentCategory, which is a lookup value from another table called Categories. The Categories table is a simple table which contains the category title and the annual maintenance contract value e.g

 

Column headings: Title, AnnualCost

 

record values:

"cat 1", 50

"cat 2", 75

"cat 3", 100

 

etc.

 

I would like to be able to iterate through the Categories table, and for each category, determine how many records in Assets there are for each category, resulting in a collection e.g.

 

"cat 1", 2

"cat 2", 4

 

However, I see that CountIf can't be used within a ForAll function and so I'm stuck now as to how to achieve this neatly. I don't want to have a line of code for each category, as this means (a) it's inefficient and inelegant and (b) if any categories are added in the future I'll need to change the code.

 

Any help greatly appreciated!

thanks

Martin

 

Categories:
I have the same question (0)
  • Verified answer
    iAm_ManCat Profile Picture
    18,256 Most Valuable Professional on at

    Based on what you've said, you want to show how many Assets are in each category type, the below pseudocode will need some tweaking to work with your Assets table and the Category lookup, but its a good starting point

     

    Clear(Categorytotals);
    ForAll( Distinct(Categories, Title) As CVal,
     Collect(Categorytotals, 
     {
     Category: CVal.Value, 
     NumberOfAssets: CountRows(Filter(Assets, Category.Title=Cval.Value))
     }
     )
    )

     

  • Gochix Profile Picture
    1,937 Moderator on at

    The only thing I would change here is how the ForAll function is performed.

     

    Collect(
     CategoryTotals,
     ForAll(
     Distinct(
     Categories, Title) As CVal,
     {
     Category: CVal.Value,
     NumberOfAssets: CountRows(Filter(Assets, Category.Title = CVal.Value))
     }
     )
    )

     


    _____________________________________________________________________________________
    Please give a thumbs up if I resolved your issue! Please click Accept as Solution to close the topic!

  • Martin_W Profile Picture
    373 on at

    Hi,

     

    Unfortunately I get a "The specified column is not accessible in this context" error, but this line works outside of the ForAll function.

     

    thanks,

    Martin

  • timl Profile Picture
    37,283 Super User 2026 Season 2 on at

    Hi @Martin_W 

    Whilst this doesn't answer your question, you could consider implementing this calculation at the Dataverse level using a rollup column on the Categories table.

    It'll be a bit more efficient as you won't need to retrieve the Assets within the ForAll  just to count them. It would also provide some additional filtering capabilites should you need it at a later time (eg, to call Filter on the categories table to return category records with 0 assets).

  • Verified answer
    Martin_W Profile Picture
    373 on at

    The fix for the "specified column is not accessible in this context" is to enable the option for "Record scope one-to-many and many-to-many relationships"

  • Martin_W Profile Picture
    373 on at

    Thanks @iAm_ManCat 

     

    The CountRows method has been the solution, although I had to enable the option that I mentioned in another reply.

     

    Many thanks

    Martin

     

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

#2
11manish Profile Picture

11manish 201 Super User 2026 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 128 Super User 2026 Season 2

Last 30 days Overall leaderboard