In Power Apps, ThisRecord is similar to ThisItem but is used within the context of functions like ForAll and With, where you're working with records in a broader context than just galleries or forms. ThisRecord allows you to reference the current record being processed within these functions, making it easier to work with and manipulate data within iterative operations.
PowerApps - Use ThisRecord in ForAll
ForAll is a function that evaluates a formula for all records of a table. ThisRecord can be used to reference the current record within the loop.
- Set up your collection:
ClearCollect(Products,
Table(
{ProductID: 1, ProductName: "Laptop", Price: 1000},
{ProductID: 2, ProductName: "Tablet", Price: 500},
{ProductID: 3, ProductName: "Monitor", Price: 300}
)
);
- Use ForAll to create a new collection with discounted prices:
ClearCollect(DiscountedProducts,
ForAll(Products,
{
ProductID: ThisRecord.ProductID,
ProductName: ThisRecord.ProductName,
DiscountedPrice: ThisRecord.Price * 0.9
}
)
);
This creates a new collection, DiscountedProducts, where each product's price is discounted by 10%.
PowerApps – Use ThisRecord in With function
With allows you to evaluate an expression in the context of a record and can be useful for simplifying complex formulas.
- Set up your collection:
ClearCollect(Employees,
Table(
{EmployeeID: 1, Name: "John Doe", Department: "IT", Salary: 70000},
{EmployeeID: 2, Name: "Jane Smith", Department: "HR", Salary: 65000}
)
);
- Use With to calculate a bonus and display employee details:
With(
{CurrentEmployee: LookUp(Employees, EmployeeID = 1)},
Concatenate(
"Employee: ", CurrentEmployee.Name,
", Department: ", CurrentEmployee.Department,
", Total Compensation: $", Text(CurrentEmployee.Salary * 1.1, "[$-en-US]#,##0.00")
)
)
This calculates a 10% bonus for the employee with EmployeeID 1 and concatenates the details into a single string for display.
PowerApps – Use ThisRecord with Collect function
You can use ThisRecord to create or update collections by referencing fields from the current record being processed.
- Set up your collection:
ClearCollect(Tasks,
Table(
{TaskID: 1, TaskName: "Task 1", Completed: false},
{TaskID: 2, TaskName: "Task 2", Completed: true}
)
);
- Use ForAll and ThisRecord to create a summary collection:
ClearCollect(TaskSummary,
ForAll(Tasks,
{
TaskID: ThisRecord.TaskID,
TaskName: ThisRecord.TaskName,
Status: If(ThisRecord.Completed, "Completed", "Pending")
}
)
);
This creates a TaskSummary collection that includes a status field based on the completion status of each task.
PowerApps - Conditional Logic with ThisRecord function
You can use ThisRecord to apply conditional logic within iterative functions.
- Set up your collection:
ClearCollect(Products,
Table(
{ProductID: 1, ProductName: "Laptop", Stock: 5},
{ProductID: 2, ProductName: "Tablet", Stock: 0},
{ProductID: 3, ProductName: "Monitor", Stock: 10}
)
);
- Use ForAll and ThisRecord to check stock availability:
ClearCollect(StockCheck,
ForAll(Products,
{
ProductID: ThisRecord.ProductID,
ProductName: ThisRecord.ProductName,
Availability: If(ThisRecord.Stock > 0, "In Stock", "Out of Stock")
}
)
);
This creates a StockCheck collection that includes an availability status based on the stock level of each product.
PowerApps - Aggregate Data with ThisRecord function
You can use ThisRecord to aggregate data across records, such as calculating totals or averages.
- Set up your collection:
ClearCollect(Sales,
Table(
{SaleID: 1, Product: "Laptop", Amount: 1000},
{SaleID: 2, Product: "Tablet", Amount: 500},
{SaleID: 3, Product: "Monitor", Amount: 300}
)
);
- Calculate the total sales amount:
Sum(Sales, ThisRecord.Amount)
This calculates the total sales amount from the Sales collection.
PowerApps - Combine ThisRecord with Nested Collections
You can use ThisRecord to work with nested collections, accessing and manipulating data within nested structures.
- Set up your collection with nested records:
ClearCollect(Departments,
Table(
{Department: "IT", Employees: Table({Name: "John Doe", Age: 30}, {Name: "Jane Doe", Age: 25})},
{Department: "HR", Employees: Table({Name: "Jim Brown", Age: 40}, {Name: "Jake White", Age: 35})}
)
);
- Use ForAll to create a flat collection of employees:
ClearCollect(AllEmployees,
ForAll(Departments,
ForAll(ThisRecord.Employees,
{
Department: ThisRecord.Department,
Name: ThisRecord.Name,
Age: ThisRecord.Age
}
)
)
);
This flattens the nested structure into a single collection containing all employees with their respective departments.