SORT by months using functions

Today, we want to learn about sort­ing by months with the SORT function. 

SORT by months using functions

Let’s start with writ­ing the SORT func­tion, with an array as the first argu­ment. In the sec­ond argu­ment we need to write 2, as we want to sort by months, which are locat­ed in the sec­ond col­umn. Now, we can close the func­tion (Fig. 1). 

=SORT(A2:C145,2)

SORT function
Fig. 1 SORT function

Just like that the SORT func­tion sort­ed data by the Month col­umn. It’s impor­tant that the SORT func­tion does­n’t con­sid­er the cus­tom list. Excel does it, but the SORT func­tion does­n’t. Now. let’s have a look at how we can prop­er­ly sort by months using the SORT func­tion. First of all, we can­not use the SORT func­tion, but the SORTBY func­tion. In this func­tion, even if we change the sec­ond argu­ment to a month col­umn, we still get an ascend­ing sort­ing, from A to Z. It means that the sort­ing is based on the alpha­bet, not on months of the year. The thing is we can­not do the sort­ing based on argu­ments. We need to use the MATCH func­tion. It will look up a giv­en month in a list of months. In the third argu­ment we write 0, as we want to have the exact match (Fig. 2)

=MATCH(B2,$J$2:$J$13,0)

 MATCH function
Fig. 2 MATCH function

The MATCH func­tion starts the sort­ing from the top on the Month col­umn. As we can see in col­umn D, we have a prop­er num­ber for each month. The col­umn acts as a helper col­umn, how­ev­er, some­times we don’t want any helper col­umn, so we can cre­ate a helper col­umn in our for­mu­la. We can match each month from the Month col­umn (col­umn B) using the MATCH func­tion, where we need to write the lookup val­ue in the first argu­ment, which is our data col­umn. In the sec­ond argu­ment, we need to write the whole list of months on the right, then let’s write 0 as the exact match (Fig. 3)

SORTBY(A2:C145,MATCH(B2:B145,J2:J13,0))

SORTBY function
Fig. 3 SORTBY function

It works. We see that the SORTBY func­tion found a prop­er num­bers for each month. It’s impor­tant that it sorts by months, not by years. How­ev­er, we can add the year as well. But, I want to show you some­thing else. First of all, if we don’t like the cus­tom helper col­umn list, we can just click F9 in the lookup array argu­ment to hard code the array list in our for­mu­la (Fig. 4)

=SORTBY(A2,C145,MATCH(B2:B145,{“January”;“February”;“March”;“April”;“May”;“June”;“July”;“August”;“September”;“October”;“November”;“December”},0))

Hard coding month names
Fig. 4 Hard cod­ing month names

We can even delete the cus­tom col­umn. As we can see, it works prop­er­ly. When we look at col­umn D, we can see #N/A error. It’s because the MATCH func­tion is try­ing to find months in the cus­tom col­umn cells which are emp­ty (Fig. 5)

=MATCH(B2,$J$2:$J$13,0)

Empty cells in the Month column
Fig. 5 Emp­ty cells in the Month column 

Now, let’s focus on dates based on years and months. Some­times, our date in a dataset isn’t writ­ten only in one col­umn, but it can be sep­a­rat­ed into the year, month and day columns. We can cre­ate a full date from the data we have. We will try it in the Helper col­umn. First of all, we write =, then 1 com­bined with a month, com­bined with a year (Fig. 6)

=1&B2&A2

Creating a date
Fig. 6 Cre­at­ing a date

And just like that, we com­bined the 1st of Jan­u­ary 2022. Our date, how­ev­er, is text. We want Excel to under­stand it as a date, so we just need to add paren­the­ses and a 0 (Fig. 7)

=(1&B2&A2)+0

Changing text into numbers
Fig. 7 Chang­ing text into numbers

Excel mag­ic has just hap­pened! It changed text into num­bers. Now, to change the num­bers into dates, we need to go to the Home tab (1), then change the for­mat­ting (2) into Short Date (3) (Fig. 8)

Numbers into a date
Fig. 8 Num­bers into a date

Since we have dates based on the year and the month columns, we can sort them. Let’s write the SORTBY func­tion. In the first argu­ment we write the data col­umn, and in the sec­ond col­umn we have to com­bine 1 with the Month col­umn, then with the Year col­umn. When we close the paren­the­ses we need to add 0 to change text val­ues into num­bers (Fig. 9)

=SORTBY(A2:C145,(1&B2:B145&A2:A145)+0)

SORTBY function
Fig. 9 SORTBY function

Now, we have only six Jan­u­ar­ies at the begin­ning, because we have have only six Jan­u­ar­ies in 2022. Next six Jan­u­ar­ies are in 2023. This way we sort­ed our data by months and years using a helper column. 

Random list no repeats, manual and formulas solutions

Today, we are going to talk about choos­ing a ran­dom ele­ment from a list with­out repeats. We have eight ele­ments in our list. I put there the same num­ber two times, which gives me the oppor­tu­ni­ty to choose the same num­ber two times. If there are three the same num­bers, I could choose the num­ber up to three times. How­ev­er, the choos­ing is ran­dom, so I don’t real­ly know how many times I will choose it. 

Ran­dom list no repeats, man­u­al and for­mu­las solutions
 A list of elements
Fig. 1 A list of elements

If we want to choose an ele­ment from the list at ran­dom with­out repeats, we can use a man­u­al solu­tion. We need to write the RAND func­tion which returns num­bers from 0 to 1 with fif­teen dig­it pre­ci­sion. It means that it’s almost impos­si­ble to choose the same num­ber twice (Fig. 2). 

=RAND()

RAND function
Fig. 2 RAND function

After drag­ging for­mu­la down, we have 8 ran­dom num­bers. We have to remem­ber that the RAND func­tion is a volatile func­tion. It means that it is recal­cu­lat­ed every time our work­sheet changes (Fig. 3).

Recalculation
Fig. 3 Recalculation

Now, that we have our helper col­umn, we can do the sort­ing. We go to Data tab and we choose the A to Z or Z to A command.

Sorting
Fig. 4 Sorting

If we want to have, e.g. five ele­ments, we have to choose five ele­ments from the top. It does­n’t mat­ter whether we sort from A to Z or Z to A. We just choose the first five ele­ments. We can see that the num­bers are recal­cu­lat­ed every time we sort our data (Fig. 5)

Five elements from the list
Fig. 5 Five ele­ments from the list

Let’s get back to our orig­i­nal list and try the sec­ond solu­tion. It is from Lega­cy Excel and it uses for­mu­las. Let’s start with a way to sort our num­bers. We can use, e.g. the LARGE func­tion, which returns the biggest num­ber from the list, then the sec­ond biggest, then the third biggest, and so on. We have to press the F4 key to lock our list. Then, we need a way to choose the first, the sec­ond, and so on num­bers. So, we want to change our key argu­ment using a prop­er func­tion. One of the sim­plest solu­tion I know is using the ROW func­tion with ref­er­ence to cell A1, which is free (with­out $ signs). When we copy our for­mu­la down, our A1 ref­er­ence will also go down, and the ROW func­tion will return the first row, the sec­ond, the third, and so on (fig. 6)

=LARGE($B$2:$B$9,ROW(A1))

ROW function
Fig. 6 ROW function

After enter­ing the for­mu­la, we have our num­bers placed from the largest to the small­est. Our five ele­ments are now in a prop­er order. It means that now we can choose ele­ments. How­ev­er, we must use the MATCH and INDEX func­tion in our data ori­en­ta­tion. Let’s add the MATCH func­tion to find the posi­tion of our num­ber. We have to add the range, which is our ran­dom num­ber col­umn and press the F4 key to lock it. Then, we are adding 0 because we want to have the exact match (Fig. 7)

=MATCH(LARGE($B$2:$B$9,ROW(A1)),$B$2:$B$9,0)

MATCH function
Fig. 7 MATCH function

After enter­ing and copy­ing down, we have posi­tions of the largest num­bers. When we look at the first posi­tion in our Cho­sen col­umn, we have 5. It means that the largest num­ber from our orig­i­nal list is locat­ed in the 5th place. 

Position of the largest number
Fig. 8 Posi­tion of the largest number

How­ev­er, we don’t want to have posi­tion num­bers, but the ele­ments con­nect­ed to those num­bers. It means that we have to add the INDEX func­tion to our MATCH func­tion. This time, we want to choose ele­ments from our list, so we are adding the range and lock it by press­ing the F4 key. It turns out that our MATCH func­tion is the row num­ber. We have to close the for­mu­la (Fig. 9)

=INDEX($A$2:$A$9,MATCH(LARGE($B$2:$B$9,ROW(A1)),$B$2:$B$9,0))

INDEX function
Fig. 9 INDEX function

After enter­ing and copy­ing down, we have ele­ments select­ed at ran­dom with­out repeats. When I press the F9 key, I am forc­ing it to recal­cu­late our results. Some­times, we see our num­ber (123) twice because it is twice on our orig­i­nal list (Fig. 10)

Elements selected at random
Fig. 10 Ele­ments select­ed at random

It’s high time we tried the final solu­tion in Dynam­ic Array Excel. This solu­tion is sim­pler that the one in Lega­cy Excel because we just need two func­tions. In DA Excel, we have the RANDARRAY func­tion which can cre­ate a ran­dom array. We just need to copy the same col­umn into our for­mu­la, which means that we need an array with eight rows and one col­umn. That’s why we are writ­ing just 8 (Fig. 11).

=RANDARRAY(8)

RANDARRAY function
Fig. 11 RANDARRAY function

After putting our for­mu­la into Excel, we have eight ran­dom num­bers. In DA Excel, we can sort by this col­umn. Thus, we have to add the SORTBY func­tion to our for­mu­la. The array which we want to sort is our list. This time, we don’t have to lock it because it is a DA for­mu­la, and our for­mu­la is the result (Fig. 12). 

=SORTBY(A2:A9,RANDARRAY(8))

SORTBY function
Fig. 12 SORTBY function

After putting the for­mu­la down, we have eight ele­ments from our list in a ran­dom order. Press­ing the F9 key allows us to choose ele­ments once again at ran­dom. Let’s choose five ele­ments from the top once again. We still can see the remain­ing ele­ments. If you don’t want to see them, you have some options. You can, for exam­ple, select five ele­ments from our list, then press the F2 key to go to the for­mu­la edit mode, then press the Ctrl + Shift + Enter com­bi­na­tion to cre­ate an array for­mu­la. The array for­mu­la is from Lega­cy Excel. Now, we have only five ele­ments because I’ve select­ed only five cells. Those cells are an array, which means that we can­not change or delete any sin­gle ele­ment of the array. If you want to change it, you have to change the whole array at one time (Fig. 13).

An array
Fig. 13 An array

Sum­ming up, if you want to choose an ele­ment at ran­dom from a list, you can use a man­u­al solu­tion, a Lega­cy Excel solu­tion or a Dynam­ic Array solution. 

https://www.youtube.com/watch?v=myT-vQzDSeE