Loop through list of fields using Liquid to check _in_query

I’m trying to loop through an array of strings that represent field names in my view to check if each field name is included in a query and if so append that field name as a string to a new variable.

The goal here is to build a derived table based on what a user selects in a query.

Currently, I have to hard code in each `_in_query` check so it’s a mess of duplicate code since we’re checking 10+ fields and this list could grow.

Here is what I attempted:

In LookML file:

```

{% assign potential_grouping_dims = 'field_name_1,field_name_2,field_name_3’ | split: ',' %}

{% assign grouping_dims = ‘’ %}

{% for dim in potential_grouping_dims %}

    {% assign assigned_dim_in_query = 'view_name_1.' | append: dim | append: '._in_query' %}

    {% if assigned_dim_in_query  %}

        {% assign grouping_dims = grouping_dims | append: dim | append: ',' %}

    {% endif %}

{% endfor %}

```

However, it appears that when I try to use liquid to loop through and check if the field is in the query `{% if assigned_dim_in_query  %}` always evaluates to TRUE since `assigned_dim_in_query` evaluates to `’view_name_1.field_name_1._in_query’` (a string) rather than being evaluated as specific LookML variable `view_name_1.field_name_1._in_query`.

Here is what works (static check for each field)

```

{% if field_name_1._in_query = ‘’ %}

    …

{% endif %}

{% if field_name_2._in_query = ‘’ %}

    …

{% endif %}

{% if field_name_3._in_query = ‘’ %}

    …

{% endif %}

```

Is it possible to use a liquid array to check if a field is in a query so that we don’t have add a bunch of duplicate LookML?

2 2 2,671
2 REPLIES 2
Top Labels in this Space