Using Field Filter in a view

Hello again everyone,

This time I need your help about Field Filter.
I have a table (payout) that has a datetime column (created_at) in UTC. I need to create a dashboard that will be controlled by a date filter.
However, to get an accurate data shown by this dashboard I need to convert this created_at from UTC into local timezone. Therefore, I created this view (payout_view) and my SQL in Metabase is:

select count(*) as trx
from stars.payout_view where status = 'funding_received'
and date(created_at) = {{created_at}}

which {{created_at}} is the Field Filter and refer to a table in stars schema,
while my view query is:

create or replace view stars.payout_view as
with converted as (
select payout.id
, payout.user_id
, case when company.country_id = 19 then payout.created_at + interval '6 hours' else payout.created_at + interval '7 hours' end as created_at
, payout.status
from production.payout
left join production.company
on payout.company_id = company.id)
select * from converted
with no schema binding

So, when I run my SQL in metabase, I got this error
ERROR: invalid reference to FROM-clause entry for table "stg_stars__date" Hint: There is an entry for table "stg_stars__date", but it cannot be referenced from this part of the query.

Do you know what's wrong with my setup?
Thanks!

Hi @abed
You should not include column or operator, when using Field Filters.
Please read this (the entire article):
https://www.metabase.com/learn/sql-questions/field-filters

Hi @flamber
Apologies, I pasted outdated code.
I meant this code:

select count(*) as trx
from production.payout_view where status = 'funding_received'
and {{created_at}}

and the error is
ERROR: invalid reference to FROM-clause entry for table "payout" Hint: There is an entry for table "payout", but it cannot be referenced from this part of the query.

@abed Make sure that you have selected the correct table and field it should reference in the sidebar, since it looks like you might have created the variable originally for a different table.

Hi @flamber, I was suspecting that as well. Then I tried another SQL and now it's not a view:

select * from
( select payout.id
, payout.user_id
, case when company.country_id = 19 then payout.created_at + interval '6 hours' else payout.created_at + interval '7 hours' end as created_at
, payout.status
from production.payout
left join production.company
on payout.company_id = company.id
) a
where {{created_at}}

and selected the reference column from a table of the same schema (production.payout.created_at) in the sidebar.
However, the error was still the same.

@abed You cannot reference table aliases: https://www.metabase.com/learn/sql-questions/field-filters#field-filter-gotchas
The where-clause has to be in the same select as the table reference.

I'm actually not making any aliases to tables cmiiw.
Your second answer makes sense. That means, it'll still be the same if I use a view then?

@abed You are making aliases.

) a
where {{created_at}}

I would recommend that you check the query log on your database. It might help you understanding what is going wrong.
Metabase works with Views, just like regular tables.

Ah I see..
I didn't know that.
Thanks @flamber, I'll try with my view and be back with the result.
Really appreciate your help!

Hi @flamber,

Here is the result.
I created a view called payout_view which is formed by:

create view production.payout_view
as
select payout.id
, payout.user_id
, case when company.country_id = 19 then payout.created_at + interval '6 hours' else payout.created_at + interval '7 hours' end as created_at
, payout.status
from production.payout
left join production.company
on payout.company_id = company.id
with no schema binding

then in the Metabase, my query is:

select * from
production.payout_view
where {{created_at}}

I also selected the column from a table of the same schema as the query in view as the reference. Please see the screenshot below:

Did I miss any step?
Thanks!

@abed But in the "Field to map to", it looks like you are referring to the actual Table and not the View.
It's difficult for me to see specifics, since you might have renamed the View in Metabase's Admin > Data Model.

Remember that if you have just created or changed structure, then make sure you sync via Admin > Databases > (db) > Sync database schema now.

Hi @flamber,

"Field to map to" is the column containing list of values we can pick from, right? Or should it be from the view as well in my case?

Update

@flamber
After selecting created_at from payout_view view as "Field to map to", it works fine!
Of course I had to sync the database manually first so that it would appear on the sidebar.


Thank you @flamber for your help.

1 Like