CDS Annotations in SAP – 2

This is Second blog post for CDS Annotations in SAP ABAP. If you have not Read First Blog post you can read from CDS Annotations in SAP

A strong framework for creating semantically rich data models in SAP is Core Data Services (CDS). Additional metadata provided by annotations in CDS views can improve the data models’ functionality and regulate different facets of the SAP system’s data access, processing, and visualization. Specific behaviors like data access control, performance optimizations, user interface annotations, and integration with other SAP tools (like Fiori or BW) are made possible by these annotations.

AnalyticsDetails- ABAP Annotations in SAP

Additional metadata for CDS views meant for use in analytical scenarios is provided by AnalyticsDetails Annotations. These CDS annotations aid in defining the behavior of a CDS view when it is used by analytical tools like SAP Analytics Cloud, SAP BW/4HANA, or SAP Fiori apps.

In order to ensure that the data is accurately represented for reporting, OLAP (Online Analytical Processing), and analytical use cases, the AnalyticsDetails Annotations are crucial in defining the view’s function as an analytical provider. Users of the CDS view can access key performance indicators (KPIs), dimensions, measures, and other analytical semantics thanks to these CDS annotations.

@AnalyticsDetails.query

The @AnalyticsDetails.query annotation indicates that a CDS view is an analytical query and is intended to be consumed as an analytical provider (e.g., for use in SAP Fiori apps or analytics tools). This annotation allows the system to treat the CDS view as a data provider for analytical reporting.

  • Usage: Marks the CDS view as an analytical query.
  • Purpose: The CDS view will be treated as a data source for analytical applications like SAP Fiori analytical apps or SAP BW.
@AnalyticsDetails.query: true
define view ZCustomerAnalytics as select from ZCustomerData {
  key CustomerID,
  CustomerName,
  TotalSales,
  TotalQuantity
}

In this example, the @AnalyticsDetails.query annotation ensures that the view ZCustomerAnalytics can be used as an analytical query in applications.

@AnalyticsDetails.measure

The @AnalyticsDetails.measure annotation is used to specify a field as a measure in an analytical query. Measures are typically numeric fields that are aggregated in reports, such as sales amounts, quantities, or profit figures.

  • Usage: Marks a field as a measure that can be aggregated.
  • Purpose: Helps analytical tools understand that this field should be treated as a numeric value that can be aggregated (e.g., sum, average).
@AnalyticsDetails.query: true
define view ZCustomerAnalytics as select from ZCustomerData {
  key CustomerID,
  CustomerName,
  @AnalyticsDetails.measure: true TotalSales,
  @AnalyticsDetails.measure: true TotalQuantity
}

In this example, TotalSales and TotalQuantity are marked as measures. Analytical tools will know that these fields should be aggregated (e.g., summed up) in reports or dashboards.

@AnalyticsDetails.dimension

The @AnalyticsDetails.dimension annotation is used to specify a field as a dimension. Dimensions represent attributes or characteristics by which measures can be sliced or filtered in reports. These typically include attributes such as customer names, product IDs, time periods, etc.

  • Usage: Marks a field as a dimension (i.e., a non-aggregated attribute).
  • Purpose: Helps analytical tools to treat the field as a categorical attribute for grouping or filtering the measures.
@AnalyticsDetails.query: true
define view ZCustomerAnalytics as select from ZCustomerData {
  key CustomerID,
  @AnalyticsDetails.dimension: true CustomerName,
  @AnalyticsDetails.measure: true TotalSales,
  @AnalyticsDetails.measure: true TotalQuantity
}

In this example, CustomerName is marked as a dimension, meaning it will be used for grouping or filtering the measures (TotalSales and TotalQuantity).

@AnalyticsDetails.timeCharacteristic

The @AnalyticsDetails.timeCharacteristic annotation is used to mark a field as a time characteristic. Time characteristics are typically used in reporting or analytics to allow users to slice data by time (e.g., by day, month, year).

  • Usage: Marks a field as a time characteristic.
  • Purpose: Analytical tools can use this field for time-based filtering and grouping.
@AnalyticsDetails.query: true
define view ZCustomerAnalytics as select from ZCustomerData {
  key CustomerID,
  @AnalyticsDetails.dimension: true CustomerName,
  @AnalyticsDetails.timeCharacteristic: true PurchaseDate,
  @AnalyticsDetails.measure: true TotalSales
}

Here, PurchaseDate is marked as a time characteristic, indicating that this field represents a time dimension and can be used for time-based analysis.

@AnalyticsDetails.kpi

The @AnalyticsDetails.kpi annotation is used to mark a field as a Key Performance Indicator (KPI). KPIs are typically numeric fields that measure the performance of a business process and are often displayed as important metrics on dashboards or reports.

  • Usage: Marks a field as a KPI.
  • Purpose: Analytical tools can identify this field as a key metric for business performance.
@AnalyticsDetails.query: true
define view ZCustomerAnalytics as select from ZCustomerData {
  key CustomerID,
  @AnalyticsDetails.dimension: true CustomerName,
  @AnalyticsDetails.kpi: true CustomerSatisfactionScore,
  @AnalyticsDetails.measure: true TotalSales
}

In this example, CustomerSatisfactionScore is marked as a KPI. This field would be treated as a key metric for business performance, and analytical tools may display it as an important value on dashboards or reports.

@AnalyticsDetails.aggregation

The @AnalyticsDetails.aggregation annotation specifies how a field should be aggregated when used in analytical queries. Common aggregation types include sum, average, count, and min/max.

  • Usage: Specifies the aggregation type for a measure field.
  • Purpose: Analytical tools will use this information to aggregate data accordingly in reports.
@AnalyticsDetails.query: true
define view ZCustomerAnalytics as select from ZCustomerData {
  key CustomerID,
  @AnalyticsDetails.dimension: true CustomerName,
  @AnalyticsDetails.measure: true TotalSales,
  @AnalyticsDetails.aggregation: #SUM TotalSales
}

Here, TotalSales is marked for aggregation using SUM, meaning it will be summed up when included in an analytical report.

@AnalyticsDetails.keyFigure

The @AnalyticsDetails.keyFigure annotation is similar to the @AnalyticsDetails.kpi annotation, but it is more generic. Key figures are numeric fields that can represent important metrics or aggregates in analytics and can be used in calculations or as part of a report.

  • Usage: Marks a field as a key figure.
  • Purpose: Specifies a numerical field that represents a business metric.
@AnalyticsDetails.query: true
define view ZCustomerAnalytics as select from ZCustomerData {
  key CustomerID,
  @AnalyticsDetails.keyFigure: true TotalRevenue
}

In this example, TotalRevenue is marked as a key figure, which means it is an important numerical value that can be analyzed or reported on.

ClientDependent-Annotations in ABAP CDS Views

To specify a view’s client-dependent behavior, use ClientDependent Annotations. In particular, they indicate if the information in the CDS view is client-independent or limited to a particular client. In multi-client systems, like SAP S/4HANA, where various clients may have distinct datasets, these CDS annotations are crucial in controlling the handling of client-specific data.

ClientDependent Annotations in CDS Views

To clearly indicate whether a CDS view should act in a client-dependent or client-independent manner, use the ClientDependent annotation. This can be specified in the @ClientDependent annotation.

Client-Dependent CDS Annotations View

A Client-Dependent view can be defined using the @ClientDependent annotation, which ensures that the data is partitioned by the MANDT field (which holds the client value).

@AbapCatalog.sqlViewName: 'ZCLIENT_DEP'
@ClientDependent: true
define view ZClientDependentView as select from sflight {
    key mandt,  " The client field
    key carrid,
    connid,
    fldate,
    price
}

Since the CDS view ZClientDependentView in this example is client-dependent, the data will be divided according to the client; that is, the system will take the value of MANDT into account when retrieving data. This is common for the majority of transactional data that varies from client to client.

Client-Independent CDS Annotations View

The @ClientDependent annotation would be either left off or set to false for a Client-Independent view. In this instance, the data is accessible to all clients and is not client-specific.

@AbapCatalog.sqlViewName: 'ZCLIENT_INDEP'
@ClientDependent: false
define view ZClientIndependentView as select from spfli {
    key carrid,
    connid,
    cityfrom,
    cityto
}

ZClientIndependentView is a client-independent view in this instance. For system-wide data, like master data (for example, airlines, travel connections), where it makes sense to have the same data for every client, this is helpful.

Consumption-Annotations in ABAP CDS View

By adding extra metadata or instructions that affect how the data is used by front-end applications (like SAP Fiori apps or SAP GUI) and other services (like OData services), consumption CDS annotations are used to improve the behavior of the CDS views.

These CDS annotations offer crucial metadata that can direct usage in a variety of situations, including business intelligence (BI) reporting, OData integration, and user interface (UI) rendering.

@EndUserText.label

The goal of this annotation is to provide the entity or fields in the CDS view an easy-to-understand label. Fiori applications and other user interfaces benefit greatly from it.

Use: This annotation can be used to give the CDS entity (view) or its fields a meaningful label. Fiori user interfaces and other front-end apps frequently display this label.

@AbapCatalog.sqlViewName: 'ZDEMO_VIEW'
@EndUserText.label: 'Flight Information'
define view ZDemo_Flight as select from sflight {
    key carrid as Carrier,
    key connid as FlightNumber,
    fldate as Date,
    price as Price
}

In this example, @EndUserText.label is used to assign a label to the entire CDS view ('Flight Information'), which will be used in the UI to describe the view.

@UI.selectionField

The goal of this annotation is to designate specific CDS view fields as selection fields for UI filtering. Fiori apps frequently use it to improve the user interface’s search and filtering features.

Use: It shows that a field in the user interface (UI), usually in a search bar or filter area, can be used as a selection criterion.

@UI.selectionField: { field = 'CARRID' }
define view ZDemo_Flight as select from sflight {
    key carrid as Carrier,
    key connid as FlightNumber,
    fldate as Date,
    price as Price
}

In this case, the CARRID field is marked as a selection field, meaning it can be used for filtering when consuming the view in a Fiori application or other UIs.

@UI.lineItem

The goal of this annotation is to specify which fields, when viewed in a user interface (such as Fiori), should be displayed as columns in a list (such as a table). By indicating which fields should be shown, it aids in defining the layout of the user interface.

Use: It indicates which fields should be shown in the user interface’s grid or list view. When consuming a CDS view, this is helpful for managing which data is displayed by default.

@UI.lineItem: [{ position: 10, label: 'Carrier' }, { position: 20, label: 'Flight Number' }]
define view ZDemo_Flight as select from sflight {
    key carrid as Carrier,
    key connid as FlightNumber,
    fldate as Date,
    price as Price
}

Here, the carrid and connid fields are marked with @UI.lineItem, meaning they will be shown as columns in a list when the CDS view is consumed in a Fiori application, and the position attribute determines their order.

@UI.chart

The purpose of this annotation is to specify which fields, when the data is consumed in a user interface (UI) like Fiori, should be used in a chart visualization, such as pie charts or bar charts.

Use: This is helpful for specifying the graphical representation of specific fields in the CDS view, which facilitates the tracking of KPIs and other significant metrics.

@UI.chart: { measure: 'PRICE', dimension: 'CARRID' }
define view ZDemo_Flight as select from sflight {
    key carrid as Carrier,
    key connid as FlightNumber,
    fldate as Date,
    price as Price
}

In this example, the PRICE field is marked as a measure, and CARRID is marked as a dimension for the chart, meaning these fields would be used to generate a chart visualization in the UI.

@UI.fieldGroup

The goal of this annotation is to arrange fields for presentation in user interfaces (UIs), like those found in Fiori applications. It enables you to divide the user interface into sensible sections, such as “General Information” or “Flight Details.”

Use: By rationally classifying the data, it enhances the user experience by grouping relevant fields into sections.

@UI.fieldGroup: { label: 'Flight Information', elements: ['CARRID', 'CONNID', 'FLDATE'] }
define view ZDemo_Flight as select from sflight {
    key carrid as Carrier,
    key connid as FlightNumber,
    fldate as Date,
    price as Price
}

This annotation creates a field group called Flight Information that contains the fields CARRID, CONNID, and FLDATE, which will appear together in the UI under the same section.

@UI.identification

The @UI.identification annotation is used to indicate which fields belong in the identification section of a user interface (UI), which is usually the first part of a form or a Fiori application.

Use: By using its most crucial features (such a unique ID or name), it enables the user to recognize a critical thing.

@UI.identification: [{ position: 10, label: 'Carrier' }]
define view ZDemo_Flight as select from sflight {
    key carrid as Carrier,
    key connid as FlightNumber,
    fldate as Date,
    price as Price
}

In this example, the Carrier field is part of the identification section in the UI, helping the user quickly identify key information about a flight.

Leave a Comment