Search code examples
powerappspowerapps-formula

Power APP, patch column based on choice made from dropdown


I'm making this work by using ifs, but I wonder if I can use a variable, here is what I have:

ForAll(
  selected_people,
  If(
    Dropdown1.SelectedText.Value = "Trainning 1",
    Patch(SampleExcelData,LookUp(SampleExcelData,ID = selected_people[@ID]),{Trainning1: "Yes"}),
    If(
     Dropdown1.SelectedText.Value = "Trainning 2",
     Patch(SampleExcelData,LookUp (SampleExcelData,ID = selected_people[@ID]),{Trainning2: "Yes"}),
     If(
       Dropdown1.SelectedText.Value = "Trainning 3",
       Patch(SampleExcelData, LookUp( SampleExcelData,ID = selected_people[@ID]),{Trainning3: "Yes"}),
       ....

I get this result:

ID Person Last Name Trainning 1 Trainning 2 Trainning 3
001 joe xxx yes yes
002 jill rrr yes yes
002 lilly bbb yes yes

but I wonder if I could save the dropdown selected in a variable and do something like this:

ForAll(
  selected_people,
  Patch(
    SampleExcelData,
    LookUp(SampleExcelData,ID = selected_people[@ID]),
    {*dropdown.selected(My column name) or variable*: "Yes"}))

Thanks, because I sometimes have 15 courses being recorded and 15 ifs is a lot.


Solution

  • You cannot use a variable (or a reference to a control property value) as the column name in an expression - Power Fx expressions need to be strongly-typed. You can make your expression a little simpler by using a Switch statement instead of multiple If calls:

    ForAll(
      selected_people,
      Switch(
        Dropdown1.SelectedText.Value,
        "Trainning 1",
          Patch(SampleExcelData,LookUp(SampleExcelData,ID = selected_people[@ID]),{Trainning1: "Yes"}),
        "Trainning 2",
          Patch(SampleExcelData,LookUp(SampleExcelData,ID = selected_people[@ID]),{Trainning2: "Yes"}),
        "Trainning 3",
          Patch(SampleExcelData,LookUp(SampleExcelData,ID = selected_people[@ID]),{Trainning3: "Yes"}),
           ....
    

    This may work as well, but I haven't tried it:

    ForAll(
      selected_people,
      With(
        { recordToUpdate: LookUp(SampleExcelData,ID = selected_people[@ID]) },
        Switch(
          Dropdown1.SelectedText.Value,
          "Trainning 1",
            Patch(SampleExcelData, recordToUpdate, { Trainning1: "Yes" }),
          "Trainning 2",
            Patch(SampleExcelData, recordToUpdate, { Trainning2: "Yes" }),
          "Trainning 3",
            Patch(SampleExcelData, recordToUpdate, { Trainning3: "Yes" }),
          ...
        )
      )
    )