How to Generate Row Numbers in Power Apps Collections
Overview
This tutorial walks you through generating row numbers in a Power Apps collection. Row numbers improve data organization and are useful for reporting, indexing, and displaying data in a user-friendly format.
Step 1: Create the Base Collection
Add a button to your screen and name it btnCreateVehicles.
In its OnSelect property, paste the following code:
ClearCollect(
colVehicleList,
{Year: 2021, Make: "Toyota", Model: "Camry"},
{Year: 2022, Make: "Honda", Model: "Civic"},
{Year: 2023, Make: "Ford", Model: "Escape"},
{Year: 2024, Make: "Chevrolet", Model: "Malibu"},
{Year: 2025, Make: "Nissan", Model: "Altima"},
{Year: 2026, Make: "Hyundai", Model: "Elantra"},
{Year: 2022, Make: "Volkswagen", Model: "Passat"},
{Year: 2023, Make: "Mazda", Model: "CX-5"},
{Year: 2024, Make: "Subaru", Model: "Outback"},
{Year: 2025, Make: "Jeep", Model: "Cherokee"},
{Year: 2026, Make: "Kia", Model: "Sportage"}
);
This creates a collection named colVehicleList
with 11 records.
Step 2: Add Row Numbers Using Sequence and Patch
Add another button to your screen and name it btnAddRowNumbers.
In its OnSelect property, use this code:
ClearCollect(
colVehicleListWithRow,
ForAll(
Sequence(CountRows(colVehicleList)),
Patch(
Index(colVehicleList, Value),
{ RowNumber: Value }
)
)
);
This creates a new collection named colVehicleListWithRow
that includes a RowNumber
column.
Step 3: Display the Collection in a Data Table
1. Add a Data Table control to your screen (e.g., name it DataTableVehicles).
2. Set its Items property to:
colVehicleListWithRow
3. With the Data Table selected, click on Edit Fields in the right pane.
4. Click + Add field and choose the fields you want to display: RowNumber
, Make
, Model
, and Year
.
Important Notes and Troubleshooting
- ⚠️ Make sure you first click the Create Vehicles button to generate
colVehicleList
. - Then click the Add Row Numbers button to generate
colVehicleListWithRow
. - If
Add Field
is disabled in the Data Table, it means the collection doesn’t exist yet or is empty. Preview (F5) and click the buttons in order. - To verify that the collection has been created, go to View > Collections in the Power Apps ribbon and check the structure of
colVehicleListWithRow
. - Use a
Label
control to test output:CountRows(colVehicleListWithRow)
Conclusion
Adding row numbers to a collection in Power Apps helps create structured, organized data for presentation and interaction. With a combination of Sequence
, ForAll
, Patch
, and Index
, you can easily enhance your dataset without modifying the original records.
If you found this guide helpful, explore our other tutorials:
- Using Switch Controls for User Roles in Power Apps
- Dynamic Galleries with Sorting and Filtering
- Creating Custom Popups Without Code Components
Happy building!