Forum Discussion
Sort Function (SortBy) - Trying to get empty cells to return empty cells using the sort function
This is just and aesthetic issue.
I have a table with around 7 headings for a simple account information table. Not all accounts have a value for each heading/field so on entering data those cells are left blank. To make it easier to find each row of account information I have used the SORT function so I can continue to add new accounts on the original table as I think of them and they are displayed alphabetically on a different sheet as the table is updated.
The displayed results from the SORT function return "0" zero in every field which is blank in the original table and as the Date column is formatted to display a date it returns 00/01/1900. I would like it to return blank cells from blank cells.
Is there anyway of getting the SORT function to return a blank/empty cell or it simply a result of the entries being processed as data?
10 Replies
- wkraslCopper Contributor
Kudos to JShipway for the only reply above I actually understood and was able to apply as:
"=IF(ISBLANK(SORTBY(Schedule!B1:J240,Schedule!J1:J240,-1)),"",SORTBY(Schedule!B1:J240,Schedule!J1:J240,-1))"
My previous best effort hid the date-time column in the sorted result array and added a helper column that replaced zero date-times with blanks. Duh!
- PeterBartholomew1Silver Contributor
A similar solution,
= LET( sorted, SORT(array), IF(sorted<>"", sorted, ""))
This is clear to read and does not require the SORT to be performed twice.
- bryanbryankarlancomCopper ContributorWhat if you are sorting 2 columns, A & B and you wanted to apply the blank test only to A? For example, if A contained a Date and B contained an Item related to that Date, and if you didn't want neither A nor B to appear in the sort if A was blank, even if B had an item in it?
- PeterBartholomew1Silver Contributor
What you describe would appear to need FILTER as well as SORT.
= LET( filtered, FILTER(table, Date), sorted, SORT(filtered), IF(sorted<>"",sorted,""))
In response to your earlier comment on LET, I find that the changes in modern Excel require thought processes that are utterly different from those of the traditional spreadsheet. So much so, that I frequently find it difficult to construct answers that will be of value to the OP.
In the present case, were you to want to apply the same filtering process to many tables, you might pass the table names to the formula by using a Lambda function
= LAMBDA(array, LET( column1, INDEX(array,,1), filtered, FILTER(array, column1), sorted, SORT(filtered), IF(sorted<>"",sorted,"") ) )(table)
That looks intimidating, but if one uses a defined name such as 'ValueScheduledλ' to refer to the formula then many data tables could be filtered and sorted with no more than
= ValueScheduledλ(table1) = ValueScheduledλ(table2)
- bryanbryankarlancomCopper Contributor
Even better. I was not familiar with the Let function and I like the ability to assign a variable or a computation to a name.
- JShipwayCopper ContributorHi there,
I believe this can be solved with a simple if statement - I just ran into the same issue and had to solve it.
=if(isblank(Sort(*your sort parameters*)), "", Sort(*your same sort parameters*))
Hopefully that helps you 🙂- bryanbryankarlancomCopper Contributor
Thank you, this worked perfectly.
- Zack BarresseIron ContributorPerhaps you want to look at the FILTER function instead.
An example of filtering a Table by 'Col1' having blanks and returning a sort of the entire Table:
=SORT(FILTER(Table1,Table1[Col1]<>""))
An example of filtering a Table by 'Col1' and 'Col2' having blanks and returning a sort of the entire Table:
=SORT(FILTER(Table1,(Table1[Col1]<>"")*(Table1[Col2]<>"")))
The FILTER 'include' parameter can take an array of boolean returns, for example:
(CriteriaRange1=Criteria1)*(CriteriaRange2=Criteria2)*(CriteriaRange3=Criteria3)