# Data Set Filters

We provide a way to filter data sets dynamically, to affect all the charts that use a specific data set. The filter is a component in itself so it will be using an order, and it will need to be passed an exiting shared data set by name.

The function to use is the following (using `s` as the Shimoku client):

```python
s.plt.filter(
    order: int,                   # Order in the view
    data: str,                    # The name of the shared data set
    field: str,                   # The field from the data set to be filtered
    multi_select: bool = False,   # Wether to use the multi select for categoricals
    cols_size: int = 4,           # The number columns that will occupy the filter
    rows_size: int = 1,           # The number of rows that will occupy the filter
    padding: Optional[str] = None # The padding that will be applie to the filter
 )
```

The filter works with 4 types of inputs, for different types of values, this will be inferred by the field to be filtered. The types are:

* Numeric values (it lets the user chose the operation):  <img src="/files/5jG2HVbEYYBJeIbxRmGd" alt="" data-size="original">
* Date values:                                                                      ![](/files/6C97ahGTZd3fgCA0fGDn)
* Categorical values (single or multi select):                    ![](/files/bMjAbDPU1MaYrDWpBGOI)

For an example we will be using this data set:

```python
data_to_filter = [
    {'date': dt.date(2021, 1, 1), 'x': 5, 'y': 5, 'filtA': 'A', 'filtB': 'Z', 'name': 'Ana'},
    {'date': dt.date(2021, 1, 2), 'x': 6, 'y': 5, 'filtA': 'B', 'filtB': 'Z', 'name': 'Laura'},
    {'date': dt.date(2021, 1, 3), 'x': 4, 'y': 5, 'filtA': 'A', 'filtB': 'W', 'name': 'Audrey'},
    {'date': dt.date(2021, 1, 4), 'x': 7, 'y': 5, 'filtA': 'B', 'filtB': 'W', 'name': 'Jose'},
    {'date': dt.date(2021, 1, 5), 'x': 3, 'y': 5, 'filtA': 'A', 'filtB': 'Z', 'name': 'Jorge'},
]
```

and we will use three linked charts joined to this data set:

```python
s.reuse_data_sets()  # For later executions

s.plt.set_shared_data(dfs={'filters data': pd.DataFrame(data_to_filter)})

s.plt.bar(data='filters data', x='date', y='x', order=0, cols_size=6)
s.plt.doughnut(data='filters data', names='date', values='x', order=1, cols_size=6)
s.plt.stacked_area(data='filters data', x='date', y=['x', 'y'], order=2)
```

<figure><img src="/files/8lecmke13QJXzim2wFut" alt=""><figcaption><p>The charts to be filtered</p></figcaption></figure>

{% tabs %}
{% tab title="Numerical" %}
To use the numeric filter we will specify the `x` field as it has more variation than the `y` field:

```python
s.plt.filter(order=3, data='filters data', field='x')
```

The possible operations are the following:

&#x20;                                           <img src="/files/PxExfREmTOPDf8KYelmt" alt="" data-size="original">

If we set the operation to `>` and the value to `4` we get:

<figure><img src="/files/eEcafIu2MDX518NmSkFl" alt=""><figcaption><p>Numeric filter result</p></figcaption></figure>
{% endtab %}

{% tab title="Date Range" %}
To use the date range we will use the field `date:`

```python
s.plt.filter(order=3, data='filters data', field='date')
```

It lets the user select a range of dates, if we select the range `2021/01/1` - `2021/01/03`, the result is:

<figure><img src="/files/hqBwLpfmHl9ePPQWa2Ce" alt=""><figcaption><p>Date range filter result</p></figcaption></figure>
{% endtab %}

{% tab title="Categorical Single Select" %}
To use the categorical filter we will use the `filtA` field. The input is a select dropdown with all the possible options, only one can be selected.

If we set the filter to the value `A` the result is:

<figure><img src="/files/C8EzuzBPgbnTkFMUlTX6" alt=""><figcaption><p>Single select filter</p></figcaption></figure>
{% endtab %}

{% tab title="Categorical Multi Select" %}
To use the categorical multi select filter we will use the `filtB` field. The input is a select dropdown with all the possible options, any combination can be selected.

If we set all the values of the filter we get:

<figure><img src="/files/obKHwCcs0IIPShzh7Rhr" alt=""><figcaption><p>Multi select filter result</p></figcaption></figure>
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
It is not recommended to use more than one filter on a single data set, only one filter can have effect on a data set at a time, and this can be confusing for the user.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.shimoku.com/dev/elements/data-sets/data-set-filters.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
