Introduction
Formulas in Power Apps frequently include expressions that are utilised multiple times in the same logic. The formula may become longer, more difficult to read, and more challenging to maintain if the same expression is used repeatedly. Repetition can also make it harder to troubleshoot and update formulas as they become more complex. The With() function in Power Apps makes formulas easier to understand and makes them simpler to write. You can reuse a value stored in a temporary variable using this function within the same formula. You can define an expression once and refer to it multiple times rather than writing it out repeatedly. When working with Lookup(), Filter(), or other calculations whose results must be used in multiple places in a formula, the With() function is especially helpful. The formula becomes more streamlined and simpler to comprehend by storing the result in a variable. When creating scalable Power Apps solutions, using With() is a good practice because it helps create formulas that are more structured, readable, and easy to maintain.
Traditional Approach (Repetitive Code)
"Note added " & If(
DateDiff(
ThisItem.Created,
Now(),
TimeUnit.Minutes
) < 60,
Text(
DateDiff(
ThisItem.Created,
Now(),
TimeUnit.Minutes
)
) & " minute(s) ago (",
DateDiff(
ThisItem.Created,
Now(),
TimeUnit.Minutes
) < 1440,
Text(
DateDiff(
ThisItem.Created,
Now(),
TimeUnit.Hours
)
) & " hour(s) ago (",
Text(
DateDiff(
ThisItem.Created,
Now(),
TimeUnit.Days
)
) & " day(s) ago ("
) & Text(
ThisItem.Created,
"ddd, d mmm h:mm AM/PM"
) & ") (IST) "
Issues with this approach
ThisItem.Created is repeated several times
Code becomes less readable
Harder to maintain if changes are required later
Optimised Approach Using With()
Using the With() function, we can store the value in a temporary variable rather than repeating the same expression.
"Note added " & With(
{createdDate: ThisItem.Created}, // One-time call, we can use any location.
DateDiff(
createdDate,
Now(),
TimeUnit.Minutes
) < 60,
Text(
DateDiff(
createdDate,
Now(),
TimeUnit.Minutes
)
) & " minute(s) ago (",
DateDiff(
createdDate,
Now(),
TimeUnit.Minutes
) < 1440,
Text(
DateDiff(
createdDate,
Now(),
TimeUnit.Hours
)
) & " hour(s) ago (",
Text(
DateDiff(
createdDate,
Now(),
TimeUnit.Days
)
) & " day(s) ago ("
) & Text(
createdDate,
"ddd, d mmm yyyy h:mm AM/PM"
)
) & ") (IST) "
// Another Optimsed version
"Note added " & With(
{
createdDate: ThisItem.Created,
minutesAgo: DateDiff(ThisItem.Created, Now(), TimeUnit.Minutes)
},
If(
minutesAgo < 60,
Text(minutesAgo) & " minute(s) ago (",
minutesAgo < 1440,
Text(minutesAgo / 60, "[$-en-US]0") & " hour(s) ago (",
Text(minutesAgo / 1440, "[$-en-US]0") & " day(s) ago ("
) & Text(createdDate, "ddd, d mmm yyyy h:mm AM/PM")
) & ") (IST)"
Benefits of Using With()
Using the With() function provides several advantages:
Improves code readability
Avoids repeating the same expression multiple times
Makes formulas cleaner and easier to maintain
Helps in optimising complex formulas
Conclusion
Formula optimisation is an essential component of developing Power Apps solutions that are both effective and easy to maintain.
In a formula, repeating the same expression can make the code longer and more difficult to comprehend. Using the With() function to refactor such formulas makes logic simpler by storing repeated values in a temporary variable.
Formulas that are well-structured, cleaner, and easier to read can be written by developers by utilising With().
It also makes future updates simpler because the value only needs to be changed in one place in the formula instead of multiple places. In Power Apps applications, adopting this approach as a best practice can significantly enhance code quality, maintainability, and overall readability.