Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

unexpected results in BigQuery query for finding first non-null value

 

 

select
    concat(
        user_pseudo_id,
        (
            select
                value.int_value
            from
                unnest (event_params)
            where
                key = 'ga_session_id'
        )
    ) as session,
    TIMESTAMP_MICROS(event_timestamp),
source,
    (
        select
            value.string_value
        from
            unnest (event_params)
        where
            key = 'campaign'
    ) as campaign,
    FIRST_VALUE(
        (
            select
                value.string_value
            from
                unnest (event_params)
            where
                key = 'campaign'
        ) IGNORE NULLS
    ) OVER (
        partition by
            concat(
                user_pseudo_id,
                (
                    select
                        value.int_value
                    from
                        unnest (event_params)
                    where
                        key = 'ga_session_id'
                )
            )
        ORDER BY
            TIMESTAMP_MICROS(event_timestamp) ASC
    ) AS first_campaign
from
    `my_project_id.google_analytics_id.events_*`
where
    _table_suffix = "20230328"
    and concat(
        user_pseudo_id,
        (
            select
                value.int_value
            from
                unnest (event_params)
            where
                key = 'ga_session_id'
        )
    ) = {my session to check}
order by
    session,
    TIMESTAMP_MICROS(event_timestamp)

 

 

Above is my bigquery code to find out the  first non-null value of ‘campaign’ for each session ordered by timestamp.
Most of the session as my imagined, but I get some result like below table:

session
event_timestamp
 campaignfirst_campaign
A2023-03-28 15:08:54nullnull
A2023-03-28 16:08:54nullnull
A2023-03-28 17:08:54(organic)(organic)

I am not sure why this result occurred.
Could anyone please help me understand the reason for this result?

0 1 1,086
1 REPLY 1

Check if the event_params array for those sessions contains a non-null value for the "campaign" parameter at all. You can modify the query to include this check by adding a CASE statement. Use the COUNTIF function to check if there are any non-null values for the "campaign" parameter within the event_params array.