web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Use a toggle to change...
Power Apps
Answered

Use a toggle to change values in a collection by looking up data in another collection

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I have an app that has a collection of metrics (each with a unique ID) that is displayed to the user in an editable grid (gallery).

The user enters the amount for each metric and then can select the Unit of Measure (i.e. gallons or liters) from a dropdown field for each metric. When the Amount or UOM value is is changed the collection is updated with the new values for amount, UOM and also a conversion factor that is not displayed but used in a calculation displayed in a label. That works well for each line item in the gallery. 

 

I was asked to supply a way to toggle between Metric and Imperial UOM for all the items to make filling out the form easier and am trying to build a toggle to do that and can't figure out how to make it work.  The code in the OnChange property of the toggle looks like this:

If(tgl_UOM_.Value = false,Set(vbl_UoM_toggle,"Metric"),Set(vbl_UoM_toggle,"Imperial"));

UpdateIf(
myMetrics,
true,
{
Units: LookUp(myUoM,ID = ThisRecord.ID && SystemofMeasure = vbl_UoM_toggle,Units),
ConFactor: LookUp(myUoM,ID = ThisRecord.ID && SystemofMeasure = vbl_UoM_toggle,ConFactor)
}
)


The first part assigns a value to a variable that indicates if the user selected Metric or Imperial and the next part attempts to update the Units and ConvFactor columns in the Metric collection by looking up the unique ID for that row and finding the row in the Unit of Measure collection that has that ID and the System of Measure selected by the toggle.  

The problem seems to be that the value that is being used for the ID doesn't change from record to record. The same value is used for all records and the fields are all changed to the same value. I'm trying to figure out how to get the unique ID value for each row and use it in the lookup and can't seem to find an easy way to do that. It seems like this should be fairly straight-forward but I haven't found anything online that talks about it. Any advice would be appreciated.

Categories:
I have the same question (0)
  • Verified answer
    v-chenzhi Profile Picture
    Microsoft Employee on at

    Hi @Anonymous ,

     

    Do you have two collections named myMetrics and myUoM?

    Does myUoM store the same ID value in metric and imperial systems? In myMetrics, different statuses are displayed according to the Toggle?

    I have a test for your reference, please try the following workaround.

    I created a collection named myUoM, two records with ID:1 have different SystemofMeasure.

    ClearCollect(
     myUoM,
     {
     ID1: 1,
     SystemofMeasure1: "Metric",
     Units1: "A1",
     Confactor1: "A2"
     },
     {
     ID1: 1,
     SystemofMeasure1: "Imperial",
     Units1: "B1",
     Confactor1: "B2"
     },
     {
     ID1: 2,
     SystemofMeasure1: "Metric",
     Units1: "C1",
     Confactor1: "C2"
     },
     {
     ID1: 2,
     SystemofMeasure1: "Imperial",
     Units1: "D1",
     Confactor1: "D2"
     }
    )

    vchenzhi_0-1636618443610.png

     

    I created a collection named myMetrics have the Similar columns,

    ClearCollect(
     myMetrics,
     {
     ID2: 1,
     SystemofMeasure2: "",
     Units2: "",
     Confactor2: ""
     },
     {
     ID2: 2,
     SystemofMeasure2: "",
     Units2: "",
     Confactor2: ""
     }
    )

    vchenzhi_1-1636618443611.png

     

    Set the Items of DataTable to following:

    ForAll(
     myMetrics,
     {
     ID2: ID2,
     Units2: LookUp(
     myUoM,
     ID1 = ID2 && SystemofMeasure1 = vbl_UoM_toggle,
     Units1
     ),
     Confactor2: LookUp(
     myUoM,
     ID1 = ID2 && SystemofMeasure1 = vbl_UoM_toggle,
     Confactor1
     ),
     SystemofMeasure2: vbl_UoM_toggle
     }
    )

    vchenzhi_2-1636618443612.jpeg

     

    When the toggle change.

    vchenzhi_3-1636618443613.jpeg

     

     

    Best Regards,

    Zhi Chen

  • Verified answer
    Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Thanks Zhi Chen,

    I actually resolved this somewhat similarly but I used an UpdateIf statement and created a separate collection for the ForAll to parse through:

    ForAll(

        RenameColumns( myIDlist, "ID", "IDtmp"  ), //So the values are not the same. You used ID1, ID2

        UpdateIf(

            myMetrics,

            ID = IDtemp,

            {

                Units: LookUp(

                    myUoM,

                    ID = IDtmp && (System = vbl_UoM_toggle || IsBlank(System)),

                    Units

                ),

                Confactor: LookUp(

                    myUoM,

                    ID = IDtmp && (System = vbl_UoM_toggle || IsBlank(System)),

                    Confactor

                )

            }

        )

    )

    Your solution seems a little more efficient and aligned with how I would have expected the syntax to look but I could not figure out how to do it. I think I will see if I can change mine to use the syntax yours does as currently the For All takes a few seconds to run on 126 records. Perhaps the more condensed code will run slightly faster.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 536

#2
WarrenBelz Profile Picture

WarrenBelz 426 Most Valuable Professional

#3
Haque Profile Picture

Haque 305

Last 30 days Overall leaderboard