Latest Posts

PowerApps - Add a ROW NUMBER in List


In this article, we will see how we can add a Row Number column in a collection List. By default, there is not any straight forward way where you can just add a column having an incremental number to identify each row differently. Let’s see through example, how we can add a Row Number column in the existing list.

Table Name: Weathers

PowerApps Examples

I will specify Two Methods here.

Add Row Number Column using two List Collections

Main List: Weathers (This is the main list or table or database containing actual data)

List Collection 1: NewWeathersList (This list will be bound to Gallery or Table)

List Collection 2: CounterList (This list is just used to maintain a counter for Row Number)

Method Steps:

  1. We will create a new list and add a column called RowNumber
    Explanation:
    • Here, we create a new list called NewWeathersList.
    • Add a column called RowNumber.
    • This column will hold value “0” for now.
      ClearCollect(NewWeathersList, AddColumns(Weathers, "RowNumber", 0));
  2. Create a New List to store counter value.

Explanation:

  • Create a new list called CounterList.
  • We will keep only one column and one row in this list.
  • By default, we will keep value = 1.

ClearCollect(CounterList, {CounterValue: 1});

  1. We will now loop through all the rows of the main list and update the Row Number Column.
    Explanation:
    • We will loop through all the records in Main List – Weathers
    • In each loop, we are doing two things.
      1. Patch 1: Update NewWeatherList RowNumber using the Value from CounterList. As we are using the LookUp function on NewWeatherList, it will get on the first row of the list and update it.
      2. Patch 2: Update CounterList with (CurrentValue + 1), so that in the next loop the value can be used.

 
ForAll(

    Weathers,

    Patch(

        NewWeathersList,

        LookUp(

            NewWeathersList,

            RowNumber = 0

        ),

        {RowNumber: First(CounterList).CounterValue}

    );

    Patch(

        CounterList,

        First(CounterList),

        {CounterValue: First(CounterList).CounterValue + 1}

    )

);

  1. Assign Items property to NewWeathersList as a data source for your Gallery or Table.
  2. Complete Code below. You can add this code on Screen - Visible and then for Gallery, set the Items property to the list Collection (NewWeathersList)

ClearCollect(NewWeathersList, AddColumns(Weathers, "RowNumber", 0));

ClearCollect(CounterList, {CounterValue: 1});

ForAll(

    Weathers,

    Patch(

        NewWeathersList,

        LookUp(

            NewWeathersList,

            RowNumber = 0

        ),

        {RowNumber: First(CounterList).CounterValue}

    );

    Patch(

        CounterList,

        First(CounterList),

        {CounterValue: First(CounterList).CounterValue + 1}

    )

);

Powerapps row number output

Let’s achieve similar output using a bit different method.

Add Row Number Column using one List Collection

Main List: Weathers (This is the main list or table or database containing actual data)

List Collection: NewWeathersList (This list will be bound to Gallery or Table)

Method Steps:

  1. Create a new list – NewWeathersList
    Clear(NewWeathersList);
  2. Loop through all the rows of the existing list– Weather.

Explanation:

  • For each row in the Main List, it is doing the following functionality.
    • The last function gets the last row from the table and FirstN gets the first – N rows from the tale.
    • So based on the below formula, in each loop, first it fetches first (N) records, and then it gets the last row and adds it to the collection.

ForAll(

    Weathers,

    Collect(

        NewWeathersList,

        Last(

            FirstN(

                AddColumns(

                    Weathers,

                    "RowNumber",

                    CountRows(NewWeathersList) + 1

                ),

                CountRows(NewWeathersList) + 1

            )

        )

    )

)

  1. Complete Code below. You can add this code on Screen - Visible and then for Gallery, set the Items property to the list Collection (NewWeathersList)

Clear(NewWeathersList);

ForAll(

    Weathers,

    Collect(

        NewWeathersList,

        Last(

            FirstN(

                AddColumns(

                    Weathers,

                    "RowNumber",

                    CountRows(NewWeathersList) + 1

                ),

                CountRows(NewWeathersList) + 1

            )

        )

    )

)

Let us know if you have any difficulties adding Row Number in your application or you have any easier method to implement.


We value your Feedback:

Page URL:

Name:

 

Email:

 
 

Suggestion:

 

© 2024 Code SharePoint