powerappspowerapps-formulapowerapps-collection

How do I use collect and if functions together in Powerapps?


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.


Solution

  • 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:

    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.