How often would you like to count the number of records without really wanting to load all the records that exist in a list or table. CountRows, CountIf, CountA and Count are four functions to get a record count in Power Apps.

Various datasources

In this post I’m going to consider both SharePoint and Dataverse only. If you use SQL you will find that a quick Stored procedure will give you the count of the records anyway.

The Count functions

Power Apps has 4 count functions that we can use to get the number of records in a list, table or collection.

  • CountRows
  • CountIf
  • CountA
  • Count

CountRows

CountRows returns the number of records in a data source.

CountRows(MyData)

CountIf

CountIf returns the number of records that match certain criteria. Settings the query to true will return all records.

CountIf(MyData, true)

CountA

CountA returns the number of records that don’t have a blank value in a single text column. Empty text values “”, are considered not blank.

CountA(MyData.TextField)

Count

The Count function counts the number of records that contain a number in a single column.

Count(MyData.ID)

Using the count functions in SharePoint

Now that we understand the various count functions, I’m going to use them on a SharePoint List with thousands of records.

ExpressionResultDetails
CountRows(MyList)500The CountRows function is not delegable and only the first 500 records are counted.
CountIf(MyList, true)500The CountIf function is not delegable and only the first 500 records are counted.
CountA(MyList.Title)500The CountA function is not delegable and only the first 500 records are counted.
Count(MyList.ID)500The Count function is not delegable and only the first 500 records are counted.
CountRows(Gallery.AllItems)100 – 500+Now this could be an interesting one. If you have a gallery, the CountRows on the all items will increase as the user scrolls down. Plenty of situation where displaying 500+ will be accurate enough.

Using Power Automate or reading the data in a collection is of course an option. The code as shown below could work, however loading all the data into your app might not be very memory efficient.

CountRows on a collection
CountRows on a collection
ClearCollect(colData, MyList);
Collect(colData, Filter(MyList, Created > Last(colData).Created));
Collect(colData, Filter(MyList, Created > Last(colData).Created));
Collect(colData, Filter(MyList, Created > Last(colData).Created));
Collect(colData, Filter(MyList, Created > Last(colData).Created));
Collect(colData, Filter(MyList, Created > Last(colData).Created));
CountRows(colData);

Using the count functions in Dataverse

If we now switch over to Dataverse we will find completely different results.

ExpressionResultDetails
CountRows(MyTable)11894 (I had a lot of records added to my table while running this test)The CountRows function is delegable however the number of records returned comes from cache. This means that if you have dynamic content this may not be very accurate
CountIf(MyTable, true)19454 (I didn’t have more records in my table)The CountIf function is not only delegable it also returns the correct number of records.
CountA(MyTable.TextColumn)500The CountA function is not delegable and only the first 500 records are counted.
Count(MyTable.ID)500The Count function is not delegable and only the first 500 records are counted. Also, for this function to work you will need a Number column
CountRows(MyTable.AllItems)100 – 500+Now this could be an interesting one. If you have a gallery, the CountRows on the all items will increase as the user scrolls down. Plenty of situation where displaying 500+ will be accurate enough.

Of course it is great to see that CountIf gives me the right number of records in my table. The number of records however is limited to 50000. If you need to get a higher number of record count back then you might need to look into the workaround mentioned in the SharePoint section of this post.


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

2 thoughts on “CountRows, CountIf, CountA and Count in Power Apps

  1. It’s good to know that CountRows against dataverse source is cached, and there is nearly nothing to do to force refresh.
    The refresh is made automatically anytime in the day, you’ll never know when, and it can be done once or twice a day.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.