Screen orientation calculation in Google Data Studio
If you’re just here for the screen orientation CASE formula to use in Google Data Studio, here it is:
CASE
WHEN CAST(REGEXP_REPLACE(Screen Resolution, 'x.*', '') AS NUMBER)<CAST(REGEXP_REPLACE(Screen Resolution, '.*x', '') AS NUMBER) THEN "Portrait"
ELSE "Landscape"
END
Code language: PHP (php)
Digging into it a bit further, the way tis works is that it takes the bit before the ‘x’ of the Screen Resolution dimension (the x-axis), and sees if it’s smaller than the bit after the ‘x’ (the y-axis). So if the x-axis is less than the y-axis the screen orientation is portrait, otherwise it’s landscape.
I’m using the Universal Analytics dimension ‘Screen Resolution’ in this example, but you can swap out that for any other reference you have, as long as it has the same format of ‘AxB’ – i.e. 900×800. You can even swap out the ‘x’ if there is another delimiter in your data. This screen orientation calculation is agnostic and easily changed to fit whatever your data looks like.
In Data Studio, you can add this in the data source itself, or as a custom calculation in any chart as such:
Using this newly created dimension, you can use them in charts to break down any data into landscape and portrait.
It’s also useful in tables where you can use it as a filter to give the viewer a different way to view device data. For example:
A small improvement on the formula at the top of this post is below where it accounts for square resolutions too! Not that it occurs frequently to be honest, but it’s all about being precise.
CASE
WHEN CAST(REGEXP_REPLACE(Screen Resolution, 'x.*', '') AS NUMBER)<CAST(REGEXP_REPLACE(Screen Resolution, '.*x', '') AS NUMBER) THEN "Portrait"
WHEN CAST(REGEXP_REPLACE(Screen Resolution, 'x.*', '') AS NUMBER)>CAST(REGEXP_REPLACE(Screen Resolution, '.*x', '') AS NUMBER) THEN "Landscape"
ELSE "Square"
END
Code language: PHP (php)
They do exist though:
We cover this example and lots of others in our Data Studio Kickstarter training course. If you’d like to book in the training for your team, or have any questions about it, please get in touch.