Displaying null values in sums

Looker Coalesces by Default

By default, Looker coalesces sum measures with zero. This gives us a nice “0” display rather than a NULL; in most cases we don’t care about the distinction between “zero” and “no data”. If we’re talking about daily sales, for example, no data is essentially the same thing as no sales.

Sums with Null Values

Sometimes, however, we do care about the distinction. In cases like these, I like to create a numeric measure (type: number or type: int) and then do the SUM() in the sql parameter.

Take this for example

In the following LookML, we create a derived table with a null value. We also create two sum measures, one with no monkeying around (total_dogs) and one that uses the above technique (total_dogs_with_nulls). As you can see by the associated results and SQL, total_dogs_with_nulls shows up as we want it (with null values)!

The LookML

- connection: thelook

- explore: orders
- view: orders
  derived_table:
    sql: |
      select null as "dog" union select 1
    persist_for: 5 minutes
    indexes: [dog]
  
  fields:
  
  - dimension: dog
    type: number
    value_format: '$#,##0'
    sql: ${TABLE}.dog

  - measure: total_dogs
    type: average
    value_format: '$#,##0'
    sql: ${dog}
    
  - measure: total_dogs_with_nulls
    type: number
    value_format: '$#,##0'
    sql: sum(${dog})

The Results

The SQL

Hi…i see that Looker is not defaulting NULL values in Sum to 0. I still see the ‘Phi’ symbol. Is there any way that i can override this behavior so that i can display 0 instead of Phi?

Hi @kmahamk,

What does your query look like? For example, are you using pivots? Are you using type: sum or type: number and then doing the SUM() in the sql: parameter? Let me know and we can go from there!

Cheers,
Vincent

Yes I’m using pivot on a column. Also I’m using type of count_distinct. Whenever there are no records for specific category in pivot column, i see that the count is displayed as phi symbol instead of zero. In my case I want the count to be displayed as 0 if there are no records for that pivot category instead displaying a phi symbol. Any way to display 0 in this scenario?

The way to do this would be COALESCE. This is a function that takes a list of values and returns the first non-null value in that list. So for example COALESCE(NULL, 0) returns 0.

This function is supported in table calculations - you can just write coalesce(${measure_name}, 0) as a calculation. This is the recommended solution.

Edit: removing the LookML “solution” because it does not work with pivots. Use the table calculation solution above.

Thank you all for your help :slight_smile:

I have an additional question: How can we display a null value sum if we are using filters{}? filters is not supported for non-aggregate types.

Hmm that’s a good question. The easiest way would be to convert your filter to a CASE WHEN in the sql of the measure, and put it as a type: number like the main post suggests…

It’s possible that a better way exists though— Anybody have an idea?