# Sales Performance Insights

This Google Cloud Cortex Framework consumption data product sample provides an
example of how to accelerate insights into sales performance health using SAP
ERP or SAP BDC sourced data.

With it you can:

- Optimize revenue by product, region, and sales organization.
- Track performance across distribution channels and divisions.
- Use AI to uncover fulfillment bottlenecks or on-time delivery trends that impact revenue and costs.

## Reference architecture

A high-level reference architecture provides an overview of the source-specific
data products required to feed this use case.

[![Sales Performance Insights consumption data product sample reference architecture for Cortex Framework](https://docs.cloud.google.com/static/cortex/docs/images/sales_performance_solution_sample.png)](https://docs.cloud.google.com/static/cortex/docs/images/sales_performance_solution_sample.png)
**Figure 1**. Sales Performance Insights consumption data product sample reference architecture for Cortex Framework.

## Published data assets

Deploying the Sales Performance Insights sample creates the following
BigQuery asset in your target dataset:

| Data asset | Materialization | Primary key grain | Description |
| `sales_performance` | SAP ERP: `Incremental` SAP BDC: `View` | SAP ERP: `client_mandt`, `sales_document_number_vbeln`, `sales_document_item_number_posnr` SAP BDC: `sales_order`, `sales_order_item` | Consolidates sales document headers and items with customer, material, sales organization, and delivery status attributes to track revenue, order quantities, and on-time delivery performance. Both the SAP ERP and SAP BDC assets include a rejection indicator (`is_rejected`) and an overdue status (`is_delivery_overdue`) governed by the `open_delivery_statuses` filter. The SAP ERP asset covers Sales and Distribution (SD) document categories and includes a returns indicator (`returns_item_shkzg`), outbound delivered quantity (`delivered_quantity_lfimg`), and category-signed net value (`signed_net_value_in_document_currency`). The SAP BDC asset covers sales orders from `sap-s4com-SalesOrder-v1` and includes a returns indicator (`is_returns_item`) and signed net amount (`signed_net_amount`). It omits `delivered_quantity` by design because `sap-s4com-SalesOrder-v1` doesn't expose an outbound delivery share. |
|---|---|---|---|

## Required data products

If building on top of SAP ERP sourced data, the following
[Cortex Framework data products](https://docs.cloud.google.com/cortex/docs/data-product.md.txt#data-products-for-sap)
are required:

- Customers
- Delivery Documents
- Sales Documents
- Sales Organizational Structure
- Materials

If building on top of SAP BDC sourced data, the following
[BDC data products](https://api.sap.com/dataproducts) are required:

- Customer
- Sales Order

## Enable data product in Cortex Framework

To deploy the Sales Performance Insights data product, enable the
`cortex_samples` namespace and register the data product module under
`data.modules.products` in your `config/config.yaml` file.

For detailed information on configuring `config/config.yaml`, see the
[Deployment](https://docs.cloud.google.com/cortex/docs/deployment.md.txt) and
[Deployment configuration](https://docs.cloud.google.com/cortex/docs/deployment-configuration.md.txt)
documentation.

### SAP ERP-sourced data product

To enable the SAP ERP consumption data product, add the following configuration
under `data` in `config/config.yaml`, ensuring all dependent source-aligned data
products are also configured and enabled:

```yaml
data:
  namespaces:
    - name: cortex
    - name: cortex_samples

  modules:
    products:
      # Enabled Sales Performance sample (SAP ERP version)
      - moduleId: sap_sales_performance
        modulePath: cortex_samples.sap.products.sales_performance
        enabled: true # Ensure the enabled flag is set to true
        dataTargetId: samples_target
        dependencyBindings:
          sapCustomers: sap_customers
          sapSalesDocuments: sap_sales_documents
          sapDeliveryDocuments: sap_delivery_documents
          sapSalesOrganizationalStructure: sap_sales_organizational_structure
          sapMaterials: sap_materials
```

### SAP BDC-sourced data product

To enable the SAP BDC consumption data product, register your external data
catalog module under `data.modules.catalogs` as described in
[Deployment configuration](https://docs.cloud.google.com/cortex/docs/deployment-configuration.md.txt#data_modules_catalogs),
and add the following configuration:

```yaml
data:
  namespaces:
    - name: cortex
    - name: cortex_samples

  modules:
    products:
      # Enabled Sales Performance sample (SAP BDC version)
      - moduleId: sap_bdc_sales_performance
        modulePath: cortex_samples.sap_bdc.products.sales_performance
        enabled: true # Ensure the enabled flag is set to true
        dataTargetId: samples_target
        dependencyBindings:
          sapBdcCustomer: sap_bdc_catalog.customer
          sapBdcSalesOrder: sap_bdc_catalog.salesorder
```

### Build and deploy the sample

Before deploying, you can customize `filters` under `inlineTableSettings` or a
`tableSettings` file. For SAP ERP defaults (`language`,
`deliverable_document_categories`, `non_commercial_document_categories`,
`negative_value_document_categories`, and `open_delivery_statuses`), see
`src/data_modules/cortex_samples/sap/products/sales_performance/table_settings.default.yaml`.
For SAP BDC defaults (`open_delivery_statuses`), see
`src/data_modules/cortex_samples/sap_bdc/products/sales_performance/table_settings.default.yaml`.
Unchanged v7.0 `delivery_status_complete: ['C']` overrides migrate automatically
to the default `open_delivery_statuses: ['A', 'B']`, whereas customized
`delivery_status_complete` values raise a compile-time error.

Build and deploy the configured sample:

```bash
export CORTEX_CONFIG_FILE=config/config.yaml
uv run cortex-build-and-deploy --config $CORTEX_CONFIG_FILE
```

> [!NOTE]
> **Note:** If you're upgrading an existing v7.0 SAP ERP incremental deployment to v7.1, run a one-time full refresh in Dataform---using `dataform run --full-refresh` or selecting the **Run with full refresh** option when running actions---on `customers` and then on `sales_performance`. This one-time refresh populates the added v7.1 columns (such as `signed_net_value_in_document_currency`, `is_rejected`, and `delivered_quantity_lfimg`) on historical rows, recomputes `is_delivery_overdue`, and satisfies the `signed_net_value_bounds` assertion.

## Entity-relationship diagrams

The following Entity-Relationship (ER) diagrams outline the structure of the
data product models and their relationships with source and associated foundation
tables.

### SAP ERP sales performance ER diagram

[![SAP ERP Sales Performance ER diagram](https://docs.cloud.google.com/static/cortex/docs/images/sales_performance_er.png)](https://docs.cloud.google.com/static/cortex/docs/images/sales_performance_er.png)
**Figure 2**. Sales Performance Insights consumption data product SAP ERP ER diagram for Cortex Framework.

### SAP BDC sales performance ER diagram

[![SAP BDC Sales Performance ER diagram](https://docs.cloud.google.com/static/cortex/docs/images/sales_performance_bdc_er.png)](https://docs.cloud.google.com/static/cortex/docs/images/sales_performance_bdc_er.png)
**Figure 3**. Sales Performance Insights consumption data product SAP BDC ER diagram for Cortex Framework.

## Source code

The source code for this Sales Performance consumption data product sample can
be found in the
[`cortex_samples`](https://github.com/GoogleCloudPlatform/cortex-framework/tree/main/src/data_modules/cortex_samples)
directory of the repository:

- [SAP ERP](https://github.com/GoogleCloudPlatform/cortex-framework/tree/main/src/data_modules/cortex_samples/sap/products/sales_performance) or in your local source code at `src/data_modules/cortex_samples/sap/products/sales_performance`
- [SAP BDC](https://github.com/GoogleCloudPlatform/cortex-framework/tree/main/src/data_modules/cortex_samples/sap_bdc/products/sales_performance) or in your local source code at `src/data_modules/cortex_samples/sap_bdc/products/sales_performance`

## Enable AI agents

After materializing your Sales Performance Insights data model in
BigQuery, you can provision the prebuilt **Sales Performance \&
Revenue Forecast Assistant** (`cortex-sales-revenue-forecast-assistant`, when
deployed alongside the required SAP and Salesforce products) or a custom agent
using [`uv run cortex-create-ca-agents`](https://docs.cloud.google.com/cortex/docs/conversational-analytics.md.txt#prebuilt-agent-templates),
or [create a custom data agent](https://docs.cloud.google.com/bigquery/docs/create-data-agents.md.txt) in
BigQuery Studio to query sales trends and fulfillment metrics without writing
SQL.

## What's next

- [Solution samples overview](https://docs.cloud.google.com/cortex/docs/solution-samples/overview.md.txt)
- [Supplier Spend Analysis](https://docs.cloud.google.com/cortex/docs/solution-samples/supplier-spend-analysis.md.txt)
- [Data product extensibility](https://docs.cloud.google.com/cortex/docs/extensibility-guide-data-product.md.txt)