Blog

The FACT Function in Google Sheets (And Why A Shuffled Deck of Cards Is Unique)

Let’s start with a mind-blowing fact, and then use the FACT function in Google Sheets to explain it.

Pick up a standard 52 card deck and give it a good shuffle.

The order of cards in a shuffled deck will be unique.

One that has likely never been seen before in the history of the universe and will likely never be seen again.

I’ll let that sink in.

Isn’t that mind-blowing?

Especially when you picture all the crazy casinos in Las Vegas.

Let’s understand why, and in the process learn about the FACT function and basic combinatorics (the study of counting in mathematics).

Four Card Deck

To keep things simple, suppose you only have 4 cards in your deck, the four aces.

You can create this deck in Google Sheets with the CHAR function:

CHAR cards

The formulas to create these four cards are:

Ace of Clubs:

=CHAR(127185)

Ace of Spades:

=CHAR(127137)

Ace of Hearts:

=CHAR(127153)

Ace of Diamonds:

=CHAR(127169)

Let’s see how many different combinations exist with just these four cards.

Pick one of them to start. You have a choice of four cards at this stage.

Once you’ve chosen the first one, you have three cards left, so there are 3 possible options for the second card choice.

When you’ve picked that second card, you have two cards left. So you have a choice of two for the third card.

The final card is the last remaining one.

So you have 4 choices * 3 choices * 2 choices * 1 choice = 4 * 3 * 2 * 1 = 24

There are 24 permutations (variations) with just 4 cards!

Visually, we can show this in our Google Sheet by displaying all the different combinations with the card images from above:

Fact function card combinations in Google Sheets

(I’ve just shown the first 6 rows for brevity.)

You can see for example, when moving from row 1 to row 2, we swapped the position of the two red suits: the Ace of Hearts and the Ace of Diamonds.

Five Card Deck

This time there are 5 choices for the first card, then 4, then 3, then 2, and finally 1.

So the number of permutations is 5 * 4 * 3 * 2 * 1 = 120

Already a lot more! I have not drawn this out in a Google Sheet and leave that as an optional exercise for you if you wish.

The FACT function

The FACT function in Google Sheets (see documentation) is a math function that returns the factorial of a given number. The factorial is the product of that number with all the numbers lower than it down to 1.

In other words, exactly what we’ve done above in the above calculations.

Four:

The 4 card deck formula is:

=FACT(4)

which gives an answer of 24 permutations.

Five:

The 5 card deck formula is:

=FACT(5)

which gives an answer of 120 permutations.

Six:

A 6 card deck is:

=FACT(6)

which gives an answer of 720 permutations.

Twelve:

A 12 card deck has  479,001,600 different ways of being shuffled:

=FACT(12)

(You’re more likely to win the Powerball lottery at 1 in 292 million odds than to get two matching shuffled decks of cards, even with just 12 cards!)

Fifty Two:

Keep going up to a full deck of 52 cards with the formula and it’s a staggeringly large number.

=FACT(52)

Type it into Google Sheets and you’ll see an answer of 8.07E+67, which is 8 followed by 67 zeros!

(This number notation is called scientific notation, where huge numbers are rounded to the first few digits multiplied by a 10 to the power of some number, 67 in this case.)

This answer is more than the number of stars in the universe (about 10 followed by 21 zeros).

Put another way if all 6 billion humans on earth began shuffling cards at 1 deck per minute every day of the year for millions of years, we still wouldn’t even be close to finding all possible combinations.

Build Numbered Lists With The Amazing SEQUENCE Function

The SEQUENCE function is a useful function in Google Sheets. It’s a powerful way to generate numbered lists.

=SEQUENCE(rows, columns, start, step)

As arguments for the SEQUENCE function, you specify 1) the number of rows, 2) the number of columns, 3) a start value, and 4) a step size.

Arguments 2, 3, and 4 are optional. However, if you want to set them you need to include the previous ones (e.g. if you want to set a step size in argument 4, then you need to set 1, 2, and 3 as well).

Keep this order in mind as you look through the examples below and you’ll soon understand how the function works.

1. Ascending list of numbers

=SEQUENCE(5)

=SEQUENCE(5)

2. Horizontal list of numbers

Set the row count to 1 and the column count to however many numbers you want e.g. 5:

=SEQUENCE(1,5)

=SEQUENCE(1,5)

3. Two-dimensional array of numbers

Set both row and number values:

=SEQUENCE(10,5)

=SEQUENCE(10,5)

4. Start from a specific value

Set the third argument to the value you want to start from e.g. 100:

=SEQUENCE(5,1,100)

=SEQUENCE(5,1,100)

5. Use a custom step

Set the fourth argument to the size of the step you want to use, e.g. 10:

=SEQUENCE(5,1,1,10)

=SEQUENCE(5,1,1,10)

6. Descending numbers

Set the fourth argument to -1 to count down:

=SEQUENCE(5,1,5,-1)

=SEQUENCE(5,1,5,-1)

7. Negative numbers

Set the start value to a negative number and/or count down with negative step:

=SEQUENCE(5,1,-1,-1)

=SEQUENCE(5,1,-1,-1)

8. Dates

Dates are stored as numbers in spreadsheets, so you can use them inside the SEQUENCE function. You need to format the column as dates:

=SEQUENCE(5,1,TODAY(),1)

=SEQUENCE(5,1,TODAY(),1)

9. Decimal numbers

Unfortunately you can’t set decimal counts directly inside the SEQUENCE function, so you have to combine with an Array Formula e.g.

=ArrayFormula( SEQUENCE(5,1,10,1) / 10 )

=ArrayFormula( SEQUENCE(5,1,10,1) / 10 )

10. Constant numbers

You’re free to set the step value to 0 if you want an array of constant numbers:

=SEQUENCE(5,1,1,0)

11. Monthly sequences

Start with this formula in cell A1, which gives the numbers 1 to 12 in a column:

=SEQUENCE(12)

In the adjacent column, use this DATE function to create the first day of each month (formula needs to be copied down all 12 rows):

=DATE(2021,A1,1)

This can be turned into an Array Formula in the adjacent column, so that a single formula, in cell C1, outputs all 12 dates:

=ArrayFormula(DATE(2021,A1:A12,1))

Finally, the original SEQUENCE formula can be nested in place of the range reference, using this formula in cell D1:

=ArrayFormula(DATE(2021,SEQUENCE(12),1))

This single formula gives the output:

1/1/2021
2/1/2021
.
.
.
12/1/2021

It’s an elegant way to create a monthly list. It’s not dependent on any other input cells either (columns A, B, C are working columns in this example).

With this formula, you can easily change all the dates, e.g. to 2022.

Building in steps like this a great example of the Onion Method, which I advocate for complex formulas.

12. Text and Emoji sequences

You can use a clever trick to set the SEQUENCE output to a blank string using the TEXT function. Then you can append on a text value or an emoji or whatever string you want to create a text list.

For example, this repeats the name “Ben Collins” one hundred times in a column:

=ArrayFormula(TEXT(SEQUENCE(100,1,1,1),"")&"Ben Collins")

And, by using the CHAR function, you can also make emoji lists. For example, here’s a 10 by 10 grid of tacos:

=ArrayFormula(TEXT(SEQUENCE(10,10,1,1),"")&CHAR(127790))

Repeating List with SEQUENCE function

Have you got any examples of using the SEQUENCE function?

Radio Buttons in Google Sheets: Only One Checkbox Checked

In this article, we’ll see how to make checkboxes in Google Sheets behave like radio buttons. In other words, we’ll ensure that only one can be checked at a time.

It’s impossible to do this with formulas alone, so we use Apps Script to uncheck boxes as required.

Here are the radio buttons in Google Sheets in action:

Radio Button In Google Sheets

You can see that when I check a new checkbox, any other checkboxes on that row are unchecked.

It takes a split second: you can see the row turns orange when the checked checkbox count is briefly 2, but this is simply the script working in the background.

Let’s see how to implement this with Apps Script.

Radio Buttons in Google Sheets Template

Click here to open the Radio Buttons in Google Sheets template

Feel free to make your own copy (File > Make a copy…).

Before you can use the radio buttons, you need to authorize the script to run.

To do this, open the script editor (Tools > Script editor…), select the onEdit function and run from within the Apps Script editor to grant the necessary permissions.

(If you can’t open the file, it’s likely because your G Suite account prohibits opening files from external sources. Talk to your G Suite administrator or try opening the file in an incognito browser.)

Radio Buttons in Google Sheets

To create your own radio buttons in Google Sheets, add this code to your Sheet:

  1. Go to Tools > Script editor…
  2. Delete the existing myFunction() code
  3. Copy in the code below
  4. Select the onEdit function and run from within the Apps Script editor to authorize the script
  5. Return to your Sheet to use the radio buttons
/**
 * onEdit to uncheck checkboxes as required
 */
function onEdit(e) {
  
  // get event object data: sheet name, row number and column number
  const sheet = e.range.getSheet();
  const row = e.range.rowStart;
  const col = e.range.columnStart;
  
  switch(col) {

    // case when column B is checked
    case 2:
      sheet.getRange("C" + row + ":E" + row).uncheck();
      break;

    // case when column C is checked
    case 3:
      sheet.getRangeList(["B" + row,"D" + row + ":E" + row]).uncheck();
      break;

    // case when column D is checked
    case 4:
      sheet.getRangeList(["B" + row + ":C" + row,"E" + row]).uncheck();
      break;
    
    // case when column E is checked
    case 5:
      sheet.getRange("B" + row + ":D" + row).uncheck();
      break;

    // cell is outside of columns B to D
    default:
      return;

  }
}

So how does this script work?

It uses the onEdit trigger in Apps Script to react when the user checks a checkbox. It then uses the information from that event (i.e. which checkbox was clicked) to know which checkboxes to uncheck.

You can see the lines that begin with e.range gather information about which Sheet we’re in and what the row and column coordinates of the checkbox are.

Then we use a switch statement to see if we clicked in column B, C, D, or E (i.e. column 2, 3, 4, or 5).

If we click a checkbox on either end of the row (i.e. column B or E) then we grab the continuous range on that row (i.e. C2:E2 or B2:D2) and use the uncheck method to uncheck any other checkboxes.

If the middle checkboxes are checked (i.e. column C or D) then the range we want to uncheck is no longer continuous, so we use the getRangeList method to get two ranges in A1 notation (e.g. B2 and D2:E2) and uncheck those checkboxes.

Formula To Count Checked Checkboxes

In column F of the GIF image at the top of this post, you’ll notice a formula that counts how many checkboxes are checked. It’s a simple check to ensure that the radio buttons are working correctly.

It’s a simple COUNTIF formula:

=COUNTIF(B2:E2,true)

(More info on the COUNTIF formula in lesson 3 of my free Advanced Formulas course.)

Formula To Return The Answer Column

We also added another formula to return the answer A, B, C, or D corresponding to the checkbox that is checked. (Note, this is not the column.)

It’s a straightforward INDEX and MATCH formula:

=INDEX($B$1:$E$1,1,MATCH(true,B2:E2,0))

(More info on the INDEX and MATCH formulas in lesson 10 of my free Advanced Formulas course.)

Conditional Formatting To Highlight Row Change

Conditional Formatting with Radio Buttons in Google Sheets

To add conditional formatting to highlight the whole row as it changes, we use the fact that the script takes a split second to run, so there are two checkboxes briefly checked.

The conditional formatting checks whether the COUNTIF result in column F is equal to 2, and if so, applies the formatting.

It’s applied to the whole row by using the $ sign in the conditional formatting custom formula:

=$F2=2

The conditional formatting is the orange that shows when a new checkbox is clicked:

Radio Button In Google Sheets

Generalizing The Script

Thanks to my fellow GDE Adam Morris for his extension to this radio button script, which works regardless of changes to the location of the checkboxes.

Here’s another great radio button tutorial from Kieran Dixon that generalizes the radio button idea to work horizontally or vertically.

Unpivot In Google Sheets With Formulas (How To Turn Wide Data Into Tall Data)

Unpivot in Google Sheets is a method to turn “wide” tables into “tall” tables, which are more convenient for analysis.

Suppose we have a wide table like this:

Wide Data Table

Wide data like this is good for the Google Sheets chart tool but it’s not ideal for creating pivot tables or doing analysis. The main reason is that data is captured in the column headings, which prevents you using it in pivot tables for analyis.

So we want to transform this data — unpivot it — into the tall format that is the way databases store data:

Unpviot in Google Sheets

But how do we unpivot our data like that?

It turns out it’s quite hard.

It’s harder than going the other direction, turning tall data into wide data tables, which we can do with a pivot table.

This article looks at how to do it using formulas so if you’re ready for some complex formulas, let’s dive in…

Unpivot in Google Sheets

We’ll use the wide dataset shown in the first image at the top of this post.

The output of our formulas should look like the second image in this post.

In other words, we need to create 16 rows to account for the different pairings of Customer and Product, e.g. Customer 1 + Product 1, Customer 1 + Product 2, etc. all the way up to Customer 4 + Product 4.

Of course, we’ll employ the Onion Method to understand these formulas.

Template

Click here to open the Unpivot in Google Sheets template

Feel free to make your own copy (File > Make a copy…).

(If you can’t open the file, it’s likely because your G Suite account prohibits opening files from external sources. Talk to your G Suite administrator or try opening the file in an incognito browser.)

Step 1: Combine The Data

Use an array formula like this to combine the column headings (Customer 1, Customer 2, etc.) with the row headings (Product 1, Product 2, Product 3, etc.) and the data.

It’s crucial to add a special character between these sections of the dataset though, so we can split them up later on. I’ve used the fox emoji (because, why not?) but you can use whatever you like, provided it’s unique and doesn’t occur anywhere in the dataset.

=ArrayFormula(B1:E1&"🦊"&A2:A4&"🦊"&B2:E4)

The output of this formula is:

Unpivot Data In Google Sheets Step 1

Step 2: Flatten The Data

Before the introduction of the FLATTEN function, this step was much, much harder, involving lots of weird formulas.

Thankfully the FLATTEN function does away with all of that and simply stacks all of the columns in the range on top of each other. So in this example, our combined data turns into a single column.

=ArrayFormula(FLATTEN(B1:E1&"🦊"&A2:A4&"🦊"&B2:E4))

The result is:

Unpivot Data In Google Sheets Step 2

Step 3: Split The Data Into Columns

The final step is to split this new tall column into separate columns for each data type. You can see now why we needed to include the fox emoji so that we have a unique character to split the data on.

Wrap the formula from step 2 with the SPLIT function and set the delimiter to “🦊”:

=ArrayFormula(SPLIT(FLATTEN(B1:E1&"🦊"&A2:A4&"🦊"&B2:E4),"🦊"))

This splits the data into the tall data format we want. All that’s left is to add the correct column headings.

Unpivot Data In Google Sheets Step 3

Unpivot With Apps Script

You can also use Google Apps Script to unpivot data, as shown in this example from the first answer of this Stack Overflow post.

Further Reading

For more information on the shape of datasets, have a read of Spreadsheet Thinking vs. Database Thinking.

Create A Student Learning Loop In Your Cohort-Based Course

I recently finished teaching my first cohort-based course, the first edition of Pro Sheets Accelerator.

Pro Sheets Accelerator is a live cohort-based course, where students go through the experience together over the course of 5 weeks. We had 37 students in this first cohort.

Rather than watching pre-recorded course videos alone, students met multiple times a week to learn together in a live setting on Google Meet. In addition, we had office hours, guest sessions, a community platform for Q&A, weekly recaps, templates, and replays of the live sessions online.

If video courses are all about the content and information, then cohort-based courses are all about community, accountability, and transformation.

The Cohort-Based Course Student Learning Loop

Student Learning Loop
Cohort-Based Course Student Learning Loop

The Student Learning Loop is a mechanism in your course to facilitate student transformations.

Cohort students pay a premium so they expect a premium outcome. They want to be transformed by the experience.

As the teacher/facilitator you have to create the mechanisms that enable students to have these transformative experiences.

There are countless videos on YouTube teaching your topic, so instead, you have to create an environment where students can undergo a transformation. Watching a YouTube video shows you a new technique. Attending a live session and participating gets you implementing a new technique. For many folks, this makes a big difference.

Let’s walk through the full cohort-based course Student Learning Loop, using specific examples from my Pro Sheets Accelerator course.

Student Learning Loop Phase 1: Learning

Learning happens in the first half of the Student Learning Loop, represented by the blue arc in this diagram:

Student Learning Loop Phase 1
Student Learning Loop Phase 1

The goal here is to get students into the zone of proximal development. To take them outside their comfort zones and stretch their abilities, but not so far that you lose them.

There’s a sweet spot where the majority of your students will be fully absorbed and learning.

There are two components in this phase:

  1. live sessions
  2. community forum for Q&A

Live Sessions

Pro Sheets Live Session
Pro Sheets Live Session 2

How do you conduct an engaging live session teaching technical topics via Google Meet or Zoom?

The key is to make it active with frequent state changes.

You can break up long slide monologues with demos, exercises, and breakout rooms.

A typical 90-minute Pro Sheets Accelerator session looked like this:

  • Ben introduction to reinforce the journey and introduce the first new topic (10 minutes)
  • Ben live demo in a Google Sheet or Apps Script (10 minutes)
  • Student exercise (or breakout room) to practice themselves (20 minutes)
  • Topic consolidation and Q&A as a whole group (10 minutes)
  • Ben slides to introduce the second new topic (5 minutes)
  • Ben live demo of second new topic (10 minutes)
  • Second student exercise or breakout room (15 minutes)
  • Topic consolidation and Q&A as a whole group (5 minutes)
  • Closing discussion: recap what we learned today (5 minutes)

Frequent activities keep the students engaged, which makes for an effective learning environment.

Community Forum For Q&A

Hands up if you’ve thought of great questions after a live session?

Of course you have! We’ve all been there.

Not only that but some students aren’t comfortable asking questions in front of a group. And sometimes students miss a live session but still want to ask questions.

So it’s critical to have a place for students to ask questions about the materials asynchronously, outside the live sessions.

I use the community platform Circle to host the Pro Sheets Accelerator community. It’s an amazing tool that let me create a welcoming space for students to ask their questions.

Here’s an example of the asynchronous learning process from Pro Sheets Accelerator:

IFS, SWITCH, and CHOOSE Functions Example

We covered the IFS function, SWITCH function, and CHOOSE function as part of session 4, in week 2. For many students, these were new functions so they were definitely outside their comfort zones during the live session demo and exercises.

After class, students practiced using these functions in their own work and could ask questions in our Circle forum:

Pro Sheets Circle Forum SWITCH function answer
Pro Sheets Circle Forum SWITCH function answer

In this particular example, a fellow student helped answer the question.

This peer coaching is another example of the value of cohort-based courses. Everyone is both a student and a teacher, bringing their own unique skills and experiences to the table.

Student Learning Loop Phase 2: Assimilation and Application

The second half of the Student Learning Loop happens when students incorporate information from the live sessions into their own workflows.

Both the office hours and the community forum help students do this effectively.

This is the second half of the Student Learning Loop, shown in green:

Student Learning Loop Phase 2
Student Learning Loop Phase 2

Student learning doesn’t stop once the lesson ends.

In fact, it’s really just the beginning.

True learning happens when students apply knowledge from the lessons to their own specific situations. Students benefit enormously from rapid feedback, so they don’t get stuck for long and learn quickly from their mistakes.

There are three components in this phase:

  1. live office hours
  2. more questions in the community forum
  3. replays of live sessions

Live Office Hours

In Pro Sheets, we had live weekly office hours. These were 90-minute, drop-in, unstructured sessions where students could ask whatever questions they wanted.

They were a place for students to ask specific questions from their own domains.

We used a Google Sheet to collect questions, which then served as a repository of that knowledge for future reference.

Pro Sheets Office Hours Sheet
Pro Sheets Office Hours Question Sheet

More Questions in the Community Forum

Throughout phase 2, students have lots of questions so the community plays an integral part in the assimilation and application of knowledge.

Students deepen their knowledge by asking and answering each other’s questions in the community forum.

Students also share their work wins with the community to get the validation they’re on track, which builds their confidence and reinforces the learning experience.

QUERY Function Example

For example, in week 2, I covered how to use the QUERY function to solve a challenging data analysis problem. The students were given a dataset of fires in New York State and asked to answer the question:

What is the average fire length in days, by county?

It was a challenging question because it required a query on top of another query (akin to a sub-query in SQL).

And here’s one student sharing their answer with the community:

Pro Sheets Circle Forum QUERY function answer
Pro Sheets Circle Forum QUERY function answer

Replays and Templates

I use Teachable to host my on-demand video courses so it was a natural place to also host the video replays of the live session recordings for the Pro Sheets Accelerator course.

Teachable allows me to present the video recordings in a syllabus, with links to all of the template files.

Students have lifetime access to these video recordings and templates, so they can watch the live session replays and review topics as many times as they want.

This repetition helps cement the understanding.

Pro Sheets replay on Teachable
Pro Sheets Session 1 replay on Teachable

Completing the Loop

Some students will progress through the loop multiple times a week, on the back of every live session. Others might progress at a slower pace and go through the loop once a week, whilst for others, it might happen a couple of times throughout the whole course.

Students undergo a transformation when they go through the learning loop. They return to work with new abilities and newfound confidence.

And that is the north star outcome we’re aiming for as course creators.

Evidence of A Transformation

“If you can’t measure it, you can’t improve it.” – Peter Drucker

Pro Sheets Accelerator Cohort 1
Virtual high-fives during the final live session

I used Google Forms to conduct pre-course and post-course surveys, so I could measure and understand the transformation occurring at different stages of the Student Learning Loop framework.

In the post-course feedback form, I asked lots of questions, including what was the most valuable thing about the course, what they liked about the Circle community, the project, and the office hours. Here are some of the answers for Pro Sheets:

“It was great to work on a project during the week knowing that if I got stuck I could ask for help on Friday [office hours].”

“I’m really enjoying the course. I find myself thinking about the subject matter at odd hours, so it’s really taking root in my head.”

“Everyone in Circle was SO HELPFUL! And I loved seeing other peoples’ projects and questions. I got so many new ideas and perspectives. I also loved that in Circle we could build off of each others’ ideas. Like, the “Adding Notes” topic – you started with the basic idea but then so many people chimed in to make improvements. I have that script in at least 3 of my sheets now!”

“The workflow and BAR models are very useful concepts that I knew in a practical sense but needed to see more concretely. I feel like I moved forward on Array formulas, queries, custom functions, and Index-Match-Match.”


See also: 5 Insights From Taking A Live Cohort-Based Course

My Twitter thread on the life cycle of Pro Sheets Accelerator, from conception to the post-course survey.


What’s next for Pro Sheets Accelerator?

This fall, I’m running the Pro Sheets Accelerator course again.

Hop onto my mailing list if you want to stay in the loop on both these courses.