Blog Post

Excel Blog
3 MIN READ

See what's possible with Copilot in Excel (part 7)

msexcel's avatar
msexcel
Icon for Microsoft rankMicrosoft
Sep 30, 2024

Hi everyone, over the last few weeks we have had a series of posts to show you some of the things that are possible to do with Copilot in Excel. This week, we will showcase how Copilot in Excel can help create a formula to calculate distinct values. 

In this example, we are 
given a table of event locations and we'd like to figure out a formula to count each distinct location.


Table of event locations with columns for Date and Location

 

Given the values that are returned, let's find out how many location are listed:


How many distinct locations are there?

Copilot in Excel pane with the above prompt and the response below

 

Analyzed data in B2:C156 returns:

Distinct Count of Location

50

Copilot said:

There are 50 distinct locations.

 

Copilot returns an answer of 50, but I would like to have a formula that updates as the list changes. A coworker suggested the formula below, but I don’t understand it. I ask copilot:

 

A coworker gave me this formula to calculate the distinct locations, can you tell me how it works: =SUM(--(FREQUENCY(MATCH(C:C, C:C, 0), MATCH(C:C, C:C, 0)) > 0))

Copilot in Excel pane with the above prompt and the response below

 

This formula calculates the number of distinct locations in column C. Here's how it works:

  • MATCH(C:C, C:C, 0): This part of the formula creates an array of the positions of each value in column C. The 0 indicates an exact match.
  • FREQUENCY(MATCH(C:C, C:C, 0), MATCH(C:C, C:C, 0)): The FREQUENCY function is used to count the number of times each value appears in the array created by the MATCH function. It returns an array where each element represents the frequency of the corresponding value.
  • FREQUENCY(...) > 0: This part of the formula creates a boolean array where each element is TRUE if the corresponding frequency is greater than 0, and FALSE otherwise.
  • --(FREQUENCY(...) > 0): The double unary operator (--) converts the boolean array into an array of 1s and 0s, where TRUE becomes 1 and FALSE becomes 0.
  • SUM(...): Finally, the SUM function adds up the array of 1s and 0s, giving the total count of distinct locations.

I understand the formula, but it seems overly complicated. I’d like to see if Copilot can do better. I ask:

 

Can you simplify the formula?

Copilot in Excel pane with the above prompt and the response below

 

Yes, I can simplify the formula. Here is a more straightforward way to count distinct locations using the UNIQUE and COUNTA functions:

=COUNTA(UNIQUE(Table19[Location]))

 

That is much simpler! I copy the formula from the copilot pane and copy into my workbook. Now when I make changes to the table, the unique location count updates automatically.

 

2 cells with the values “Unique Locations” and the value “50” returned from the formula that was entered, next to the event location table.

 

Here are some additional examples from the last six weeks if you missed them:

 

Stay tuned,

Microsoft Excel Team

 

*Disclaimer: If you try these types of prompts and they do not work as expected, it is most likely due to our gradual feature rollout process. Please try again in a few weeks.

Updated Sep 27, 2024
Version 1.0
  • Sameer_Bhide's avatar
    Sameer_Bhide
    Iron Contributor

    I cant believe this blog post  ! - So your premium AI (Co Pilot) gives you the Complex answer first and only when prompted returns the simpler solution - Should you not be paying us 10$ to use it ? 🙂

     

    Cheers 

    Sam

  • BA_Max's avatar
    BA_Max
    Iron Contributor

    Sameer_Bhide I think you might have misunderstood.

     

    In the example, a colleague of the user provided them with a complex formula. When they put it into copilot, copilot provided them with the simpler formula. You're not just paying for Excel copilot either, it's a monthly subscription to use copilot across all Microsoft 365 solutions.