# 2. Queries

[Queries](https://docs.tukanmx.com/queries) let you filter and aggregate data from a table.

With queries, you can sift through the dataset and pull out only the data you need, exactly how you need it.

What's more, **queries** come in handy when you want to **automate** reports or dashboards that use the same data over and over. That way, you don't have to stress about updating your spreadsheets or dashboards every time there's new info.

{% hint style="success" %}
**If you've already created a query, and wish to learn about how you can connect tukan to your favorite tools;** [**click here**](https://docs.tukanmx.com/queries/connecting-to-a-query)**.**
{% endhint %}

***

### Creating your first query

Let's create our first query by using the *Mexican CPI* as an example.

#### Step 1: selecting the dataset

In the [explore](https://app.tukanmx.com/explore) module of the tukan app, search for ***"Mexican CPI"***; this should filter all datasets realted to Mexican consumer prices.&#x20;

Next, select the ***"Query table"*** button to enter the query module for [this particular dataset](https://app.tukanmx.com/visualizations/queries/new?tableId=mex_inegi_inpc_subindex_monthly).

<figure><img src="https://1499304022-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKkVlcNPFyJ2f7v0nyyEo%2Fuploads%2FSLXXqR5i4IPcMBGPNB8a%2Fimage.png?alt=media&#x26;token=1e9c4f22-4290-4ce3-80c7-525763298666" alt=""><figcaption><p>Querying a table.</p></figcaption></figure>

#### Step 2: The query module

Once you've clicked on the button, you'll be taken to the query module.&#x20;

Here you can select the indicators you wish to export, and assign filters to the data's columns (or references). You'll also be shown a *preview* of the data you'r collecting.

> *If you're on a small screen (or have lots of zoom) the preview will be shown below the filters & indicators panel*.

<figure><img src="https://1499304022-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKkVlcNPFyJ2f7v0nyyEo%2Fuploads%2FIdSgqom3JvGLImx1BaPt%2Fimage.png?alt=media&#x26;token=4a0139e7-f76d-4ae8-9cdc-b39e385b02f6" alt=""><figcaption><p>Query module.</p></figcaption></figure>

In this case, let's select an indicator from the dataset and perform a filter to get historical data.

For example, in the date filter, we select ***from*** to get data since 2014. This ensures you get data since that particular date all the way up to the most recent values.

> *In the indicator field, we select CPI (the consumer price index), which in this case is the only indicator present in the dataset.*

<figure><img src="https://1499304022-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKkVlcNPFyJ2f7v0nyyEo%2Fuploads%2FSj3wjISt0932Zo7aSm5g%2Fadd_indicators_query.gif?alt=media&#x26;token=fd51fafc-413e-4ee6-86a2-275f2e546a46" alt=""><figcaption><p>Selecting indicators and date filters.</p></figcaption></figure>

As you can see from the previous screenshot, a preview of the data is immediately shown on the right panel. This is how your data will look like once you export it.

#### Step 3: Adding more filters

As we discussed, tables can have references, which are additional attributes to the data.

For this example, our CPI dataset has a reference called the **CPI Subindex Structure** which divides the CPI based on the goods & services consumed by Mexicans.

If we are interested in only some *subindices*, we can add an additional filter to our query.

<figure><img src="https://1499304022-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKkVlcNPFyJ2f7v0nyyEo%2Fuploads%2FiYj5vyy6N4OxqLPo10SB%2Fadd_filter_query.gif?alt=media&#x26;token=ca2049aa-28ae-4015-bed0-a69d2060de6c" alt=""><figcaption><p>Adding reference filters to a query.</p></figcaption></figure>

Finally, we can export the data to Excel or as a CSV file; or save our query for future reference.

<figure><img src="https://1499304022-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKkVlcNPFyJ2f7v0nyyEo%2Fuploads%2FEJ0edpTI21UAPPKNkwVV%2Fexport%20query.gif?alt=media&#x26;token=e9c038e3-709b-4f59-b7bd-b0820e713ffd" alt=""><figcaption><p>Downloading a query.</p></figcaption></figure>

#### Step 4: Saving a query

The great thing about tukan is that it's desgined to help you automate the collection of public data; which means that your queries can be saved for future use and can even be integrated directly to your Python scripts, Excel workbooks and Power BI dashboards.

To save a query, simply assign a name and click save. That's it.

<figure><img src="https://1499304022-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKkVlcNPFyJ2f7v0nyyEo%2Fuploads%2FFblK41KnSo8cW0c8ADam%2Fsave_query.gif?alt=media&#x26;token=c3e02d50-ef0f-4538-af5d-9573beed1339" alt=""><figcaption><p>Saving a query.</p></figcaption></figure>

For detailed connection guides to each of these tools, please [click here](https://docs.tukanmx.com/queries/connecting-to-a-query).
