# Supplier Spend Analysis

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

With it you can:

- Optimize total spend by material type and track country concentration.
- Understand hidden purchasing patterns to maximize procurement value.
- Use AI to uncover new insights.

## Reference architecture

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

[![Supplier Spend Analysis consumption data product sample reference architecture for Cortex Framework](https://docs.cloud.google.com/static/cortex/docs/images/supplier_spend_analysis.png)](https://docs.cloud.google.com/static/cortex/docs/images/supplier_spend_analysis.png)
**Figure 1**. Supplier Spend Analysis consumption data product sample reference architecture for Cortex Framework.

## Published data assets

Deploying the Supplier Spend Analysis sample creates the following
BigQuery asset in your target dataset:

| Data asset | Materialization | Primary key grain | Description |
| `supplier_spend_analysis` | SAP ERP: `Incremental` SAP BDC: `View` | SAP ERP: `client_mandt`, `purchasing_document_number_ebeln`, `item_number_of_purchasing_document_ebelp` SAP BDC: `purchase_order`, `purchase_order_item` | Combines purchase order headers and items with supplier, material, and purchasing organization attributes to analyze procurement spend concentration and delivery completion across vendors and regions. |
|---|---|---|---|

## 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:

- Vendors
- Purchasing Documents
- Purchasing Organizational Structure
- Materials

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

- Purchase Order (tables: `purchaseorder`, `purchaseorderitem`, and `purchaseorderscheduleline`)
- Supplier
- Purchasing Organization
- Procurement Configuration Data

## Enable data product in Cortex Framework

To deploy the Supplier Spend Analysis 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 Supplier Spend Analysis sample (SAP ERP version)
      - moduleId: sap_supplier_spend_analysis
        modulePath: cortex_samples.sap.products.supplier_spend_analysis
        enabled: true # Ensure the enabled flag is set to true
        dataTargetId: samples_target
        dependencyBindings:
          sapMaterials: sap_materials
          sapPurchasingDocuments: sap_purchasing_documents
          sapPurchasingOrganizationalStructure: sap_purchasing_organizational_structure
          sapVendors: sap_vendors
```

### 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 Supplier Spend Analysis sample (SAP BDC version)
      - moduleId: sap_bdc_supplier_spend_analysis
        modulePath: cortex_samples.sap_bdc.products.supplier_spend_analysis
        enabled: true # Ensure the enabled flag is set to true
        dataTargetId: samples_target
        dependencyBindings:
          sapBdcSupplier: sap_bdc_catalog.supplier
          sapBdcProcurementConfigurationData: sap_bdc_catalog.procurementconfigurationdata
          sapBdcPurchasingOrganization: sap_bdc_catalog.purchasingorganization
          sapBdcPurchaseOrder: sap_bdc_catalog.purchaseorder
```

### Build and deploy the sample

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 `vendors` and then on `supplier_spend_analysis`. This one-time refresh backfills historical purchasing items for vendors without an `ADRC` record or with delimited addresses.

## 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 supplier spend analysis ER diagram

[![SAP ERP Supplier Spend Analysis ER diagram](https://docs.cloud.google.com/static/cortex/docs/images/supplier_spend_analysis_er.png)](https://docs.cloud.google.com/static/cortex/docs/images/supplier_spend_analysis_er.png)
**Figure 2**. Supplier Spend Analysis consumption data product SAP ERP ER diagram for Cortex Framework.

### SAP BDC supplier spend analysis ER diagram

[![SAP BDC Supplier Spend Analysis ER diagram](https://docs.cloud.google.com/static/cortex/docs/images/supplier_spend_analysis_bdc_er.png)](https://docs.cloud.google.com/static/cortex/docs/images/supplier_spend_analysis_bdc_er.png)
**Figure 3**. Supplier Spend Analysis consumption data product SAP BDC ER diagram for Cortex Framework.

## Source code

The source code for this Supplier Spend Analysis 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/supplier_spend_analysis) or in your local source code at `src/data_modules/cortex_samples/sap/products/supplier_spend_analysis`
- [SAP BDC](https://github.com/GoogleCloudPlatform/cortex-framework/tree/main/src/data_modules/cortex_samples/sap_bdc/products/supplier_spend_analysis) or in your local source code at `src/data_modules/cortex_samples/sap_bdc/products/supplier_spend_analysis`

## Enable AI agents

After materializing your Supplier Spend Analysis data model in
BigQuery, you can provision a natural-language data agent as code
using [`uv run cortex-create-ca-agents`](https://docs.cloud.google.com/cortex/docs/conversational-analytics.md.txt)
or [create a custom data agent](https://docs.cloud.google.com/bigquery/docs/create-data-agents.md.txt) in
BigQuery Studio to query procurement spend concentration and
vendor performance without writing SQL.

## What's next

- [Solution samples overview](https://docs.cloud.google.com/cortex/docs/solution-samples/overview.md.txt)
- [Sales Performance Insights](https://docs.cloud.google.com/cortex/docs/solution-samples/sales-performance-insights.md.txt)
- [Supplier and Plant Weather Risk Insights](https://docs.cloud.google.com/cortex/docs/solution-samples/supplier-plant-weather-risk-insights.md.txt)