HomeGoogle DocsSpreadsheetHow to Replace Blank Cells with 0 in a QUERY Pivot in...

How to Replace Blank Cells with 0 in a QUERY Pivot in Google Sheets

Published on

Replacing blank cells with 0 in a QUERY pivot in Google Sheets can enhance the readability and interpretation of the data. This can be achieved effortlessly by leveraging modern Google Sheets functions like LET or MAP.

It’s important to note that you don’t need to fully comprehend the QUERY formula you’ve used to apply this trick. Whether you’ve crafted the formula yourself or not, it’s alright. You can still replace blank cells with 0 in the QUERY Pivot result without directly modifying the formula. Instead, we’ll use a wrapper to substitute blanks with zero.

Let’s explore a sample dataset that includes text, number, and date columns. This will help you understand how the formula handles text or date columns while replacing blank cells with 0.

Sample Data and QUERY Formula: Setting Up Your Example Sheet

Let’s work with a minimal sample dataset featuring three data types: Date, Text, and Number.

In the provided sample data (please refer to the image below), column A holds the payment made date, column B contains the item or the type of expense, and column C holds their corresponding amounts.

Query Pivot Formula with Blank Cells in the Resulting Table

The following QUERY formula in cell E1 generates a Pivot table, organizing payment dates in rows, items in columns, and aggregating the payment amounts.

=QUERY(A1:C, "Select A, Sum(C) where A is not null group by A Pivot B", 1)

See the two green dots (manually placed) in the QUERY Pivot output. How do we fill those blank cells with 0? Of course, we can’t enter 0 in those cells because that will break the QUERY formula. Let’s go to our solutions.

You May Like: Learn Google Sheets Query Function: Step-by-Step Guide.

Replacing Blank Cells with 0 in a QUERY Pivot Using the MAP Function

The MAP function is a useful LAMBDA helper function in Google Sheets, allowing us to map each value in an array to a new one.

In this context, the array is the result of a QUERY formula, such as a Pivot table. By employing the MAP function, we can iterate through each value in the Pivot table and replace any blank cells with zero.

Syntax:

MAP(array1, [array2, …], lambda)

Generic Formula:

=MAP(array1, lambda(cell_value, IF(cell_value="", 0, cell_value)))

Simply substitute array1 with your specific QUERY formula.

For example, in my case, the formula to replace blank cells with 0 values would be:

=MAP(QUERY(A1:C, "Select A, Sum(C) where A is not null group by A Pivot B", 1), lambda(cell_value, IF(cell_value="", 0, cell_value)))
Solution: Replacing Blank Cells with 0 in a QUERY Pivot

Note: The formula will convert dates to date values. Therefore, select the dates in the Pivot table and apply Format > Number > Date.

This formula effectively transforms the specified QUERY result, replacing any blank cells with 0 while leaving other values unaffected.

Formula Breakdown:

  • QUERY(A1:C, "Select A, Sum(C) where A is not null group by A Pivot B", 1): This serves as the array or range of values that you intend to manipulate.
  • lambda: This is a function defining the transformation to be applied to each element in the array.
  • cell_value: It is a user-defined name within the lambda function, representing each cell value in the array.
  • IF(cell_value="", 0, cell_value): This IF logical test is the lambda function. It takes each cell_value from QUERY Pivot as input and applies the following logic: IF(cell_value="", 0, cell_value)

Replacing Blank Cells with 0 in a QUERY Pivot Using the LET Function

The LET function facilitates the evaluation of formula expressions by using declared named arguments.

Syntax:

LET(name1, value_expression1, [name2, …], [value_expression2, …], formula_expression)

Generic Formula:

LET(name1, value_expression1, ArrayFormula(IF(name1="", 0, name1)))

In this generic formula, replace value_expression1 with your QUERY formula.

For instance, to replace empty cells with zero in a QUERY Pivot, you can use the following formula:

=LET(name1, QUERY(A1:C, "Select A, Sum(C) where A is not null group by A Pivot B", 1), ArrayFormula(IF(name1="", 0, name1)))

Just like the MAP formula, here as well, you need to format the date values to dates in the Pivot result.

Formula Breakdown:

  • name1: This is the name assigned to the QUERY formula (value_expression1). You can choose a more meaningful name, like “query_formula.”
  • QUERY(A1:C, "SELECT A, SUM(C) WHERE A IS NOT NULL GROUP BY A PIVOT B", 1): The formula (value_expression1) whose result can be referred to later with name1 that was declared before.
  • ArrayFormula(IF(name1="", 0, name1)): This expression is evaluated using the LET function. It checks if the value of name1 is an empty string. If true, it returns 0; otherwise, it returns the original value of name1.

Conclusion

Given that you now have two solutions to replace blanks with 0 in Query Pivot, you might find yourself a little uncertain about which one to opt for.

Feel free to choose either, as both are straightforward formulas and performance-oriented. It’s worth noting that both formulas will convert dates/timestamps to date values. Simply ensure to apply the relevant formatting from the Format menu by selecting those date values.

Related:

  1. How to Fill Empty Cells with 0 in Pivot Table in Google Sheets.
  2. How to Use QUERY Function Similar to Pivot Table in Google Sheets.
  3. How to Format Query Pivot Header Row in Google Sheets.
  4. How to Pivot Multiple Columns in Query in Google Sheets.
  5. How to Retain All Column Labels in Query Pivot in Google Sheets.
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.

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

More like this

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

8 COMMENTS

  1. I think there is a simpler solution than using a query within the query: use the TEXT function with a “multi” format string.

    =ARRAYFORMULA(TEXT(QUERY(...), "0;0;0;@"))

    • Rehan – to enable my pivot table to show “0” instead of blank cells, where would I add your formula to my current formula? I can’t figure it out 🙁

      =QUERY('Order Engagment NATIONAL Chart SORTED Data V1'!U5:W,"SELECT V, SUM(W) Where U is Not Null GROUP BY V PIVOT U")

      • Hi, Weston B,

        If you follow his suggestion, you can replace your formula as below.

        =ARRAYFORMULA(TEXT(QUERY('Order Engagment NATIONAL Chart SORTED Data V1'!U5:W,"SELECT V, SUM(W) Where U is Not Null GROUP BY V PIVOT U"), "0;0;0;@"))

        The issue is it will convert all the number values to text format.

        Here is my suggested formula as per the tutorial.

        ={
        query('Order Engagment NATIONAL Chart SORTED Data V1'!U5:W,"SELECT V, SUM(W) Where U is Not Null GROUP BY V PIVOT U limit 0",1)
        ;
        query(query('Order Engagment NATIONAL Chart SORTED Data V1'!U5:W,"SELECT V, SUM(W) Where U is Not Null GROUP BY V PIVOT U",1),"Select Col1 offset 1",0)
        ,
        ArrayFormula(N(query(query('Order Engagment NATIONAL Chart SORTED Data V1'!U5:W,"Select V, Sum(W) where U is not null group by V Pivot U",1),"Select "&textjoin(", ",true,"Col"&sequence(1,countunique(U6:U),2))&" offset 1",0)))
        }

        Here is my new suggestion:

        Syntax: If(query=space, zero, query).

        Formula:

        =ArrayFormula(if(
        QUERY('Order Engagment NATIONAL Chart SORTED Data V1'!U5:W,"SELECT V, SUM(W) Where U is Not Null GROUP BY V PIVOT U")="",0,
        QUERY('Order Engagment NATIONAL Chart SORTED Data V1'!U5:W,"SELECT V, SUM(W) Where U is Not Null GROUP BY V PIVOT U")
        ))

  2. Hi Prashanth,

    Thanks for all the info on your site, it has been extremely helpful.
    I am stuck on one part of a query pivot if you have any ideas to help.

    I currently have a formula;

    ={query(Filter!A:N, "Select G, Count(J) group by G Pivot H limit 0",1); query(query(Filter!A:N, "select G, Count(J) where G is not null group by G Pivot H", 1),"select Col1 offset 1",0), ArrayFormula((N(Query(query(Filter!A:N, "select Count(J) group by G Pivot H",1),"Select * offset 1",0))))}

    This displays how I want it to and the blanks are now zeros, fantastic.

    One further step that I would like to do is to filter by the year, Column J is a year step with the following years 2017, 2018 and 2019.

    I would like a different pivot for each year.

    Column G is a string with the day of the week, example “1 Sunday” or “4 Wednesday”

    Column H is the week number found using Weeknum()

    Any help would be much appreciated.

    Kind Regards,
    Dan

  3. Great, how about when the values are countA? My chart won’t produce because the “column should be numeric” but dumb google sheets returns "" to countA() instead of zero.

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.