If you work with BigQuery and GA4, you know how tricky segmentation can get — it’s like finding a decent café with reliable Wi-Fi. Recently, I came across a post by Katie Kaczmarek, where she shared how to use LOGICAL_OR and LOGICAL_AND.
Spoiler alert: it’s a game-changer.
These functions are like condition checkers on steroids. LOGICAL_OR checks if at least one condition is true, while LOGICAL_AND ensures all conditions are met. They work within window functions, allowing you to analyze data across groupings like users, sessions, or whatever you want (or need).
Here’s how they shine in real life: say you’re looking at GA4 data with user IDs, events, and parameters. You want to identify users who’ve clicked on an ad at least once, or sessions where multiple actions happened — like viewing a page, adding to the cart, and completing a purchase. That’s where LOGICAL_OR and LOGICAL_AND come in and save you hours of frustration.
The best part? These functions aren’t limited to simple conditions like “event = X.” Need to segment users based on combinations? For instance, find those who performed a specific action with certain parameters — no problem. They can handle filtering by event parameters, user attributes, or even custom metrics with ease.
Let’s take it up a notch. Imagine you’re identifying users who don’t just click on ads but do so under specific circumstances — like clicking on a banner with a discount icon or a button that screams “Buy Now.” Here, LOGICAL_AND steps in to ensure all your criteria are met: the user, the action, the icon. If even one element is missing, the function will politely tell you, “Nope, not your segment.”
These functions are powerful tools for grouping your data. Want to focus on users? Done. Sessions? Easy. Specific events? Absolutely. And the cherry on top — you get clean, actionable results without wading through endless irrelevant rows.
If you work with GA4 to BigQuery exports, be sure to check out my SQL cheat sheet.