How to Perform Conditional MMULT in Google Sheets

Published on

MMULT is primarily used for matrix multiplication, but it can also serve as an alternative to SUMPRODUCT to achieve similar results in a more expanded form. As a side note, SUMPRODUCT returns the sum of the products of corresponding entries in two equal-sized arrays or ranges, and we can apply specific conditions to control the sum of products. This is where the application of criteria or conditions in MMULT becomes important. Let’s explore how to perform conditional MMULT calculations to replace the SUMPRODUCT function in Google Sheets.

Understanding Conditional MMULT in Google Sheets

In the following example, I have items listed in A1:A7, their unit prices in B1:B7, and the monthly sales of these items from January to March across three columns (C1:E7).

SUMPRODUCT Without Condition - Example 1

You can use the following SUMPRODUCT formula in cell C8 to calculate the total sales for January:

=SUMPRODUCT($B$2:$B$7, C2:C7)

Drag this formula to E8 to get the total sales for the other months.

Alternatively, you can use the following MMULT formula in cell C8 to calculate the total sales for each month at once:

=MMULT(TRANSPOSE(B2:B7), C2:E7)

This eliminates the need to drag the formula across multiple cells.

Now, let’s explore conditional MMULT with the same example.

Suppose you want to calculate the monthly sales for a specific item, such as “Pear.”

You can use the following SUMPRODUCT formula in cell C9 and drag it across:

=SUMPRODUCT($A$2:$A$7="Pear", $B$2:$B$7, C2:C7)
SUMPRODUCT with Condition - Example 2

But how would you approach this using conditional MMULT?

Let’s dive into the details!

Example of Conditional MMULT in Google Sheets

The conditional MMULT formula is not much different from the regular MMULT formula mentioned above. What you need to do here is replace the unit rates of all other items with 0.

In our previous MMULT without criteria, the matrices are as follows:

  • Matrix 1: TRANSPOSE(B2:B7)
  • Matrix 2: C2:E7

For the conditional version, Matrix 1 will be defined as IF(A2:A7="Pear", B2:B7, 0).

This logical test requires the ARRAYFORMULA, so you should enter the MMULT as an array formula.

Here is the conditional MMULT formula:

=ArrayFormula(MMULT(TRANSPOSE(IF(A2:A7="Pear", B2:B7, 0)), C2:E7))

How Do We Specify Multiple Conditions?

Once you learn the above, specifying multiple conditions is not a Himalayan task.

For example, to get the total sales amount for the fruits “Pear” and “Avocado,” you can specify the conditions as follows:

Replace A2:A7="Pear" with (A2:A7="Pear")+(A2:A7="Avocado").

So, the conditional MMULT formula with multiple conditions will be:

=ARRAYFORMULA(MMULT(TRANSPOSE(IF((A2:A7="Pear")+(A2:A7="Avocado"), B2:B7, 0)), C2:E7))
MMULT with Multiple Conditions - Example 3

Additional Notes

Empty cells in matrix multiplication can cause errors in the formula. If you expect blank cells in any range (matrix 1 or matrix 2) specified in the formula, you should use the N function. The N function returns the value if it is not empty; otherwise, it returns 0.”

In the above examples, you can wrap B2:B7 (unit rates) and C2:E7 (quantities) with N as follows:

=ARRAYFORMULA(MMULT(TRANSPOSE(IF((A2:A7="Pear")+(A2:A7="Avocado"), N(B2:B7), 0)), N(C2:E7)))

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Hierarchical Numbering Sequences in Excel

Creating hierarchical numbering sequences in an Excel spreadsheet can significantly improve the way you...

How to Easily Repeat a Sequence of Numbers in Excel

Excel offers multiple ways to accomplish tasks, and the simplicity of each approach depends...

Create a Sequence of Dates at Every Nth Row in Excel (Dynamic Array)

Would you like to create a sequence of dates in every nth row in...

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.