Custom analytical scalar functions in ABAP CDS: What customers can build today
A manufacturer's developer needs a derived reporting value and must distinguish an analytical calculation from a database-executed function.
START HERE · NO PRIOR TOPIC KNOWLEDGE NEEDED
What it is. Why it matters.
A scalar function turns input values into one output value. In ABAP CDS, database functions and analytical functions run in different engines, so developers must choose the supported kind.
Completely new to SAP?
Manufacturers use SAP to connect work such as buying components, recording inventory, planning production, shipping equipment and accounting for costs. SAP S/4HANA is an enterprise resource planning system: it connects these activities through shared business data.
ABAP is SAP’s programming language for business applications. A program reads data, applies rules and produces a result. Start with the objects involved before investigating code.
A transaction code, or t-code, is a shortcut to a task in classic SAP GUI. Fiori apps provide another way to work. Names and screens depend on the product and release; you do not need a live system to learn the concepts in this guide.

Scope and source-review notes
Scope and evidence
Source review by FAST SAP SOLUTIONS® editorial. This badge describes our editorial review; it does not represent SAP certification or independent endorsement.
CDS scalar functions in supported ABAP 7.58 and later contexts; customer analytical-function creation remains restricted in reviewed SAP documentation.
Primary documentation reviewed on 14 November 2026 using official indexed excerpts. Documentation review only; authored examples were not compiled or executed in an SAP system.
A MANUFACTURING EXAMPLE
Calculate a manufacturing reporting metric in the right engine
Choose the execution engine
Read the example values
- Input
- One business value
- SQL scalar function
- Database execution
- Analytical function
- Analytical engine
- First question
- Which engine consumes it?
Words you’ll meet in this guide
- Program
- A set of instructions executed by a computer.
- Class
- A reusable unit that groups data with operations called methods.
- Database
- Organized information kept beyond one program execution.
- Internal table
- A list of rows held temporarily in an ABAP program’s memory.
- Key
- One or more fields used to identify or find a record.
- ABAP
- SAP’s programming language for business applications.
- CDS
- Core Data Services: reusable definitions of business data and relationships.
- HANA
- SAP’s database platform.
- SQL
- A language used to read and work with database data.
- ADT
- ABAP Development Tools: SAP tools for ABAP development in Eclipse and, on supported systems, Visual Studio Code.
Preview the practical steps
SEE THE CONNECTION
See it in a simple example.
A small example to make the idea concrete.
Calculate a manufacturing reporting metric in the right engine
A manufacturer's developer needs a derived reporting value and must distinguish an analytical calculation from a database-executed function.
Customers can build SQL-based functions, not their own analytical-engine functions
The answer depends on which execution engine you mean.
Prerequisites and release scope
The relevant scalar-function capabilities were introduced with ABAP release 7.58 and corresponding supported product versions, including SAP S/4HANA 2023.
Scalar describes the result shape, not the timing
A scalar function returns an elementary result from its input values.
Calculate a manufacturing reporting metric in the right engine
A manufacturer's developer needs a derived reporting value and must distinguish an analytical calculation from a database-executed function.
Identify the documented execution engine and supported function contract before reusing the calculation in another reporting layer.
| Reader | Business question | Evidence to carry forward |
|---|---|---|
| SAP developers and application teams supporting manufacturers | Calculate a manufacturing reporting metric in the right engine | Identify the documented execution engine and supported function contract before reusing the calculation in another reporting layer. |
01 Customers can build SQL-based functions, not their own analytical-engine functions
The answer depends on which execution engine you mean. Current SAP documentation distinguishes SQL-based CDS scalar functions from analytical CDS scalar functions. Customers can create SQL-based scalar functions in supported releases. Analytical scalar functions can currently be created, edited or extended only by SAP; customers consume the supported SAP-delivered functions in the documented analytical-query context. [1] [2]
That restriction matters when a requirement says “make our KPI reusable as an analytical function.” A reusable business calculation is a valid requirement, but it does not establish a supported customer extension point in the analytical engine. First decide whether the calculation belongs at row level, after aggregation, or in the consuming application. This tutorial develops that decision using a margin example rather than offering unsupported analytical-function creation steps.
02 Prerequisites and release scope
The relevant scalar-function capabilities were introduced with ABAP release 7.58 and corresponding supported product versions, including SAP S/4HANA 2023. Support differs by language version, runtime and consumption position. Check the installed ABAP keyword documentation, backend release and ADT capabilities together. A recent editor cannot add a backend language feature to an older system. [2]
You should understand CDS view entities, measures and aggregation, and have authorized access to a development package. SQL-based implementation uses the documented AMDP path and supporting definition/implementation artifacts. Do not copy a partially remembered definition into production. This guide gives a concrete design and verification worksheet; any activation and runtime exercise must use the syntax and permitted objects of your actual release. [3]
03 Scalar describes the result shape, not the timing
A scalar function returns an elementary result from its input values. That says little about when a reporting framework evaluates it. A database expression may transform each source row before aggregation. An analytical calculation can operate on aggregated measures in the analytical engine. Similar-looking arithmetic can therefore produce very different totals. [1]
Keep three concepts separate: the function's signature, the implementation engine and the expression context in which it can be called. The signature establishes input and result types. The implementation reference connects the definition to a runtime implementation. The consuming CDS entity determines whether the function is allowed there. Activation of one artifact does not prove that every intended analytical consumer supports the same expression. [3]
04 Worked case: two products and a misleading average
Assume a fictional sales dataset already expressed in one reporting currency, EUR. Product A has revenue 100 and cost 80, so profit is 20 and margin is 20%. Product B has revenue 900 and cost 450, so profit is 450 and margin is 50%. Total revenue is 1,000 and total profit is 470. The business wants total margin of 47%, calculated as total profit divided by total revenue.
An unweighted average of the two row margins is 35%. Summing those margins gives 70%, which is also unsuitable. The arithmetic is valid at each row, but the aggregation meaning is wrong. This is why a request for a function called margin must state its aggregation behavior before choosing a scalar-function technology. A reusable function name cannot repair a measure whose total has the wrong semantics.
| Product | Revenue | Cost | Profit | Row margin |
|---|---|---|---|---|
| A | 100 | 80 | 20 | 20% |
| B | 900 | 450 | 450 | 50% |
| Total | 1,000 | 530 | 470 | 47% from aggregated amounts |
05 Choose the layer from the required result
For a row-level transformation such as mapping an input category to a normalized value, a supported SQL-based function may be appropriate if built-in CDS expressions cannot express the rule clearly. For margin on totals, preserve revenue and profit as additive measures and derive their ratio at the analytical level using supported calculated-measure facilities. For a display-only suffix, the consuming UI may be the right layer.
Ask what happens when the user removes Product from the drilldown. If the result should be recomputed from grouped revenue and cost, the requirement is aggregation-sensitive. If the result is a stable property of each original row, database evaluation is more plausible. Also ask whether currency conversion, unit handling, authorization or null values affect the input population before the calculation.
| Requirement | Likely design direction | Proof needed |
|---|---|---|
| Normalize each source value | Built-in CDS expression or SQL-based function | Supported types and call context |
| Margin after revenue aggregation | Analytical calculated measure | Correct total and zero-revenue behavior |
| Use an SAP-delivered analytical function | Documented query projection consumption | Function availability and supported signature |
| Create a new analytical-engine function | Not a supported customer creation path today | Choose another supported model |
| Add display decoration | Consumer formatting | No change to business measure semantics |
06 Design the data contract before implementing SQL logic
Write a signature worksheet with each input's business meaning, CDS type, null policy and unit or currency reference. For the margin case, an amount alone is incomplete without its currency context. Do not pass 100 EUR and 900 USD into the same total and call the result a meaningful reporting margin. Establish conversion and aggregation rules explicitly.
Specify the result scale and rounding policy. “Return 47” and “return 0.47 formatted as 47%” are different contracts. Decide what zero revenue means: undefined, excluded, zero by policy, or a separate indicator. A SQL implementation and an analytical formula must agree on that decision if both are exposed. Keep exceptional cases visible rather than silently replacing every undefined result with a reassuring zero.
07 Build a minimal supported SQL-based proof
If row-level reusable logic is still required, create the scalar-function definition and corresponding implementation reference through the release-matched ADT workflow. Follow the documented AMDP implementation requirements for that backend. Start with simple fixed inputs and a small ordinary CDS view-entity consumer before adding analytical projections, associations or application service layers. [3]
Treat each activation as a separate result. The definition may activate while its implementation reference or consuming expression fails. Read that exact diagnostic; it may identify unsupported parameter types or a disallowed consumption position rather than an arithmetic defect. Keep a tiny test consumer so you can isolate function support from the larger report. Do not infer database execution support from an SAP-internal analytical example.
Inventory the environment
Record ABAP release, language version, database/runtime and ADT version alongside the target consumer.
Expected observation: The feature decision has a concrete platform.
Reduce the signature
Use explicit supported scalar inputs and one result, with a written null and rounding contract.
Expected observation: Inputs are independently understandable.
Activate each dependency
Create definition, implementation reference and documented runtime implementation, then activate the smallest consumer.
Expected observation: Activation failures identify a particular artifact.
Compare fixed values
Use known cases before performance or analytic integration tests.
Expected observation: The scalar result matches the written contract.
08 A calculation worksheet that exposes aggregation errors
The following pseudocode expresses the business requirement rather than claiming release-independent CDS syntax. It separates row profit from total margin. Use the actual calculated-measure syntax documented for your analytical-query type when implementing the report. This distinction keeps an illustrative formula from masquerading as an executable declaration.
For the two-product fixture, verify both product drilldown and the grand total. Add a third row with revenue zero and positive cost; this forces an explicit undefined-margin decision. Then group by a different dimension. If the result changes merely because a UI averages displayed percentages, the consumer is using the wrong aggregation rule.
row_profit = revenue - cost
group_revenue = SUM(revenue)
group_profit = SUM(row_profit)
IF group_revenue <> 0:
group_margin_ratio = group_profit / group_revenue
ELSE:
group_margin_ratio = undefined_by_contract
A: 20 / 100 = 0.20
B: 450 / 900 = 0.50
Total: 470 / 1000 = 0.4709 Troubleshoot by engine and artifact
A syntax failure, an incorrect total and a slow report are three different investigations. Capture the exact artifact and expression position before changing the algorithm. An unsupported customer analytical function cannot be made supported by retrying activation, while a supported formula can still be modeled incorrectly.
| Observation | Check | Next design action |
|---|---|---|
| Customer analytical creation rejected | Documented SAP-only creation restriction | Use supported SQL logic or calculated measure |
| Definition works; consumer fails | Permitted expression context and provider contract | Test the smallest supported consumer |
| Rows correct; total wrong | Ratio before or after aggregation | Calculate from aggregated measures |
| Blank or invalid result | Null and zero-denominator contract | Define the business outcome explicitly |
| Amounts aggregate incorrectly | Currency/unit references and conversion | Normalize measures before comparison |
| High execution cost | Row count, repeated evaluation and plan | Measure representative SQL work |
10 Verification across positive, negative and boundary cases
A positive case uses A and B and expects profit 470 with margin 47% at total level. A negative case attempts the unsupported analytical creation route and records the actual documentation restriction rather than treating an activation failure as a configuration puzzle. A boundary case uses zero revenue, null input where possible, and the maximum meaningful scale required by the business.
Also test mixed currencies, a filtered subset and a different drilldown. The subset containing only A should yield 20%; the full set should yield 47%. Confirm that display formatting does not change the stored ratio. Record actual execution separately from this illustrative worksheet. No performance or customer-system validation is claimed here.
11 Maintainability and performance questions
A reusable function creates a dependency that other CDS entities can adopt. Give the signature stable business meaning and document the intended aggregation layer. A later change from ratio to percentage points can silently break consumers even when the technical type remains compatible. Version or coordinate material contract changes rather than relying on the function's convenient name.
Measure the deployed design with representative row counts. Moving arithmetic to the database can reduce application transfer, but a function evaluated across many rows is not automatically cheaper than a transparent expression. Evaluate correctness first, then investigate execution cost using the supported database and ABAP tools. Keep authorization and analytical semantics intact when simplifying the model.
12 Frequently asked questions
Can customers create custom analytical scalar functions today? The reviewed SAP documentation says analytical creation, editing and extension are restricted to SAP. Customers can create SQL-based functions within supported releases and contexts. [1]
Can I use a SQL-based function in any analytical query? Do not assume so. The execution engine, provider contract and allowed expression position must all match your release. Prove the intended consumer independently.
Is a calculated measure less reusable? It solves a different placement problem. Reuse additive base measures and an appropriately defined analytical contract rather than forcing a post-aggregation requirement into row-level code.
What should I document for approval? Include the two-row calculation, expected grand total, type and currency contract, release evidence, supported artifact path and unexecuted checks. This makes the design reviewable before a large implementation is built.
Related services and next steps
Primary sources
Found this useful?
Share this guide with your network, or leave a like.
Your like is saved in this browser.