BW Queries
BW / Analyticsintermediate

Query Design Elements: Restricted/Calculated Key Figures, Variables, Conditions, and Exceptions

Learn how to build advanced query logic using restricted and calculated key figures, variable types, conditions, and exceptions, and how these choices affect performance and front-end consumption.

Explanation

Once basic query structure is understood, most real-world reporting requirements need query-level calculations and conditional logic that go beyond simple rows and columns. Restricted Key Figures (RKFs) take an existing key figure and apply a fixed or variable restriction to one or more characteristics, producing a new reusable column such as 'Revenue - Current Year' or 'Quantity - Region North'. Calculated Key Figures (CKFs) combine existing key figures (including RKFs) using formulas, such as computing a margin percentage from revenue and cost key figures. Both RKFs and CKFs can be defined locally within a single query or saved globally for reuse across multiple queries against the same InfoProvider, which is an important design decision: global reuse reduces duplication but increases the blast radius if the definition changes. Variables extend beyond simple characteristic value prompts. Common variable types include: characteristic value variables (user enters or defaults a value, e.g., fiscal year), text variables (dynamically change a column header, e.g., showing the selected month name), formula variables (inject a value into a calculation, e.g., an exchange rate), and hierarchy node variables (restrict to a selected node in a characteristic hierarchy). Variables can be manual input, default values, replacement path (deriving the value from another query or characteristic), or customer-exit (ABAP-based logic for complex derivation, common for 'current fiscal period' logic). Processing type selection matters: manual-input variables require user interaction at runtime, while customer-exit variables run automatically, which is important for scheduled/broadcast queries where no user is present to respond to a prompt. Conditions filter the result set based on key figure values, such as showing only customers with revenue above a threshold or the top N materials by quantity. Conditions are evaluated after the OLAP engine calculates the result set, so they affect what is displayed but not necessarily what is read from the database, which matters when explaining performance to less experienced colleagues. Exceptions apply conditional formatting (e.g., highlighting cells red when a KPI falls below target) purely for visual emphasis and do not filter data; the two are often confused but serve different purposes and should not be described interchangeably in documentation or interviews. From a performance perspective, complex nested CKFs, especially formulas dividing by key figures that can be zero, or heavy use of exception aggregation, can slow query runtime and require careful testing with realistic data volumes. RKFs restricted on many characteristics can also generate wide, expensive query definitions if not structured through structures (fixed structures with multiple restricted/calculated key figure columns) that reuse selection logic efficiently. Front-end consumption differs by tool: BEx Analyzer and Analysis for Office both execute the same underlying query object and honor its variables, conditions, and exceptions, but web-based tools (BEx Web templates, or newer Fiori-based consumption in embedded analytics scenarios) may render variable input screens and hierarchy selections differently. When a query is exposed through an OData or BICS connection to Analysis for Office or a custom Fiori app, consultants must verify that variable behavior (mandatory vs optional, default values) is tested in the actual consuming tool, not just in Query Designer's test execution, because runtime behavior can differ subtly across connection types. In BW/4HANA, the query design paradigm is largely preserved from classic BW, but consultants should verify current tool-specific capabilities and any deprecated features rather than assuming full parity, since some legacy BEx web items and reporting agent-based scheduling patterns evolved or were replaced over successive releases.

Real project scenario

A manufacturing client needs a profitability query showing margin percentage by product line, with only product lines below a 10% margin flagged visually, and only the top 20 customers by revenue displayed to reduce noise for executive reporting. The consultant creates a calculated key figure for margin percentage (revenue minus cost, divided by revenue, with a check for zero revenue to avoid division errors), applies a condition to show only the top 20 customers ranked by revenue, and adds an exception that colors the margin percentage cell red when below 10%, keeping the underlying data unfiltered by the exception so it only affects visual presentation.

Common mistakes

โ€ข Confusing conditions (which filter displayed rows) with exceptions (which only apply formatting), leading to incorrect requirements documentation. โ€ข Building calculated key figures that divide by a key figure without handling zero or missing values, causing runtime errors or blank cells. โ€ข Overusing customer-exit variables for logic that could be handled with simpler replacement path or default variables, increasing maintenance complexity. โ€ข Making a variable mandatory manual-input when the query is also meant to run unattended in a scheduled broadcast, causing job failures. โ€ข Defining restricted/calculated key figures locally in many separate queries instead of reusing global definitions, causing inconsistent margin or KPI logic across reports.

Best practices

โ€ข Prefer global restricted/calculated key figures when the same KPI logic (e.g., margin percentage) is needed across multiple queries. โ€ข Always guard calculated key figures against division by zero or missing key figures to prevent runtime errors. โ€ข Choose variable processing type based on how the query will be consumed: manual input for interactive ad hoc use, customer-exit or default values for scheduled/broadcast use. โ€ข Use conditions to reduce result set size for performance and readability, and reserve exceptions purely for visual KPI signaling. โ€ข Test query variable behavior directly in the actual front-end tool (Analysis for Office, web template, or embedded app) rather than relying solely on Query Designer's test mode.

Interview angle

Expect questions distinguishing restricted key figures from calculated key figures, and conditions from exceptions, since these are frequently confused. Interviewers may also probe variable processing types (manual input vs customer exit vs replacement path) and ask when each is appropriate, particularly for automated/scheduled reporting versus interactive ad hoc analysis.