1
votes

Below is the sample canvas-app functions that I have tried, however I would like to convert the below canvas-app functions that can access collection data to use it:

If("EC - Empire Complex" in BuildingDropdown.Selected.Value, Distinct(Filter(Area, "1" in buildingID), storey), If("BTB - Brani Terminal Building" in BuildingDropdown.Selected.Value, Distinct(Filter(Area, "2" in buildingID), storey), If("KW - Keppel Workshop" in BuildingDropdown.Selected.Value, Distinct(Filter(Area, "3" in buildingID),storey), If("CSO - Container Side Office"in BuildingDropdown.Selected.Value, Distinct(Filter(Area, "4" in buildingID), storey), If("Others" in BuildingDropdown.Selected.Value, Distinct(Filter(Area, "5" in buildingID), storey))))))

How do I convert the above canvas-app functions such that it can make use of the collect function together with the use of if function? Thanks.

3

3 Answers

2
votes

Its unclear where you want to use the PowerApps Collect function. Please elaborate.

Its also unclear what "Area" is. Is it a Collection or an Excel table or a Sharepoint list or a SQL table?

Choose a naming convention in your PowerApps code and consistently use it.

Example:

  • Prefix all Collections in your code with col
    • colArea
  • Make all data tables (Sharepoint, SQL, etc.) ALL CAPITALS_WITH_UNDERSCORES
    • MY_SHAREPOINT_LIST

Then, at-a-glance, you can tell what type of data source it is.

You can also simplify your code by removing the nested If's:

If(
    "EC - Empire Complex" in BuildingDropdown.Selected.Value, 
        Distinct(
            Filter(Area, "1" in buildingID), 
            storey
        ), 
    "BTB - Brani Terminal Building" in BuildingDropdown.Selected.Value, 
        Distinct(
            Filter(Area, "2" in buildingID), 
            storey
        ), 
    "KW - Keppel Workshop" in BuildingDropdown.Selected.Value, 
        Distinct(
            Filter(Area, "3" in buildingID),
            storey
        ), 
    "CSO - Container Side Office"in BuildingDropdown.Selected.Value, 
        Distinct(
            Filter(Area, "4" in buildingID), 
            storey
        ), 
    "Others" in BuildingDropdown.Selected.Value, 
        Distinct(
            Filter(
                Area, "5" in buildingID), 
            storey
        )
)

Keep Switch() in mind too (though it wouldn't do much good here).

Typically use nested If's if there is branching logic, but not for multiple choice.

1
votes

Well, If in Powerapps has separate syntax to go with, i.e. if (condition = true, "do this", "else do this"). If you want multiple actions to happen inside if, then your syntax becomes, if(condition = true, "do this";"do this also";"do this also", "else do this";"else do this also").

You can have collect function in if quite easily.

0
votes

Here is a working - hopefully self-explaining - example for selecting and deselecting a record in a power app collection=>

If( LookUp(Sammlung3; ID=ThisItem.ID).ID >0; 
          RemoveIf(Sammlung3; ID=ThisItem.ID); 
    Collect(Sammlung3;ThisItem)
  )

if record is found in collection then remove it, otherwise put it in the collection