0

I have a power app, I have listed my columns in the Item Section of a drop down:

enter image description here

enter image description here

How can I alter my Browse Gallery to Sort by Ascending/Descending based on my dropdown choice?

Current Browse Gallery Statement:

If(
    Toggle2.Value = false,
    SortByColumns(
        Filter(
            [@'Documentation Tracker'],
            Or(Lower(Requestor_two) = Lower(User().Email), Lower(Approver.Value) = Lower(User().Email)) && TextSearchBox1.Text in Title
        ),
        "Title",
        If(SortDescending1, Descending, Ascending)
    ),
    SortByColumns(
        Filter(
            [@'Documentation Tracker'],
            TextSearchBox1.Text in Title
        ),
        "Title",
        If(SortDescending1, Descending, Ascending)
    )
)

The kind of pseudo code I'm trying to come up with:

If Dropdown1.Value = "Documentation Type", Ascending, If Dropdown1.Value = "Requestor", Ascending", etc

Trying to put it into the code is very confusing to me.

What I'm trying:

If(
    Toggle2.Value = false Or Dropdown1.SelectedText.Value = "Approval Type",
    SortByColumns(
        Filter(
            [@'Documentation Tracker'],
            Or(Lower(Requestor_two) = Lower(User().Email), Lower(Approver.Value) = Lower(User().Email)) && TextSearchBox1.Text in Title
        ),
        "Title","DocType",
        If(SortDescending1, Descending, Ascending)
        ),
    SortByColumns(
        Filter(
            [@'Documentation Tracker'],
            TextSearchBox1.Text in Title
        ),
        "Title","DocType",
        If(SortDescending1, Descending, Ascending)
    )
)
3
  • Currently you are using SortDescending1 variable to sort the gallery. Do you want to continue using this variable along with dropdown selection to sort gallery or ignore this variable and just sort based on dropdown selection? Commented Aug 17, 2022 at 4:36
  • @GaneshSanap , Sorry should have clarified. I want to ignore the sort gallery and sory just based on dropdown selection.
    – Jonnyboi
    Commented Aug 17, 2022 at 13:00
  • Try using formula given in my answer below. Commented Aug 17, 2022 at 13:14

1 Answer 1

1

Use formula like below in items property of gallery control:

If(
    Toggle2.Value = false,
    SortByColumns(
        Filter(
            [@'Documentation Tracker'],
            Or(Lower(Requestor_two) = Lower(User().Email), Lower(Approver.Value) = Lower(User().Email)) && TextSearchBox1.Text in Title
        ),
        "Title",
        If(Dropdown1.Selected.Value = "Documentation Type" || Dropdown1.Selected.Value = "Requestor", Ascending, Descending)
    ),
    SortByColumns(
        Filter(
            [@'Documentation Tracker'],
            TextSearchBox1.Text in Title
        ),
        "Title",
        If(Dropdown1.Selected.Value = "Documentation Type" || Dropdown1.Selected.Value = "Requestor", Ascending, Descending)
    )
)

Documentation: If and Switch functions in Power Apps


Update from comments:

Use below formula to sort by column selected in dropdown:

If(
    Toggle2.Value = false,
    SortByColumns(
        Filter(
            [@'Documentation Tracker'],
            Or(Lower(Requestor_two) = Lower(User().Email), Lower(Approver.Value) = Lower(User().Email)) && TextSearchBox1.Text in Title
        ),
        Dropdown1.Selected.Value,
        Ascending
    ),
    SortByColumns(
        Filter(
            [@'Documentation Tracker'],
            TextSearchBox1.Text in Title
        ),
        Dropdown1.Selected.Value,
        Ascending
    )
)

Note: Internal name of column based on which sorting is applied & dropdown option should be exact same. Also, there maybe some warnings related to delegation while using this formula.


If you have different column names & dropdown options, you can use formula like:

If(
    Toggle2.Value = false,
    SortByColumns(
        Filter(
            [@'Documentation Tracker'],
            Or(Lower(Requestor_two) = Lower(User().Email), Lower(Approver.Value) = Lower(User().Email)) && TextSearchBox1.Text in Title
        ),
        If(Dropdown1.Selected.Value = "Documentation Type", "DocType", Dropdown1.Selected.Value = "Requestor", "Requestor_two", "ID"),
        Ascending
    ),
    SortByColumns(
        Filter(
            [@'Documentation Tracker'],
            TextSearchBox1.Text in Title
        ),
        If(Dropdown1.Selected.Value = "Documentation Type", "DocType", Dropdown1.Selected.Value = "Requestor", "Requestor_two", "ID"),
        Ascending
    )
)
12
  • Thanks! if I want to add more to the dropdown is it as simple as adding items and using || to separated? Also how will this sort if it is a Date based column?
    – Jonnyboi
    Commented Aug 17, 2022 at 13:23
  • Yes, you can add more conditions to same formula based on dropdown choices. Also, you might need to change the conditions if you want to sort based on date columns. Commented Aug 17, 2022 at 13:27
  • How does it work for requestor? My requestor fields are emails and should be ordered alphabetically? But it isn't sorting like this when I drop down to requestor. Or is it doing it descending when I drop down? I think its only sorting based on "Title" column, is that correct? I need it to sort on each column
    – Jonnyboi
    Commented Aug 17, 2022 at 13:45
  • Just want to clarify: When you select "Requestor" in dropdown, do you want to sort gallery based on "Title" column or "Requestor" column? Also, when you select "Documentation Type" in dropdown, do you want to sort gallery based on "Title" column or any other column? Commented Aug 17, 2022 at 13:49
  • Sorry. When Requestor is selected, sort on Requestor. When Documentation Type is selected, Sort on Documentation Type and so on.
    – Jonnyboi
    Commented Aug 17, 2022 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.