[[create-a-dashboard-of-panels-with-ecommerce-data]]
== Analyze time series data

In this tutorial, you'll use the ecommerce sample data to analyze sales trends, but you can use any type of data to complete the tutorial.

When you're done, you'll have a complete overview of the sample web logs data. 

[role="screenshot"]
image::images/lens_timeSeriesDataTutorialDashboard_8.3.png[Final dashboard with ecommerce sample data]

Before you begin, you should be familiar with the <<kibana-concepts-analysts>>.

[discrete]
[[add-the-data-and-create-the-dashboard-advanced]]
=== Add the data and create the dashboard

Add the sample ecommerce data, and create and set up the dashboard.

. On the home page, click *Try sample data*.

. Click *Other sample data sets*. 

. On the *Sample eCommerce orders* card, click *Add data*.

Create the dashboard where you'll display the visualization panels.

. Open the main menu, then click *Dashboard*.

. On the *Dashboards* page, click *Create dashboard*.

[float]
[[open-and-set-up-lens-advanced]]
=== Open and set up the visualization editor

Open the visualization editor, then make sure the correct fields appear.

. On the dashboard, click *Create visualization*.

. Make sure the *Kibana Sample Data eCommerce* {data-source} appears, then set the <<set-time-filter,time filter>> to *Last 30 days*.

[discrete]
[[custom-time-interval]]
=== Create visualizations with custom time intervals

When you create visualizations with time series data, you can use the default time interval, or increase and decrease the interval. For performance reasons, the visualization editor allows you to choose the minimum time interval, but not the exact time interval. The interval limit is controlled by the <<histogram-maxbars, `histogram:maxBars`>> setting and <<set-time-filter,time range>>. 

To analyze the data with a custom time interval, create a bar chart that shows you how many orders were made at your store every hour:

. From the *Available fields* list, drag *Records* to the workspace.
+
The visualization editor creates a bar chart.

. To zoom in on the data, click and drag your cursor across the bars. 
+
[role="screenshot"]
image::images/lens_clickAndDragZoom_7.16.gif[Cursor clicking and dragging across the bars to zoom in on the data]

. In the layer pane, click *Count of Records*.

.. Click *Advanced*. 

.. From the *Normalize by unit* dropdown, select *per hour*, then click *Close*.
+
*Normalize by unit* converts `Count of Records` into `Count of records per hour` by dividing by 24.

.. In the *Name* field, enter `Number of orders`.

.. Click *Close*.

. To hide the *Horizontal axis* label, open the *Bottom Axis* menu, then select *None* from the *Axis title* dropdown.

To identify the 75th percentile of orders, add a reference line:

. In the layer pane, click *Add layer > Reference lines*.

. Click *Static value*.

.. Click *Quick function*, then click *Percentile*.

.. From the *Field* dropdown, select *total_quantity*.

.. In the *Reference line value* field, enter `75`.

. Configure the *Appearance* options.

.. In the *Name* field, enter `75th`.

.. To display the name, select *Name* from *Text decoration*.

.. From the *Icon* dropdown, select *Tag*.

.. In the *Color* field, enter `#E7664C`.

. Click *Close*.
+
[role="screenshot"]
image::images/lens_barChartCustomTimeInterval_8.3.png[Orders per day]

. Click *Save and return*.

[discrete]
[[add-a-data-layer-advanced]]
=== Analyze multiple data series

You can create visualizations with multiple data series within the same time interval, even when the series have similar configurations with minor differences.

To analyze multiple series, create a line chart that displays the price distribution of products sold over time:

. On the dashboard, click *Create visualization*.

. Open the *Visualization type* dropdown, then select *Line*.

. From the *Available fields* list, drag *products.price* to the workspace.

Create the 95th price distribution percentile:

. In the layer pane, click *Median of products.price*.

. Click the *Percentile* function.

. In the *Name* field, enter `95th`, then click *Close*.

To copy a function, you drag it to the *Drop a field or click to add* field within the same group. To create the 90th percentile, duplicate the `95th` percentile:

. Drag the *95th* field to *Add or drag-and-drop a field* for *Vertical axis*.
+
[role="screenshot"]
image::images/lens_advanced_2_2.gif[Easily duplicate the items with drag and drop]

. Click *95th [1]*, then enter `90` in the *Percentile* field.

. In the *Name* field enter `90th`, then click *Close*.

. To create the `50th` and `10th` percentiles, repeat the duplication steps.

. Open the *Left Axis* menu, select *Custom* from the *Axis title* dropdown, then enter `Percentiles for product prices` in the *Axis title* field.
+
[role="screenshot"]
image::images/lens_lineChartMultipleDataSeries_7.16.png[Percentiles for product prices chart]

. Click *Save and return*.

[discrete]
[[add-a-data-layer]]
=== Analyze multiple visualization types

With layers, you can analyze your data with multiple visualization types. When you create layered visualizations, match the data on the horizontal axis so that it uses the same scale. 

To analyze multiple visualization types, create an area chart that displays the average order prices, then add a line chart layer that displays the number of customers. 

. On the dashboard, click *Create visualization*.

. From the *Available fields* list, drag *products.price* to the workspace.

. In the layer pane, click *Median of products.price*.

.. Click the *Average* function.

.. In the *Name* field, enter `Average price`, then click *Close*.

. Open the *Visualization type* dropdown, then select *Area*.

Add a layer to display the customer traffic:

. In the layer pane, click *Add layer > Visualization*.

. From the *Available fields* list, drag *customer_id* to the *Vertical Axis* field in the second layer.

. In the layer pane, click *Unique count of customer_id*.

.. In the *Name* field, enter `Number of customers`.

.. In the *Series color* field, enter `#D36086`.

.. Click *Right* for the *Axis side*, then click *Close*.
+
image::images/lens_advancedTutorial_numberOfCustomers_8.5.0.png[Number of customers area chart in Lens]

. From the *Available fields* list, drag *order_date* to the *Horizontal Axis* field in the second layer.

. In the second layer, open the *Layer visualization type* menu, then click *Line*.
+
[role="screenshot"]
image::images/lens_layerVisualizationTypeMenu_7.16.png[Layer visualization type menu]

. To change the position of the legend, open the *Legend* menu, then select the *Alignment* arrow that points up.
+
[role="screenshot"]
image::images/lens_mixedXYChart_7.16.png[Layer visualization type menu]

. Click *Save and return*.

[discrete]
[[percentage-stacked-area]]
=== Compare the change in percentage over time

By default, the visualization editor displays time series data with stacked charts, which show how the different document sets change over time. 

To view change over time as a percentage, create an *Area percentage* chart that displays three order categories over time:

. On the dashboard, click *Create visualization*.

. From the *Available fields* list, drag *Records* to the workspace.

. Open the *Visualization type* dropdown, then select *Area percentage*.

For each order category, create a filter: 

. In the layer pane, click *Add or drag-and-drop a field* for *Breakdown*.

. Click the *Filters* function.

. Click *All records*, enter the following in the query bar, then press Return:

* *KQL* &mdash; `category.keyword : *Clothing`

* *Label* &mdash; `Clothing`

. Click *Add a filter*, enter the following in the query bar, then press Return:

* *KQL* &mdash; `category.keyword : *Shoes`

* *Label* &mdash; `Shoes`

. Click *Add a filter*, enter the following in the query bar, then press Return:

* *KQL* &mdash; `category.keyword : *Accessories`

* *Label* &mdash; `Accessories`

. Click *Close*.

. Open the *Legend* menu, then select the *Alignment* arrow that points up.
+
[role="screenshot"]
image::images/lens_areaPercentageNumberOfOrdersByCategory_8.3.png[Prices share by category]

. Click *Save and return*.

[discrete]
[[view-the-cumulative-number-of-products-sold-on-weekends]]
=== View the cumulative number of products sold on weekends

To determine the number of orders made only on Saturday and Sunday, create an area chart, then add it to the dashboard.

. On the dashboard, click *Create visualization*.

. Open the *Visualization type* dropdown, then select *Area*.

Configure the cumulative sum of store orders:

. From the *Available fields* list, drag *Records* to the workspace.

. In the layer pane, click *Count of Records*.

. Click the *Cumulative sum* function.

. In the *Name* field, enter `Cumulative weekend orders`, then click *Close*.

Filter the results to display the data for only Saturday and Sunday:

. In the layer pane, click *Add or drag-and-drop a field* for *Breakdown*. 

. Click the *Filters* function.

. Click *All records*, enter the following in the query bar, then press Return:

* *KQL* &mdash; `day_of_week : "Saturday" or day_of_week : "Sunday"`

* *Label* &mdash; `Saturday and Sunday`
+
The <<kuery-query,KQL filter>> displays all documents where `day_of_week` matches `Saturday` or `Sunday`.

. Open the *Legend* menu, then click *Hide* next to *Display*.
+
[role="screenshot"]
image::images/lens_areaChartCumulativeNumberOfSalesOnWeekend_7.16.png[Area chart with cumulative sum of orders made on the weekend]

. Click *Save and return*.

[discrete]
[[compare-time-ranges]]
=== Compare time ranges

With *Time shift*, you can compare the data from different time ranges. To make sure the data correctly displays, choose a multiple of the date histogram interval when you use multiple time shifts. For example, you are unable to use a *36h* time shift for one series, and a *1d* time shift for the second series if the interval is *days*.   

To compare two time ranges, create a line chart that compares the sales in the current week with sales from the previous week: 

. On the dashboard, click *Create visualization*.

. Open the *Visualization type* dropdown, then select *Line*.

. From the *Available fields* list, drag *Records* to the workspace.

. To duplicate *Count of Records*, drag *Count of Records* to *Add or drag-and-drop a field* for *Vertical axis* in the layer pane.

To create a week-over-week comparison, shift *Count of Records [1]* by one week:

. In the layer pane, click *Count of Records [1]*.

. Click *Advanced*, select *1 week ago* from the *Time shift* dropdown, then click *Close*.
+
To use custom time shifts, enter the time value and increment, then press Enter. For example, enter *1w* to use the *1 week ago* time shift.
+
[role="screenshot"]
image::images/lens_time_shift.png[Line chart with week-over-week sales comparison]

. Click *Save and return*.

Time shifts can be used on any metric. The special shift *previous* will show the time window preceding the currently selected one in the time picker in the top right, spanning the same duration.
For example, if *Last 7 days* is selected in the time picker, *previous* will show data from 14 days ago to 7 days ago. This mode can't be used together with date histograms.

[float]
[[compare-time-as-percent]]
==== Analyze the percent change between time ranges

With *Formula*, you can analyze the percent change in your data from different time ranges.

To compare time range changes as a percent, create a bar chart that compares the sales in the current week with sales from the previous week: 

. On the dashboard, click *Create visualization*.

. From the *Available fields* list, drag *Records* to the workspace.

. In the layer pane, click *Count of Records*.

. Click *Formula*, then enter `count() / count(shift='1w') - 1`.

. In the *Name* field, enter `Percent of change`.

. From the *Value format* dropdown, select *Percent*, then enter `0` in the *Decimals* field.

. Click *Close*.
+
[role="screenshot"]
image::images/lens_percent_chage.png[Bar chart with percent change in sales between the current time and the previous week]

. Click *Save and return*.

[discrete]
[[view-customers-over-time-by-continents]]
=== Analyze the data in a table

With tables, you can view and compare the field values, which is useful for displaying the locations of customer orders.

Create a date histogram table and group the customer count metric by category, such as the continent registered in user accounts:

. On the dashboard, click *Create visualization*.

. Open the *Visualization type* dropdown, then select *Table*.

. From the *Available fields* list, drag *customer_id* to the *Metrics* field in the layer pane.

.. In the layer pane, click *Unique count of customer_id*.

.. In the *Name* field, enter `Customers`, then click *Close*.

. From the *Available fields* list, drag *order_date* to the *Rows* field in the layer pane.

.. In the layer pane, click the *order_date*.

.. In the *Minimum interval* field, enter *1d*.

.. In the *Name* field, enter `Sales`, then click *Close*.

To split the metric, add columns for each continent using the *Columns* field:

. From the *Available fields* list, drag *geoip.continent_name* to the *Split metrics by* field in the layer pane.
+
[role="screenshot"]
image::images/lens_table_over_time.png[Date histogram table with groups for the customer count metric]

. Click *Save and return*.

[discrete]
=== Save the dashboard

Now that you have a complete overview of your ecommerce sales data, save the dashboard.

. In the toolbar, click *Save*.

. On the *Save dashboard* window, enter `Ecommerce sales`, then click *Save*.

. Select *Store time with dashboard*.

. Click *Save*.

[role="screenshot"]
image::images/lens_timeSeriesDataTutorialDashboard_8.3.png[Final dashboard with ecommerce sample data]
