Analyzing a store chain's sales based on data from a ClickHouse DB
- Before you start
- Step 1. Create a connection and a dataset
- Step 2. Configure dataset fields
- Step 3. Create a line chart
- Step 4. Create a column chart
- Step 5. Create a pivot table chart
- Step 6. Create a heat map chart
- Step 7. Create a dashboard
- Step 8. Add charts to the dashboard
- Step 9. Add selectors to the dashboard
As a data source, we'll use a demo ClickHouse database on sales in a Moscow chain of stores.
In this example, DataLens is used to visualize:
- Sales dynamics by day and month.
- Sales by product categories.
- Order heat map.
A connection named Sample ClickHouse will be created for database access.
To visualize and analyze data, set up DataLens and follow these steps:
- Define the data source for a dataset.
- Configure dataset fields.
- Create a line chart.
- Create a column chart.
- Create a pivot table chart.
- Create a heat map chart.
- Create a dashboard.
- Add charts to the dashboard.
- Add selectors to the dashboard.
Before you start
To get started with DataLens:
-
Log in to your Yandex account.
-
Open the homepage DataLens.
-
Click Open Datalens.
-
Select one of the options:
-
If you already have an organization, select it from the drop-down menu in the Organizations tab and click Open DataLens.
Note
To activate a DataLens instance, the user must have the
admin
orowner
role. For more information about roles, see Granting permissions. -
If you have a cloud but no organization, click Add new DataLens. In the window that opens, enter your organization's name and description and click Create organization and DataLens. For more information about working with organizations, see Getting started with organizations.
-
If you have a technical question about the service, please contact support. To ask for advice or discuss the solution to your problem or best service practices, write to the DataLens chat in Telegram.
Step 1. Create a connection and a dataset
-
Go to the connections page.
-
Click Create connection.
-
Select the ClickHouse connection.
-
In the window that opens, specify the connection parameters:
-
Connection name:
Sample ClickHouse
. -
Connection type: Specify manually.
-
Host name:
rc1a-ckg8nrosr2lim5iz.mdb.yandexcloud.net
. -
HTTP interface port:
8443
(default). -
Username:
samples_ro
. -
Password:
MsgfcjEhJk
.
-
-
Enable the HTTPS and the Allow subqueries in datasets options.
-
Check the connection and click Create.
Wait for the connection to be saved.
-
-
In the upper-right corner, click Create dataset.
Step 2. Configure dataset fields
-
Drag the MS_SalesFacts table to the workspace.
-
Drag the MS_Clients table to the workspace. The tables are automatically linked.
-
To check the link, click the link icon between the tables.
-
Tables are linked by the ClientID field. If needed, you can modify or expand the link by specifying another pair of fields. To close the link settings window, click the cross button or anywhere outside the window.
-
Drag the MS_Products table to the workspace. The table is automatically linked to the left (root) table MS_SalesFacts.
-
Drag the MS_Shops table to the workspace. The table is automatically linked to the left (root) table MS_SalesFacts.
-
Go to the Fields tab.
-
Delete the duplicate fields left over from joining the tables: ClientID (1), ProductID (1), and ShopID (1).
-
Create an order date field named OrderDate.
-
Duplicate the OrderDatetime field.
-
Rename the OrderDatetime (1) duplicate field to OrderDate: click the row name, delete the current name, and enter the new one.
-
Change the data type from Date and time to Date.
-
-
For the ShopDistrictCoordinates field, change the data type to Geopolygon.
-
For the DeliveryDistrictCoordinates field, change the data type to Geopolygon.
-
For the DeliveryAddressCoord field, change the data type to Geopoint.
-
In the Aggregation column, select Sum for the Sales field.
The aggregation field changes color to blue: Sales is now a measure.
-
Create a measure for the number of orders.
-
Duplicate the OrderID field.
-
Rename the OrderID (1) duplicate field to OrderCount.
-
Change the aggregation type to Number of unique.
-
-
Create a calculated field for the average sales amount per order.
-
In the upper-right corner, click Add field.
-
In the Name field, specify Sales per Order.
-
In the column to the left, click on the Sales field.
-
Enter a
/
. -
In the column to the left, click the OrderCount field.
-
Click Create.
-
-
Click Save in the upper-right corner to save the dataset.
-
Enter a name for the dataset: Moscow Sales dataset, then click Create.
-
When the dataset is saved, click Create chart.
Step 3. Create a line chart
To visualize sales dynamics by month, create a chart: line chart.
-
For the visualization type, choose Line chart.
-
Add the sales date to the chart. Drag the OrderDate field from the Dimensions section to the X section.
-
Add a sales measure to the chart. To do this, drag the Sales field from the Measures section to the Y section.
-
Add the delivery type to the chart. To do this, drag the PaymentType field from the Dimensions to the Colors section.
-
Display the chart by month.
- Click the calendar icon next to the OrderDate field in the X section.
- In the Grouping field, select Rounding ⟶ Month and click Apply.
-
Save the chart.
-
Click Save in the upper-right corner to save the chart.
-
In the window that opens, enter a name for the chart: Sales dynamics by month and payment type, and click Save.
-
Step 4. Create a column chart
To visualize sales by brands and product categories, create a chart: column chart.
-
Copy the chart from the previous step.
-
Click the down arrow next to the Save button in the upper-right corner.
-
Choose Save as.
-
In the window that opens, enter the Sales by brand and category name for the new chart and click Save.
-
-
For the visualization type, choose Column chart.
-
The OrderDate, Sales, and PaymentType fields are automatically added to the X, Y, and Colors sections, respectively.
-
Replace the months with brands on the X-axis. From the Dimensions section, drag the ProductBrand field to the X section and hold it over the OrderDate field until it turns red.
-
Replace payment types for product categories in the Colors section. Drag ProductCategory from the Dimensions to the Colors section and hold it over the PaymentType field until it turns red.
-
Sort the chart in descending order of sales measure. Drag the Sales field from the Measures section to the Sorting section.
-
Save the chart.
Step 5. Create a pivot table chart
To visualize sales in terms of products and time, create a chart: pivot table.
-
Copy the chart from the previous step.
- Click the down arrow next to the Save button in the upper-right corner.
- Click Save as.
- In the window that opens, enter the name Sales by year and product for the new chart.
- Click Save.
-
Choose the Pivot table visualization type.
-
The ProductBrand and Sales fields are automatically copied to the Columns and Measures sections, respectively.
-
Remove ProductBrand from the table.
-
Add the order date to the table. To do this, drag the OrderDate field from the Dimensions section to the Columns section.
-
Change the display format in the OrderDate field to years.
- Click the calendar icon next to the OrderDate field in the Columns section.
- In the Grouping field, select Date part ⟶ Year and click Apply.
-
Add the product category and subcategory to the table. To do this, drag the ProductCategory and ProductSubcategory fields from the Dimensions section to the Rows section.
-
Change the color of the sales measure in the table. To do this, drag the Sales field from the Measures section to the Colors section.
-
Save the chart.
Step 6. Create a heat map chart
To visualize the density of orders on the Moscow map, create a chart: heat map.
-
Copy the chart from the previous step.
- Click the icon next to the Save button in the upper-right corner and select Save as.
- In the window that opens, enter the name Sales heat map for the new chart.
- Click Save.
-
Select the Map visualization type.
-
Select the Points (heat map) layer type.
-
Add the delivery point coordinates to the map. To do this, drag the DeliveryAddressCoord field from the Dimensions section to the Points (heat map) section.
-
Save the chart.
Step 7. Create a dashboard
Create a dashboard to add your charts to.
-
Go to the DataLens main page.
-
Click Create dashboard.
-
Enter the name Moscow Shops dashboard for the dashboard and click Create.
Step 8. Add charts to the dashboard
-
The first time you open the dashboard after saving, it opens in edit mode. If you open it later, click Edit in the upper-right corner.
-
Click Add and select Chart.
-
In the window that opens, click Select.
-
Select the chart Sales dynamics by month and payment type. This automatically fills in the Title field with the name of the selected chart.
-
Click Add.
-
Similarly, add the following charts:
- Sales by brand and category
- Sales by year and product
- Sales heat map
-
Position the charts on the dashboard however you like.
Step 9. Add selectors to the dashboard
Add selectors to filter the charts by date, Moscow districts, products, and customer statuses.
-
Click Add.
-
Choose Selector.
-
Add the calendar selector for the order date.
- Select the Moscow Sales dataset.
- Select the OrderDate field.
- This automatically fills in Title with the name of the selected field. Click the Show checkbox next to the selector title.
- Select the Calendar type.
- Enable Range.
- Click Add.
-
Add a product category selector.
- Select the Moscow Sales dataset.
- Select the ProductCategory field.
- This automatically fills in Title with the name of the selected field. Click the Show checkbox next to the selector title.
- Enable the Multiple choice option.
- Click Add.
-
Similarly, add selectors for the following fields:
- ProductBrand
- DeliveryDistrictName
- DeliveryType
- PaymentType
-
Position the selectors on the dashboard however you like.
-
Save the dashboard.
-
Your dashboard is ready. Now you can filter charts using selectors.