# From a Local Excel File to NL2SQL Questions

Import an Excel workbook into Catalog, add the resulting table to a knowledge base, and ask questions in English. MOI generates SQL, queries the table, and summarizes the results.

This tutorial uses Microsoft's public Financial Sample workbook, which includes countries, products, sales, profit, and dates.

## Before you begin

- Sign in to [MOI](https://moi.matrixorigin.cn/) and use the English interface.
- Download [Financial Sample.xlsx](https://download.microsoft.com/download/1/4/E/14EDED28-6C58-4055-A65C-23B4DA81C4DE/Financial%20Sample.xlsx).

## 1. Import the Excel workbook

1. Open **Data**. Under **Data Connection**, open **Import Tasks** and click **New Import Task**.
2. Select **Structured**, keep **Load Type** set to **File**, and select **Local Upload**.
3. Upload `Financial Sample.xlsx` and select **Sheet1**. The worksheet contains 701 rows, including the header.
4. Under **Load Target**, select **New Table** and choose a Catalog and database. This example uses Catalog `the_first_tutorial`, database `excel`, and table `sheet1`. Use your own Catalog name if it differs.
5. In **Table Definition**, set **Column Name row** to `1` and **Start from row** to `2`. Review the column names and types before importing. In particular, check how sales, profit, and year values will be stored.

```{image} ../assets/images/tutorials/excel-nl2sql-import-config-en.png
:alt: Financial Sample workbook with Sheet1 selected and a new table in the excel database
```

6. Click **Create & Start Loading**. Wait until the task status becomes **Finished**.

```{image} ../assets/images/tutorials/excel-nl2sql-import-finished-en.png
:alt: Financial Sample import task marked Finished with target the_first_tutorial / excel / sheet1
```

## 2. Create a knowledge base

1. Under **Resource Center**, open **Knowledge Base** and click **Create Knowledge Base**.
2. Enter `financial_sales_knowledge` as the **Knowledge Base Name**. The **Remark** is optional.
3. Click **Finish** to open the new knowledge base.

```{image} ../assets/images/tutorials/excel-nl2sql-create-knowledge-en.png
:alt: Creating the financial_sales_knowledge knowledge base
```

## 3. Add the imported table

1. On the knowledge base's **Data Source** tab, click **Add Data**.
2. Open your Catalog, select the `excel` database, and open **Tables**.
3. Select `sheet1` and click **Append**.

```{image} ../assets/images/tutorials/excel-nl2sql-add-table-en.png
:alt: Selecting sheet1 from the excel database and appending it to the knowledge base
```

4. Confirm that the data source lists `sheet1` with **700** rows and a **Completed** processing status.

```{image} ../assets/images/tutorials/excel-nl2sql-table-completed-en.png
:alt: Knowledge base data source showing sheet1 with 700 rows and Completed status
```

## 4. Ask questions about the data

Open **Dialogue** in Knowledge Base. Use **Select Knowledge** to select `financial_sales_knowledge` if it is not already selected.

Send this question:

```text
Summarize total Sales and total Profit by Country, sorted by total Profit from highest to lowest. Use all rows in the sheet1 table and answer in English.
```

MOI returns five countries ordered by total profit: France, Germany, Canada, United States of America, and Mexico. Check the table, the requested sort order, and the table reference returned with the answer.

```{image} ../assets/images/tutorials/excel-nl2sql-country-answer-en.png
:alt: Country sales and profit totals sorted by total profit in descending order
```

Then ask:

```text
Which product had the highest total Sales in 2014? Return the product name and total Sales. Use the sheet1 table and answer in English.
```

In this run, MOI returned **Paseo**, with total Sales of **$27,096,396.87**.

```{image} ../assets/images/tutorials/excel-nl2sql-product-answer-en.png
:alt: MOI identifies Paseo as the product with the highest total Sales in 2014
```

## 5. Inspect the generated SQL

Expand **Thinking** and locate the SQL for the query. Open the SQL details to inspect the statement without needing to read or capture the entire execution trace.

```{image} ../assets/images/tutorials/excel-nl2sql-sql-en.png
:alt: SQL for the 2014 product query, including sales conversion, year filtering, grouping, and descending sorting
```

For the second question, check that the SQL:

- reads `excel.sheet1`;
- filters `Year` to `2014`;
- sums `Sales` by `Product`; and
- sorts total Sales in descending order.

The SQL in this example removes currency formatting from text values and casts each Sales value to `DECIMAL(18, 2)` before summing. Generated SQL may differ if your imported column types differ.

**Precision note:** Rounding each row before summing can differ from summing the original Excel values and rounding only the final total. For Paseo in 2014, the original workbook totals **$27,096,396.86** when rounded at the end, one cent below the result shown here. When checking financial totals, use the same conversion and rounding rules on both sides.

## Complete

You have imported an Excel workbook as a Catalog table, added its 700 data rows to a knowledge base, asked two questions in English, and inspected the generated SQL. You can continue with questions by country, product, market segment, or month using the same knowledge base.
