# Using parameters in calculated fields


A parameter is a variable that can substitute constant values in calculated fields.

You will learn how to manage visualizations using parameters:

* Create parameters at the dataset level and chart level.
* Work with formulas and visualizations in charts using parameters.
* Edit a parameter value on a dashboard using selectors with manual input.

You will use a direct connection to a demo ClickHouse® database as your data source.


To visualize and explore data, [set up DataLens](#before-you-begin) and follow the steps below:


1. [Create a connection](#create-connection).
1. [Create a dataset](#create-dataset).
1. [Add parameters to the dataset](#create-dataset-parameters).
1. [Create a chart with configurable grouping by date](#create-chart-grouping).
1. [Create a chart with a configurable visualization dimension](#create-chart-measure-select).
1. [Create a dashboard](#create-dashboard).
1. [Add charts to the dashboard](#add-charts-on-dashboard).
1. [Add selectors to the dashboard](#add-selectors-on-dashboard).


## Getting started {#before-you-begin}

To get started with DataLens:


{% list tabs group=datalens_user %}

- New user {#new}

  1. [Log in](https://passport.yandex.com/auth) to your Yandex account. If you do not have an account, [create](https://yandex.ru/support/passport/authorization/registration.html) one.
  1. Open the DataLens [home page](https://datalens.ru/promo).
  1. Click **Start in cloud**.
  1. Confirm that you have read the [Terms of use](https://yandex.com/legal/cloud_termsofuse/?lang=en) and click **Log in**.

- I am already using Yandex Cloud {#already}

  1. [Log in](https://passport.yandex.com/auth) to your Yandex account.
  1. Open the DataLens [home page](https://datalens.ru/promo).
  1. Click **Start in cloud**.
  1. Select one of these options:

     * If you already have an organization, select it from the drop-down menu in the **Organizations** tab and click **DataLens**.

       {% note info %}

       To activate a DataLens instance, the user must have the `admin` or `owner` role. For more information about roles, see [Access management in Yandex Identity Hub](../../organization/security/index.md).

       {% endnote %}

     * If you have a cloud but no organization, click **Add new DataLens**. In the window that opens, enter a name and description for your organization and click **Create organization and DataLens**. For more information about working with organizations, see [Getting started with organizations](../../organization/quickstart.md).

{% endlist %}

If you encounter a technical issue with the service, contact Yandex Cloud [support](https://center.yandex.cloud/support). To ask for advice, discuss a solution to your issue, or explore current best practices, join the [DataLens](https://t.me/YandexDataLens) chat in Telegram.


## Create a connection {#create-connection}

{% note warning %}

If you use the new DataLens object model with [workbooks and collections](../workbooks-collections/index.md):

1. Go to the DataLens [home page](https://datalens.ru/?skipPromo=true). In the left-hand panel, select ![collections](../../_assets/console-icons/rectangles-4.svg) **Collections and workbooks**.
1. Open the workbook, click **Create** in the top-right corner, and select the object you need.

Proceed with this guide from Step 3.

{% endnote %}



1. Go to the [connections page](https://datalens.ru/connections).
1. Click **Create connection**.



1. Select the **ClickHouse** connection.

   
   1. In the window that opens, specify the connection parameters:

      * Connection type: **Specify manually**.
      * Host name: `rc1a-ckg8nrosr2lim5iz.mdb.yandexcloud.net`.
      * HTTP interface port: `8443` (default).
      * Username: `samples_ro`.
      * Password: `MsgfcjEhJk`.

    1. Enable **Allow subqueries in datasets and queries from charts**.
    1. Check the connection and click **Create connection**.
    1. Enter the connection name: `Sample ClickHouse`.
    1. Click **Create**.

      
      ![create-connection](../../_assets/datalens/sql-chart/create-sample-connection-sql-chart.png)



Wait for the connection to be saved.

## Create a dataset {#create-dataset}

1. In the top-right corner, click **Create dataset**.

1. Drag the **MS_SalesFullTable** table to the workspace.

   ![image](../../_assets/datalens/solution-parameters/add-table.png)

1. Navigate to the **Fields** tab.

   ![image](../../_assets/datalens/solution-parameters/dataset-field-tab.png)

1. Create an order date field named **OrderDate**.

   1. Duplicate the **OrderDatetime** field.

      ![image](../../_assets/datalens/solution-parameters/dataset-dublicate-field.png)

   1. Rename the **OrderDatetime (1)** duplicate field to **OrderDate**: click the row name, delete the current name, and enter the new one.
   1. Change the data type from **Date and time** to **Date**.

      ![image](../../_assets/datalens/solution-parameters/dataset-rename-field.png)

1. In the **Aggregation** column, select **Sum** for the **Sales** field.

   ![image](../../_assets/datalens/solution-parameters/dataset-choose-agg.png)

   The aggregation field will become a measure and change its color to blue.

1. In the top-right corner, click **Save**.
1. Enter **Moscow Sales dataset with parameters** for the dataset name, then click **Create**.

## Add parameters to the dataset {#create-dataset-parameters}

1. Go to the **Parameters** tab.

   ![image](../../_assets/datalens/solution-parameters/dataset-parameters-tab.png)

1. Add the `scale` and `dimension` parameters:

   1. Click **Add**. Then, in the window that opens, enter:

      * **Name**: `scale`.
      * **Type**: `String`.
      * **Default value**: `month`.

      ![image](../../_assets/datalens/solution-parameters/dataset-add-parameter.png)

      Click **Add**.

   1. Click **Add**. Then, in the window that opens, enter:

      * **Name**: `dimension`.
      * **Type**: `String`.
      * **Default value**: `Status`.

      Click **Add**.

1. Click **Save**.

## Create a chart with configurable grouping by date {#create-chart-grouping}

1. In the top-right corner of the dataset, click **Create chart**.
1. Select **Area chart** as the visualization type.

   ![image](../../_assets/datalens/solution-parameters/select-area-chart.png)

1. Add a calculated field to the chart:

   1. On the left of the screen, click ![image](../../_assets/console-icons/plus.svg) under the dataset and select **Field**.
   1. In the **Field settings** window, enter the following:

      * In the **Name** field, `Date`.
      * In the formula field, `DATETRUNC([OrderDate], [scale])`. The order date will be rounded depending on the value of the `scale` dataset parameter.

        ![image](../../_assets/datalens/solution-parameters/chart-add-field.png)

   1. Click **Create**.
   1. Drag the new **Date** field to the **X** section.

1. Drag the **Sales** measure to the **Y** section.
1. Add grouping by product category to the chart. To do this, drag the **ProductCategory** field from **Dimensions** to the **Colors** section. The chart areas mapping to each product category will get different color fills.
1. Save the chart.

   1. In the top-right corner, click **Save**.
   1. Enter **Sales by product category** for the chart name, then click **Save**.

      ![image](../../_assets/datalens/solution-parameters/save-chart-grouping.png)

## Create a chart with a configurable visualization dimension {#create-chart-measure-select}

1. In the top-right corner of the dataset, click **Create chart**.
1. For the visualization type, select **Line chart**.
1. Add a calculated field for selecting a dimension to the chart:

   1. On the left of the screen, click ![image](../../_assets/console-icons/plus.svg) under the dataset and select **Field**.
   1. In the **Field settings** window, enter the following:

      * In the **Name** field, `Field`.
      * In the formula field:

        ```
        if(
             [dimension] = 'Status', [ClientStatus],
             [dimension] = 'Category', [ProductCategory],
             [dimension] = 'Payment type', [PaymentType],
             null
           )
        ```

        The visualized dimensions will change depending on the value of the `dimension` dataset parameter.

   1. Click **Create**.
   1. Drag the new **Field** field to the **Y** section.

1. Drag the **Sales** measure to the **X** section.
1. Save the chart.

   1. In the top-right corner, click **Save**.
   1. Enter **Chart with a configurable visualization dimension** for the chart name, then click **Save**.

      ![image](../../_assets/datalens/solution-parameters/save-chart-measure-select.png)

## Create a dashboard {#create-dashboard}

Create a [dashboard](../concepts/dashboard.md) for the charts.

1. In the left-hand panel, click ![image](../../_assets/console-icons/layout-cells-large.svg) **Dashboards**.
1. Click **Create dashboard**.

## Add charts to the dashboard {#add-charts-on-dashboard}

1. The first time you open the dashboard after saving, it opens in edit mode. If you open it later, click **Edit** in the top-right corner.
1. Click **Add** and select **Chart**.

   ![image](../../_assets/datalens/solution-parameters/add-chart.png)

1. In the **Chart** field, click **Select**.
1. Select the **Sales by product category** chart. The **Name** field will be populated automatically.
1. Click **Add**.

   ![image](../../_assets/datalens/solution-parameters/add-chart-window.png)

1. Similarly, add **Chart with a configurable visualization dimension**.
1. Position the charts on the dashboard however you prefer.
1. Save the dashboard:

   1. In the top-right corner, click **Save**.
   1. Enter **Visualizing data using parameters** for the dashboard name and click **Create**.

## Add selectors to the dashboard {#add-selectors-on-dashboard}

Add [selectors](../dashboard/selector.md) with manual input to edit values in the charts.

{% note warning %}

After adding a selector, unlink it from other selectors in this dashboard tab.

{% endnote %}

1. To switch to dashboard editing mode, click **Edit** in the top-right corner.
1. Add a selector for the **Sales by product category** chart:

   1. At the top of the page, click **Add**.
   1. Choose **Selector**.

      ![image](../../_assets/datalens/solution-parameters/add-selector.png)

   1. Select **Manual input** as the source type.
   1. In **Field or parameter name**, enter `scale`. The selected selector value will be provided to this dataset parameter.
   1. Select **List** as your selector type.
   1. Click the input field next to the **Default value** parameter and add these values:

      * `day`
      * `week`
      * `month`
      * `year`

      ![image](../../_assets/datalens/solution-parameters/add-selector-values.png)

      Click **Apply**.

   1. In the **Default value** list, specify **month**.
   1. Enable the **Name** option and enter `Grouping`.
   1. Check the specified selector parameters.

      ![image](../../_assets/datalens/solution-parameters/add-selector-parameters.png)

   1. Click **Add**.
   1. Place the selector on the dashboard above the **Sales by product category** chart. Now you can manage grouping by date in this chart.

      ![image](../../_assets/datalens/solution-parameters/chart-grouping-w.png)

      ![image](../../_assets/datalens/solution-parameters/chart-grouping-y.png)

1. Add a selector for **Chart with a configurable visualization dimension**:

   1. At the top of the page, click **Add**.
   1. Choose **Selector**.
   1. Select **Manual input** as the source type.
   1. In **Field or parameter name**, enter `dimension`. The selected selector value will be provided to this dataset parameter.
   1. Select **List** as your selector type.
   1. Click the input field next to the **Default value** parameter and add these values:

      * `Status`
      * `Category`
      * `Payment type`

      Click **Apply**.

   1. In the **Default value** list, specify **Status**.
   1. Enable the **Name** option and enter `Dimension`.
   1. Click **Add**.
   1. Place the selector on the dashboard above **Chart with a configurable visualization dimension**.
   1. Unlink this selector from the **Grouping** selector:

      1. Click ![image](../../_assets/console-icons/gear.svg) at the top of the screen. The settings window will open on the right.
      1. In the settings window, click **Links** ![image](../../_assets/console-icons/sliders-vertical.svg).
      1. In the **Links** window, select **Dimensions**.
      1. Make sure that you selected the **Outgoing link** link type for the **Chart with a configurable visualization selection**. For the **Grouping** selector, specify the link type **Ignore**.

         ![image](../../_assets/datalens/solution-parameters/alias-ignor.png)

      1. Click **Save**. Now, you can use this selector to select a dimension for visualization.

         ![image](../../_assets/datalens/solution-parameters/chart-measure-select-cat.png)

         ![image](../../_assets/datalens/solution-parameters/chart-measure-select-pay.png)

#### See also {#see-also}

* [Parameters in Yandex DataLens](../concepts/parameters.md)
* [Adding parameters to a dashboard in Yandex DataLens](../operations/dashboard/add-parameters.md)
* [Adding a parameter to a chart in Yandex DataLens](../operations/chart/add-parameter-chart.md)
* [Adding parameters](../dataset/create-dataset.md#add-parameters)

_ClickHouse® is a registered trademark of [ClickHouse, Inc](https://clickhouse.com)._