Date range filter based on lastest data

Hello guys,

I'm stuck with date filtering- Please suggest

The data is stored in BQ as Month end dates and we have an additional period field to identify the latest data in the database

 Reporting Month end date PeriodMeasure
Today's date is 17th July 202430-Jun-2024Current150,000,000
 31-May-2024Prior100,000,000
 30-Apr-2024Prior50,000,000
 31-Mar-2024Prior150,000,000
 29-Feb-2024Prior100,000,000
 31-Jan-24Prior50,000,000
 31-Dec-23Prior75,000,000
 30-Nov-23Prior25,000,000
    

When you run the report for July 2024 - I want to pick the latest date data (which is 30th Jun 2024) and its last 6 months date (until 31st Dec 2023) using SQL_WHERE or always filters

 

Solved Solved
0 2 469
1 ACCEPTED SOLUTION

Hello @AnalyticsWorld ,
Thank you for your interest in Looker!

Assuming you have a date dimension "created" and a table `looker-core-demo-ffrancois.thelook_ecommerce.order_items`, below would be a valid LookML approach (among many others I'm sure) using sql_always_where to filter on the 6 months prior to your latest date,

 

sql_always_where:
    CASE
    WHEN DATE(${created_raw}) = (SELECT MAX(cast(created_at as date)) FROM `looker-core-demo-ffrancois`.`thelook_ecommerce`.`order_items`)
    OR DATE(${created_raw}) BETWEEN
    DATE_SUB((SELECT MAX(CAST(created_at AS DATE)) FROM `looker-core-demo-ffrancois`.`thelook_ecommerce`.`order_items`), INTERVAL 6 MONTH)
    AND
    DATE_SUB((SELECT MAX(CAST(created_at AS DATE)) FROM `looker-core-demo-ffrancois`.`thelook_ecommerce`.`order_items`), INTERVAL 1 DAY)
    THEN TRUE
    ELSE FALSE
    END ;;

 

To go further, consider using the current date instead of the Max date as this will return a similar result, but may have greater query performances 💡

Take care and happy LookML coding 🙌

View solution in original post

2 REPLIES 2

Hello @AnalyticsWorld ,
Thank you for your interest in Looker!

Assuming you have a date dimension "created" and a table `looker-core-demo-ffrancois.thelook_ecommerce.order_items`, below would be a valid LookML approach (among many others I'm sure) using sql_always_where to filter on the 6 months prior to your latest date,

 

sql_always_where:
    CASE
    WHEN DATE(${created_raw}) = (SELECT MAX(cast(created_at as date)) FROM `looker-core-demo-ffrancois`.`thelook_ecommerce`.`order_items`)
    OR DATE(${created_raw}) BETWEEN
    DATE_SUB((SELECT MAX(CAST(created_at AS DATE)) FROM `looker-core-demo-ffrancois`.`thelook_ecommerce`.`order_items`), INTERVAL 6 MONTH)
    AND
    DATE_SUB((SELECT MAX(CAST(created_at AS DATE)) FROM `looker-core-demo-ffrancois`.`thelook_ecommerce`.`order_items`), INTERVAL 1 DAY)
    THEN TRUE
    ELSE FALSE
    END ;;

 

To go further, consider using the current date instead of the Max date as this will return a similar result, but may have greater query performances 💡

Take care and happy LookML coding 🙌

@francois_care - Thanks a lot , yeah this should work. I'll try this out

Top Labels in this Space
Top Solution Authors