This is a sophisticated bug. Minor since I have found a work around.
If you use the If function in the argument for Sort, it cannot understand if you have a mix of text and values in your columns.
For instance, in the example below, I am trying to sort the Datasource by datetimes, values, and texts, depending on what is set in the variable "SortBy." In the way that it is currently written, the Sort will work correctly for datetimes and values, but will not sort text from the Name column. This was puzzling to me since sorting names should be commonplace.
Sort( Datasource, If(SortBy="Recent",CreatedOnDateTime, SortBy="Favorite",Favorite, SortBy="Name",Name, SortBy="HP",Value(HPnow), SortBy="Power",Max(Attack,SpAttack) ) )
However, when I rewrite the formula with "Name" as the first condition in the If, then the columns of text will work, but few of the values will.
Sort( Datasource, If(SortBy="Name",Name, SortBy="Favorite",Favorite, SortBy="Recent",CreatedOnDateTime, SortBy="HP",Value(HPnow), SortBy="Power",Max(Attack,SpAttack) ) )
I have not gone too deep in the testing, but I think the problem is that Sort() will rely on the first condition of an If formula to figure out whether to look for boolean, text, or value when sorting.
I worked around this by making the If wrap around the Sort instead. So if I have specified SortBy to sort by Name, it will do it separately. If the SortBy variable is any of the other options, then it will continue to use the original scheme with the If inside the Sort. I think this may be less efficient though.
If(SortBy="Name", Sort(Datasource,Name),
Sort( Datasource, If(SortBy="Recent",CreatedOnDateTime, SortBy="Favorite",Favorite, SortBy="HP",Value(HPnow), SortBy="Power",Max(Attack,SpAttack) ) )
)
I chose not to use SortByColumns because that requires names of columns wrapped in quotations. This prevents you from performing LookUps and aggregate functions on the columns for sorting.

Report
All responses (
Answers (