How to dynamically select properties in Power Apps

In Power Apps there is sometimes a need to select a property dynamically. You get the name of the property from ,for example, a dropdown, and now you want to retrieve the value of that property in a record.

JSON Objects and Properties

When we look at objects or records in Power Apps, then we will find that JSON formatted data is important.

JSON is all about arrays, objects and properties. Now a typical definition of a JSON object could look like this:

{
   Fruit: "Banana",
   Colour: "Yellow"
}

And we can create an array like this:

[
 {
   Fruit: "Banana",
   Colour: "Yellow"
 },
 {
   Fruit: "Apple",
   Colour: "Red"
 }
]

All easy stuff so far. And if we want to select and object in an array we can use the following syntax with square brackets.

Array[1]

In a similar way we can select a property in an object

Object.Property1

In the last example we are hard coding the property name. Now in most situations in Power Apps you will find that hard coding the properties is fine. But sometimes we want to use a value that was selected in for example dropdown to select a specific property.

An example for selecting a property dynamically

I’ve created the following code in my app on start. But this data could of course come from any data source:

Set(varObject, {
       Property1: "Value 1",
       Property2: "Value 2"
    }
);

Then I created a dropdown which using the following expression:

ColumnNames(ParseJSON(JSON(varObject)))
Create a dropdown with all columns from an object
Create a dropdown with all columns from an object

The ColumnNames function will list all the column names in a untyped object. Therefor ColumnNames(varObject) would give an error that an untyped object is expected. The easy way around this is to convert our object to JSON text using the JSON function and then Parse the generated JSON using the ParseJSON function. Now we have an untyped object.

Dropdown with property names
Dropdown with property names

With the above dropdown we can now select the property that we want to display in the app.

First of all we need to use the alue in the Dropdown control. Then the Column function to uses this column value (which is the text “Property1”) to then pick up the property varObject.Property1

dynamically select properties
dynamically select properties

FAQs

Is dynamically selecting properties always better?

Most of the time hardcoding the properties in Power Apps is fine as any changes in the data model are likely to require changes in the app anyway. However when the app needs to respond on different ways depending on a user’s choices dynamically selecting properties can be useful.

Could I use a flow in Power Automate instead to implement dynamic content?

Yes, you could use a flow as a workaround however it would make the app more complicated.


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.