First create a Collection which I’ve called CountryCount. The Collection is created using the ‘Load’ Button. The Collection has two columns. The Continent column holds the name of the Continent and the Number column contain the number of Countries in the source table for that particular Continent
The code for the OnSelect property of the Button is shown below
The first line Clear(colCountryCount) clears the existing colCountryCount Collection. This is because the Collect function can be used inside the subsequent ForAll function but ClearCollect cannot. Imagine if ClearConnect was used inside a ForAll loop. Every time the ForAll loop runs a cycle then the data created in the previous cycle would be removed!
Now on to the main business. It is always worth thinking through the approach before starting to code. To get the output we are looking for, it’s pretty clear that we need to use the ForAll, Filter, CountRows and Distinct functions, but in what order? I like to think it through logically to understand exactly what I want to happen. It works as follows:
ForAll the Distinct values of Continent within the Country table, add the following two Columns to the Collection called colCountryCount. First add the name of the Continent specified in the current iteration of the ForAll loop (referenced by the Result parameter). Then add the number of rows remaining when the Country table is filtered using the current Continent in the ForAll loop (again specified by the Result paramenter)
The resulting code below is assigned to the OnSelect property of the ‘Load’ Button
That’s a great tip. Thanks!