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

c1623c549828e2ef1bcd3b9c762d6ce6e34496b5.png

The SQL

bbef428a9b6355941351c049d431b33fb2843399.png

0 13 14.5K
13 REPLIES 13
Top Labels in this Space
Top Solution Authors