Skip to content
Home » AddColumns function in Power Apps

AddColumns function in Power Apps

The AddColumns function is used to add new columns to a table. This function allows you to create new columns by defining expressions or calculations based on existing data. It does not modify the original table but returns a new table with the additional columns.




Syntax:
AddColumns(Table, ColumnName, Formula [, ColumnName2, Formula2, ...])
  • Table: The original table to which you want to add new columns.
  • ColumnName: The name of the new column you want to add.
  • Formula: The calculation or expression that will determine the values in the new column.
  • (Optional) You can add multiple columns by specifying additional pairs of column names and formulas.

Using AddColumns to add a new column

As you can see, we have a collection named colStudents, which contains student records.

Collect(
    colStudents,
    {
        'First Name': "Sanjay ",
        'Last Name': "Singh",
        'Cource Code': 586,
        Course: "Bio Tech",
        'Enroll Date': DateValue("2020-09-01")
    },
    {
        'First Name': "Manoj ",
        'Last Name': "Rana",
        'Cource Code': 510,
        Course: "Computer Science",
        'Enroll Date': DateValue("2021-08-15")
    },
    {
        'First Name': "Pratap",
        'Last Name': "Aggarwal",
        'Cource Code': 589,
        Course: "Mathematics",
        'Enroll Date': DateValue("2022-02-10")
    }
)
To add a new column to a collection using the AddColumns function, first we will add a button control and name it AddColumns.
Now, go to the OnSelect property of the button and enter the following formula.

This will not directly modify the existing collection but will create a new collection with the added column.

Collect(
    colStudentDetails,
    AddColumns(
        colStudents,
        FullName,
        'First Name' & " " & 'Last Name'
    )
)
Let’s understand the above formula:

colStudentsUpdated: The new collection with the newly added column.

AddColumns: This adds the new column.

colStudents: This is the existing collection.

FullName: The new column being added.

‘First Name’ & ” ” &  ‘Last Name’: To concatenate First Name and Last Name to create the FullName.




Now, click on the AddColumns button, and you will see that a new collection named colStudentDetails is created, which includes the newly added column called FullName

 

Using AddColumns function to add multiple columns

You can also add multiple new columns to an existing collection.
Below formula uses a combination of the ClearCollect and AddColumns functions to create a new collection colStudentDetails based on the existing colStudents collection.

It adds two new columns, FullName and Course Duration based on conditional logic to determine the course duration based on the Cource Code value.

Let’s go to the OnSelect property of AddColumns button and modify the formula.
ClearCollect(
    colStudentDetails,
    AddColumns(
        colStudents,
        FullName,
        'First Name' & " " & 'Last Name',
        'Course Duration',
        If(
            'Cource Code' = 586,
            "6 Months",
            'Cource Code' >= 510,
            "1 Year",
            "2 Year"
        )
    )
)
Let’s understand the above formula:

ClearCollect: This creates or clears and then fills the colStudentDetailscollection with the result of the AddColumns function.

AddColumns: Adds two new columns to the colStudents collection.

FullName: Concatenates ‘First Name’ and ‘Last Name’.

Course Duration: Uses an If condition to determine the course duration based on the Course Code.

Now, click on the AddColumns button, and you will see that a new collection named colStudentDetails is created, which includes the newly added columns called FullName, and Course Duration.

Recommended for you..

Loading

Leave a Reply

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

Discover more from SQL BI Tutorials

Subscribe now to keep reading and get access to the full archive.

Continue reading