Using collection and lookup in PowerApps

447 views Asked by At

Attempting to better my coding ability here as I have used ineffective code that made sense to me instead of efficient code that I couldn't make sense of. I'm doing this retrospectively and I've deleted the original code but I'll try to recreate generic example.

Essentially, in PowerApps I have 11 drop downs, each option of each drop down has a value associated with it. Initially, I created a collection with two columns, one for Text: and Value:

ClearCollect(DropDownValues, 
    { Text: "Option 1", Value: 10 },
    { Text: "Option 2", Value: 20 },
    { Text: "Option 3", Value: 30 },
    { Text: "Option 4", Value: 40 },
    { Text: "Option 5", Value: 50 }
)

I then tried to create an effective calculation formula using Lookup:

Set(TotalValue =
    Lookup(DropDownValues, Text = DropDown1.Selected.Value) +
    Lookup(DropDownValues, Text = DropDown2.Selected.Value) +
    Lookup(DropDownValues, Text = DropDown3.Selected.Value) 
)

I wanted to then use a switch statement to result low, medium, high, based on the TotalValue variable.

I successfully created the collection, but I could not get the Lookup calculation to work, I think it had something to do with collection returning part of calculation as a table? In any case. I tried to use SUM(Filter( to sum up each element of text converted to value, then add them up and it did not work.

Set(TotalValue, 
    Sum(Filter(DropDownValues, Text = Dropdown1.Selected.Value), Value) +
    Sum(Filter(DropDownValues, Text = Dropdown2.Selected.Value), Value) +
    Sum(Filter(DropDownValues, Text = Dropdown3.Selected.Value), Value)
)

So instead, for each drop-down object OnChange I created a variable based on a switch statement that gave the variable the correct value based on the option chosen in the drop down. I then created the TotalValue variable separate and added up each Variable from each dropdown. I've not tested the app yet, but the code is not immediately error as it was with the collection. I repeated this for 10 different TotalValue Variables (TotalValue1-TotalValue10) for 10 sets of 11 drop downs.

I'm very new to Power Apps, with limited experience in Python. I've admittedly been looking at this too much with now squared eyes so I imagine I'm missing something simple, but let me know your thoughts, reflections, and ideas.

1

There are 1 answers

0
Sam Nseir On

You don't need to do a look-up as the drop down will give you the selected item which has both Text and Value.

Set(TotalValue = Dropdown1.Selected.Value + Dropdown2.Selected.Value)

And for closure on the LookUp:

Set(TotalValue =
  Lookup(DropDownValues, Text = DropDown1.Selected.Text).Value +
  Lookup(DropDownValues, Text = DropDown2.Selected.Text).Value +
  Lookup(DropDownValues, Text = DropDown3.Selected.Text).Value 
)

Without knowing more about your app - consider if the Gallery control can be used for repeating your drop downs - this will offer improved performance (as a Gallery is considered as one control) and can help with your code logic. Example: TotalValue = Sum(myGallery.AllItems, mgDropdown.Selected.Value)