Introduction - lookup inside a gallery
A gallery is one of the most frequently used controls in Power Apps. Many times, there is a need to perform a lookup inside a gallery. Here is the use case we will use for example:
- There is a gallery of users (from the Users table in Dataverse)
- It has a lookup column called Language which looks up to the User Languages table
- There is a label inside the gallery that displays the language of each user
- The color of the label text should be green if the language of a user is English, else it should be red
One way to achieve this is to set the Color property of the label to:
If(
ThisItem.Language.'Language Code' = LookUp(
'User Languages',
'Language Code' = "EN"
).'Language Code',
Color.Green,
Color.Red
)
Optimized lookup inside a gallery
A more optimized way to achieve this is to store the User Language record where Language Code = EN in a variable. This could be done in the app's OnStart, or the OnVisible of the screen. The code to set up the variable will be:
Set(
gblUserLanguage,
LookUp(
'User Languages',
'Language Name' = "English"
)
);
And then use the variable to set the Color property of the label to:
If(
ThisItem.Language.'Language Code' = gblUserLanguage.'Language Code',
Color.Green,
Color.Red
)
Here is an image of the monitor tool showing the difference in number of data calls for these 2 approaches:
Gallery1 uses the 1st approach. Gallery1_1 uses the 2nd approach. Subtitle1 is the label in the 1st approach which has the color property as specified above.
If we look at the details of the line for Subtitle1 in the above screenshot of the monitor tool, it shows that it is the Lookup that is causing these calls. The number of data calls will be more as there are more records in the gallery.
It is better to avoid making data calls like a Lookup inside a gallery because otherwise it runs once for every single record in the gallery.
1 thought on “How to optimize data calls inside a gallery”