title: Last event of a user
body: <p>could you help me.</p>
  <p>I have the query below that works and meets my need but I don't know if it was done in the best way and if there is a less complex way to build the query.</p>
  <p>Purpose: Capture the last event (event_name) of the user_id, only one per user and the most recent.
  Query developed in Bigquery environment for GA4 event tables</p>
  <pre><code>    CAST(CONCAT(SUBSTR(t2.event_date, 0, 4), '-', SUBSTR(t2.event_date, 5, 2), '-', SUBSTR(t2.event_date, 7, 2)) AS DATE) AS event_timestamp,
      DATE_DIFF(CURRENT_DATE(&quot;UTC-3&quot;),CAST(CONCAT(SUBSTR(t2.event_date, 0, 4), '-', SUBSTR(t2.event_date, 5, 2), '-', SUBSTR(t2.event_date, 7, 2)) AS DATE),day) AS days_ult_event,
      t1.user_id,
      t2.event_name
    FROM (
      SELECT
        MAX(TIMESTAMP_MICROS(event_timestamp)) event_timestamp,
        USER_ID
      FROM
        `events_*`
      WHERE
        user_id IS NOT NULL
      GROUP BY
        2) t1
    LEFT JOIN (
      SELECT
      DISTINCT user_id,
        event_name,
        event_date,
        TIMESTAMP_MICROS(event_timestamp) AS event_timestamp
      FROM
        `events_*`) t2
    ON
      t1.user_id = t2.user_id
      AND t1.event_timestamp = t2.event_timestamp
  </code></pre>