Not all NULLs are the same NULLs

Why You Should Pay Attention to NULLs in BigQuery and GA4

🚨 Not all NULLs are the same! 🚨

When working with BigQuery, especially with data from GA4 (Google Analytics 4), it’s crucial to understand a subtle but important concept: NULLs are not generic, they are tied to specific data types.


What does this mean in practice?

In BigQuery:

  • A NULL of type DATETIME is different from a NULL of type STRING or NUMERIC.
  • This means that if not handled properly, these differences can lead to errors when combining or comparing values of different types.

For example:

-- This will throw an error!
WITH SampleData AS (
  SELECT NULL AS int_null, CAST(NULL AS STRING) AS string_null
)
SELECT * FROM SampleData;

BigQuery requires that all types in a query be consistent.


Why does this matter for GA4?

GA4 uses a highly flexible event-based data structure where values can:

  • Appear as NULL if a field is not populated.
  • Change type dynamically (e.g., STRING for custom dimensions, NUMERIC for counts and metrics).

⚠️ Common pitfalls include:

  • Direct comparisons between NULLs of different types causing errors.
  • Challenges in creating combined reports if data types are not managed correctly.

How to handle this effectively?

Here are a few best practices:

  1. Standardize data types: Convert NULL values to a common type (e.g., STRING) to avoid errors.
  2. Identify data types: Add explicit fields to track the original data type, as shown in the corrected query (see below).
  3. Monitor GA4 data: Carefully review dimensions and metrics to identify potential NULLs in your reports.

Correct query:

WITH SampleData AS (
  SELECT
    'int_null' AS column_name, CAST(NULL AS STRING) AS column_value, 'INT64' AS data_type
  UNION ALL
  SELECT
    'string_null', CAST(NULL AS STRING), 'STRING'
  UNION ALL
  SELECT
    'datetime_null', CAST(NULL AS STRING), 'DATETIME'
  UNION ALL
  SELECT
    'numeric_null', CAST(NULL AS STRING), 'NUMERIC'
)

SELECT
  column_name,
  data_type,
  column_value,
  CASE
    WHEN data_type = 'STRING' THEN 'STRING'
    WHEN data_type = 'DATETIME' THEN 'DATETIME'
    WHEN data_type = 'NUMERIC' THEN 'NUMERIC'
    WHEN data_type = 'INT64' THEN 'INT64'
    ELSE 'UNKNOWN'
  END AS null_data_type
FROM
  SampleData
WHERE
  column_value IS NULL;

Expected Result

Running the query above, you will obtain output similar to the following:

column_namedata_typecolumn_valuenull_data_type
int_nullINT64NULLINT64
string_nullSTRINGNULLSTRING
datetime_nullDATETIMENULLDATETIME
numeric_nullNUMERICNULLNUMERIC

Why does this matter?

💡 Properly handling NULLs in BigQuery allows you to:

  • Build more robust, error-free queries.
  • Avoid unexpected results in GA4 reporting.
  • Save valuable time troubleshooting complex issues.

💭 Have you ever encountered NULL-related problems when working with BigQuery or GA4? How did you solve them? Share your experience in the comments!

#BigQuery #GA4 #DataAnalytics #SQLTips #DataEngineering