Screen orientation calculation in Google Data Studio

Here's a Data Studio CASE formula to work out the screen orientation (portrait or landscape) from the resolution.

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:

Data Studio interface with the calculation editor open with the formula for screen orientation visible

Using this newly created dimension, you can use them in charts to break down any data into landscape and portrait.

Data Studio bar and pie charts showing screen orientation (landscape and portrait) by day

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:

Data Studio table showing a filter control for screen orientation with both portrait and landscape selected
Data Studio table showing a filter control for screen orientation with portrait selected

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:

Data Studio table showing a filter control for screen orientation with 'square' selected

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.

Share:
Written by

Daniel is an Analytics Innovation Lead at Measurelab - he is a trainer, podcaster and overall fanatic. He loves getting stuck into all things GA4 and anything new and exciting.

Subscribe to our newsletter: