Field Filter on multiple tables

Hi guys,

I’m trying to write a query that involves multiple tables but it seems like it’s not possible due to the nature of Field Filter being tied to a table’s column. Is there a way to refer to a Field Filter’s from and to directly in the query? I want to use a Field Filter so I can use the same dashboard filter as my other queries, so having two Date variables are not an option for me. That option sounds like a hack.

The query is basically:

SELECT
  (SELECT SUM(field) FROM table_a WHERE {{date}}) /
  (SELECT SUM(field) FROM table_b WHERE {{date}})

I feel like the field filter should look more like {{date:table_a.created_at}} so it can be referred to different fields in one query and generated properly. Maybe I don’t know how to use this. Please help!

2 Likes

I'm facing the exact same issue. Did you ever find a solution? Thanks!

Field Filters does not support aliasing currently:
https://github.com/metabase/metabase/issues/3324 - upvote by clicking :+1: on the first post

https://www.metabase.com/learn/sql-questions/field-filters
https://www.metabase.com/docs/latest/questions/native-editor/sql-parameters#the-field-filter-variable-type

There are a lot of upvotes on that issue sir! Do you have any idea when they plan to add alias support?

I saw your response about a lookup table in some other places. I don't have write access to the database I'm querying - can you think of any other workaround?

Thank you!

@calvin Use two Date filters instead if you don't have the option to create a lookup table. I cannot provide any timelines for any issues.

One of the workaround I've just come up with is to add an extra inner join with one of the tables on a date condition.

context: I have a cte called table1 containing from multiple tables, and I'd like to apply the same date for each of them, then aggregate values. so I add an unnecessary join just to attach the table2 and return distinct values only from the table1 as there will be lots of duplications.

select bank, sum(credit)
from (
    select distinct t.bank, t.trans_datetime, t.credit
    from table1 t
        join table2
            on extract(month from t.trans_datetime) = extract(month from table2.trans_datetime)
            and extract(year from t.trans_datetime) = extract(year from table2.trans_datetime)
    where {{ month }}
) temp
group by bank;