#PowerPlatformTip 93: 'Add RowNumbers'
TL;DR: Add row numbers to records already loaded into a Power Apps gallery or collection with a
With/ForAll/Sequenceformula. This does not remove delegation or row-limit constraints.
💡 Challenge
Incorporating a row number feature in PowerApps galleries and other elements can significantly enhance user interaction and data management. However, understanding how and where this operation is executed is vital.
✅ Solution
Implement row numbering within your app for records already loaded into the local table. The formula does not retrieve unloaded rows or change the data source’s delegation behavior or app row limit.
🔧 How It’s Done
Use the following formula (thanks to Matthew Devaney) to add a row number to each record:
With({locLoadedData: YOUR DATA},
Ungroup(
ForAll(
Sequence(CountRows(locLoadedData)),
{myRecord: Table(Index(locLoadedData, Value)), RowNumber: Value}
),
"myRecord"
)
)
Replace YOUR DATA with your specific formula. This operation runs within the app on locLoadedData; it cannot number records that were not loaded. Check that the source query and loaded row count meet your needs.
🎉 Result
Whenever the formula recalculates, each row in the loaded table receives a position-based number. This does not mean every matching source record was loaded.
🌟 Key Advantages
🔸 Enhanced user experience with clear data referencing.
🔸 In-app numbering for the currently loaded rows.
🔸 No external processing or storage required.
🔸 The numbering runs inside the app on loaded rows; it sends nothing to the data source and does not bypass delegation limits.
🔁 Shorter Alternative: Patch Inside ForAll
The Ungroup wrapper exists because ForAll returns one record per iteration and the data record has to be unpacked again. Patch on the indexed record does the same job in one step:
With({locLoadedData: YOUR DATA},
ForAll(
Sequence(CountRows(locLoadedData)) As s,
Patch(Index(locLoadedData, s.Value), {RowNumber: s.Value})
)
)
Tested on a four-row table (Anna, Ben, Cara, Ben): the result is the four original rows plus RowNumber 1 to 4, duplicate names included. The same formula numbered three filtered rows of a Dataverse table 1 to 3. As s is optional here; Patch(Index(locLoadedData, Value), {RowNumber: Value}) without it returns the same result.
Start value and step
🔸 Zero-based: Sequence(CountRows(locLoadedData), 0) and Index(locLoadedData, s.Value + 1) give RowNumber 0 to 3.
🔸 Steps of ten: Sequence(CountRows(locLoadedData), 10, 10) with Index(locLoadedData, (s.Value - 10) / 10 + 1) gives 10, 20, 30, 40.
🎥 Video Tutorial
🛠️ FAQ
1. Can I apply this technique to any data source in PowerApps?
Yes, for records successfully loaded into the local variable or collection. The formula does not bypass delegation or the app’s data row limit.
2. How can I adjust the starting value of the row numbers?
You can customize the Sequence function by adding start or step parameters, or simply add/subtract an offset to Value in the record formula. Careful: the same s.Value is also the table position in Index(locLoadedData, s.Value). If you change the start or step in Sequence, convert the index back as shown under “Start value and step”; if you only shift the displayed number, keep Index one-based, for example Patch(Index(locLoadedData, s.Value), {RowNumber: s.Value - 1}).
3. Will this method update row numbers automatically on data refresh?
It recalculates when its inputs change. It numbers the rows in the loaded table; refresh does not guarantee every matching source row is loaded.
4. Is there a shorter formula without Ungroup?
Yes: ForAll(Sequence(CountRows(Data)) As s, Patch(Index(Data, s.Value), {RowNumber: s.Value})). It returns the original rows with a RowNumber column, numbered 1 to n, and needs no Ungroup.
5. Do duplicate rows get the same number?
No. Numbering is by position, so two identical rows get two different numbers (Ben got 2 and 4 in the test).
🔗 Related Tips
🔸 #PowerPlatformTip 16: ‘PowerApps: Collections Cookbook’ - more copy-paste With/ForAll/Sequence-style recipes for collections.
🔸 #PowerPlatformTip 32: ‘Optimize with GroupBy’ - another in-app technique for reshaping data; check delegation behavior for your source and formula.
This tip is by Marcel Lehmann, Microsoft MVP (Business Applications) and founder of the Power Platform Academy, which runs free live workshops (in German). See the next free workshop dates (in German). For companies that want this implemented for them, see KMUpower: Power Platform consulting & automation for SMEs (DACH).