Hello Friends,
Welcome back with some new topics. Today, we will discuss about Column operations in PowerApps. PowerApps provides below options to play on columns-
- AddColumns
- ShowColumns
- DropColumns
- RenameColumns
These functions are used to re-shape the collection/table in PowerApps. Apart from that, it also provide functionality to get distinct data using Distinct function.
In this post, we will learn about:
- Add/Show/Drop/Rename Column functions
- Distinct function
- Adding Blank Record (or "Please Select") at Beginning/End of collection (beneficial when binding with Dropdown)
- Add ID using Filter function in collection having Distinct data
Let's start one by one.
- First we will create a collection. I had created a collection named "collSampleData". The data is:
-
ClearCollect( collSampleData, { ID: 1, FName: "Sachin", LName: "Jain" }, { ID: 2, FName: "Lokesh", LName: "Kumar" }, { ID: 3, FName: "Rajeev", LName: "Khanna" }, { ID: 4, FName: "Anuj", LName: "Sharma" }, { ID: 5, FName: "Rupesh", LName: "Jaiswal" }, { ID: 6, FName: "Anuj", LName: "Sharma" }, { ID: 7, FName: "Sachin", LName: "Jain" }, { ID: 8, FName: "Sanjiv", LName: "Sharma" }, { ID: 9, FName: "Rajeev", LName: "Sharma" } );
- It will create a collection with 9 records. Here I had added couple of duplicate entries which we will use for Distinct function.
- If we check the collection, it has:
- Add Column (AddColumns):
- AddColumns function is used to add column to the collection/table. You may add a calculated column, add and existing column with different datatype. I am using to add a calculated column by adding FName & LName.
-
ClearCollect( collWithFullName, AddColumns( collSampleData, "FullName", FName & " " & LName ) );
- If we check the collection, it has:
- This way, we can add columns to collection.
- Show Column (ShowColumns)
- ShowColumns is used to include only those columns in table/collection that needs to be displayed and rest needs to be dropped.
-
ClearCollect( collShowColumnData, ShowColumns( collWithFullName, "ID", "FullName" ) );
- If we check the collection, it has:
- Clearly, we can see, only 2 columns are retained and rest are removed. This is useful when we need to show a couple of columns from a large columns list.
- Drop Column (DropColumns)
- DropColumns is used to drop the columns from table/collection that needs not to be displayed. It can be say a opposite of ShowColumns.
-
ClearCollect( collWithOnlyFullName, DropColumns( collWithFullName, "ID", "FName", "LName" ) );
- If we check the collection, it has:
- Clearly, we can see, all 3 columns are dropped and rest only 1 column is retained. This is useful when we need to drop couple of columns from a large columns list and rest to be retained.
- Distinct (Distinct)
- Before moving forward to RenameColumns, we will discuss about Distinct function. After this, you will better understand the RenameColumns function. Distinct function is used to get the distinct values from a table/collection. Distinct works only on single column and return that particular column from the collection.
-
ClearCollect( collDistinctData, Distinct( collWithOnlyFullName, FullName ) );
- If we check the collection, it has:
- Rename Column (RenameColumns)
- RenameColumns is used to rename the name of existing column.
-
ClearCollect( collRenameColumnData, RenameColumns( collDistinctData, "Result", "Full Name" ) );
- If we check the collection, it has:
- As we can check the column name has been renamed from "Result" to "Full Name" while the data remains unchanged.
- Nested Use Of These Functions
- Now, once you are expertise in using functions, you may consolidate all these functions into one. I am using below function in one command to achieve the same functionality-
- AddColumns
- DropColumns
- RenameColumns
- Distinct
-
ClearCollect( collConsolidated, RenameColumns( Distinct( DropColumns( AddColumns( collSampleData, "FullName", FName & " " & LName ), "FName", "LName" ), FullName ), "Result", "Full Name" ) );
- If we check the collection, it has:
- Add Blank Record At Beginning/End of Collection
- We have consolidated data collection. We will add a blank record at the end of collection.
-
ClearCollect( collBlankAtEnd, collConsolidated, {'Full Name': Blank()} );
- For the purpose to display blank record, I had set border of labels used.
- Now, we will add a blank record at the beginning of collection.
-
ClearCollect( collBlankAtBegin, {'Full Name': Blank()}, collConsolidated );
- Add ID using Filter function in collection having Distinct data
- Now suppose a scenario, where you have a collection which is having multiple duplicate records. Your objective is to have Distinct records having ID and Name. The problem is that Distinct function returns only single column, so what the approach is?
- We will create a collection.
- Apply Distinct (to get distinct records) >> AddColumns (add ItemID column with default value as 0) >> RenameColumns (rename the column "Result" created as as result of Distinct function to ItemName).
- Create a replica of this distinct collection to apply ForAll loop.
- Apply ForAll loop. In this loop apply Patch. In Patch, use Filter with First function to get the desired ItemID of each item.
- Sequentially code is as below-
-
ClearCollect( collStationeryData, { ID: 1, ItemName: "Pencil" }, { ID: 2, ItemName: "Pen" }, { ID: 3, ItemName: "Eraser" }, { ID: 2, ItemName: "Pen" }, { ID: 4, ItemName: "Sharpner" }, { ID: 5, ItemName: "Paper A4" }, { ID: 1, ItemName: "Pencil" }, { ID: 3, ItemName: "Eraser" }, { ID: 6, ItemName: "Stapler" } );
-
ClearCollect( collDistinctStationeryData, RenameColumns( AddColumns( Distinct( collStationeryData, ItemName ), "ItemID", 0 ), "Result", "ItemName" ) );
-
ClearCollect( collDistinctStationeryDataTemp, collDistinctStationeryData );
-
ForAll( collDistinctStationeryDataTemp, Patch( collDistinctStationeryData, LookUp( collDistinctStationeryData, ItemName = collDistinctStationeryDataTemp[@ItemName] ), { ItemID: First( Filter( collStationeryData, ItemName = collDistinctStationeryDataTemp[@ItemName] ) ).ID } ) );
- If we check the collection, it has:
- collStationeryData
- collDistinctStationeryData (initial stage before Patch) / collDistinctStationeryDataTemp
- collDistinctStationeryData (After Patch)
- This way, you can utilize these power packed functions as per the required. Even you may nest these functions to avoid multiple steps.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.
Stay Safe !
Stay Healthy !
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.