In Power Apps, ThisItem is a keyword that refers to the current item or record in a gallery, form, or other data-bound control. It's used to access and display properties of the current record within the context of the control.
PowerApps – Simple use of ThisItem function
When you bind a gallery to a data source, you can use ThisItem to refer to fields of the current record being displayed.
- Set up your data source:
ClearCollect(Products,
Table(
{ProductID: 1, ProductName: "Laptop", Price: 1000},
{ProductID: 2, ProductName: "Tablet", Price: 500},
{ProductID: 3, ProductName: "Monitor", Price: 300}
)
);
- Add a Gallery control and set its Items property:
- Add Labels to display product information:
- Set the Text property of one Label to:
- Set the Text property of another Label to:
This setup displays the product name and price for each item in the Products collection within the gallery.
ThisItem is useful in forms to refer to the current record being edited or displayed.
- Set up your data source (e.g., a SharePoint list or a collection):
ClearCollect(Employees,
Table(
{EmployeeID: 1, Name: "John Doe", Department: "IT"},
{EmployeeID: 2, Name: "Jane Smith", Department: "HR"}
)
);
- Add an Edit Form control and set its DataSource property:
- Add a Label inside the form and set its Text property:
This displays the name of the employee currently being edited or viewed in the form.
You can use ThisItem to apply conditional formatting to items in a gallery based on specific criteria.
- Set up your data source:
ClearCollect(Tasks,
Table(
{TaskID: 1, TaskName: "Task 1", DueDate: Today()-1},
{TaskID: 2, TaskName: "Task 2", DueDate: Today()+2}
)
);
- Add a Gallery control and set its Items property:
- Add a Label to display task names and set its Text property:
- Set the Color property of the Label to apply conditional formatting:
If(ThisItem.DueDate < Today(), Red, Black)
This changes the text color to red for tasks that are overdue.
PowerApps – Access Nested Records using ThisItem function
If your data source contains nested records, you can use ThisItem to access fields within those nested records.
- Set up your data source:
ClearCollect(Departments,
Table(
{Department: "IT", Employees: Table({Name: "John Doe"}, {Name: "Jane Doe"})},
{Department: "HR", Employees: Table({Name: "Jim Brown"}, {Name: "Jake White"})}
)
);
- Add a Gallery control and set its Items property:
- Add a nested Gallery inside the first Gallery to display employees:
- Set the Items property of the nested Gallery to:
- Add a Label inside the nested Gallery to display employee names and set its Text property:
This setup displays departments in the outer gallery and employees within each department in the nested gallery.
PowerApps – Set variable Using ThisItem function
You can use ThisItem in actions like OnSelect to perform operations based on the current item.
- Set up your data source:
ClearCollect(Products,
Table(
{ProductID: 1, ProductName: "Laptop", Price: 1000},
{ProductID: 2, ProductName: "Tablet", Price: 500}
)
);
- Add a Gallery control and set its Items property:
- Add a Button to each item in the gallery and set its OnSelect property:
Set(SelectedProduct, ThisItem)
- Display the selected product details using Labels:
- Set the Text property of one Label to:
SelectedProduct.ProductName
- Set the Text property of another Label to:
"$" & SelectedProduct.Price
This sets the selected product to a variable and displays its details outside the gallery.
PowerApps - Use ThisItem with LookUp and Patch Functions
You can use ThisItem in combination with functions like LookUp and Patch to update records based on the current item.
- Set up your data source (e.g., a SharePoint list or a collection):
ClearCollect(Inventory,
Table(
{ItemID: 1, ItemName: "Laptop", Stock: 10},
{ItemID: 2, ItemName: "Tablet", Stock: 5}
)
);
- Add a Gallery control and set its Items property:
- Add a Button to each item in the gallery to restock items and set its OnSelect property:
Patch(Inventory, LookUp(Inventory, ItemID = ThisItem.ItemID), {Stock: ThisItem.Stock + 1})
This button will increase the stock count of the selected item by 1.
PowerApps - Use ThisItem with Dynamic Filtering and Sorting
You can use ThisItem to implement dynamic filtering and sorting within galleries based on user input.
- Set up your data source:
ClearCollect(Employees,
Table(
{EmployeeID: 1, Name: "John Doe", Department: "IT", Age: 30},
{EmployeeID: 2, Name: "Jane Smith", Department: "HR", Age: 25},
{EmployeeID: 3, Name: "Jim Brown", Department: "Finance", Age: 40}
)
);
- Add a Dropdown control (DepartmentDropdown) to filter by department.
Items: Distinct(Employees, Department)
- Add a Gallery control and set its Items property:
SortByColumns(
Filter(Employees,
DepartmentDropdown.Selected.Result = "All" ||
DepartmentDropdown.Selected.Result = ThisItem.Department
),
"Age",
Ascending
)
This will filter the gallery to display employees based on the selected department and sort them by age.