> For the complete documentation index, see [llms.txt](https://docs.dbnl.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.dbnl.com/v0.23.x/using-distributional/tests/creating-tests/using-filters-in-tests.md).

# Using Filters in Tests

Filters can be used to specify a sub-selection of rows in Runs you would like to be tested.

For example, you might want to create a test that asserts that the absolute difference of means of the correct churn predictions is <= 0.2 between Baseline and Experiment Runs, **only for rows where the `loc` column is `NY`**.

## Apply a Filter to a Test

{% tabs %}
{% tab title="UI" %}
Navigate to the[ Create Test page](/v0.23.x/using-distributional/tests/creating-tests.md#creating-tests-manually) and create the test with the filter specified on the baseline and experiment run.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdOHWNqmXs7InU7g3n3PEFznh2rwUPc9ML937vRLCAHUwuO0iJnrfcU-helDES7uZsJn3Agzy7igOHR7k5D-afGkJBQsgB91MjtRpEEmqPql8P35ezRIVGCWEWRPz5A8Efa3sZdkAnAQ4fuXoTd80KPrf2A?key=rITDctX4nVqKraPSKOD6NQ" alt=""><figcaption></figcaption></figure>

Filter for the baseline Run:

```
equal_to({BASELINE}.loc, 'NY')
```

Filter for the experiment Run:

```
equal_to({EXPERIMENT}.loc, 'NY')
```

{% endtab %}

{% tab title="SDK" %}
You can create a test with filters in the SDK via the [`create_test`](/v0.23.x/reference/python-sdk/dbnl.experimental.md#create_test) function:

```python
import dbnl
dbnl.login()

proj = dbnl.get_or_create_project(name="My Project")

dbnl.experimental.create_test(
    test_spec_dict={
        "project_id": proj.id,
        "name": "abs diff of mean of correct churn preds of NY users is within 0.2",
        "statistic_name": "abs_diff_mean",
        "statistic_params": {},
        "assertion": {
            "name": "less_than_or_equal_to",
            "params": {
                "other": 0.2
            },
        },
        "statistic_inputs": [
            {
                "select_query_template": {
                    "select": "{BASELINE}.pred_correct",
                    "filter": "equal_to({BASELINE}.loc, 'NY')"
                }
            },
            {
                "select_query_template": {
                    "select": "{EXPERIMENT}.pred_correct",
                    "filter": "equal_to({EXPERIMENT}.loc, 'NY')"
                }
            },
        ],
    }
)
```

{% endtab %}
{% endtabs %}

Once you've used one of the methods above, you can now see the new test in the Test Configuration tab of your Project.

<figure><img src="/files/bGS8ZfimBx0spZVMNL3r" alt=""><figcaption></figcaption></figure>

When a Test Session is created, this test will use the defined filters to sub-select for the rows that have the `loc` column equal to `NY`.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfOxEhCjs_BPRsJe0nRQbO3Rn1DClwuqyRRpuaTp9QVrpDW7qMLj93Xy8w4jpFilcAXBsv2F4KEiWp2VKEC7SlPiY61fvdeV57ss_HxcWCon_6RkTMVcEbLCg5fXPc9FLykRPRdAxx8xe5_Z7qTmENtHqwv?key=rITDctX4nVqKraPSKOD6NQ" alt=""><figcaption></figcaption></figure>
