Almost all analytical queries include filters. Filters ensure that the results are meaningful to a business user. Filters also help to reduce the wait-time for the results to appear. Filter values that never change are often hard-coded in the analytical query definition.
However, hard-coding filter values in an analytical query means that the analytical query cannot be reused in different scenarios. To avoid this problem, we can implement variables in our analytical queries.
Note
Let's be clear about the difference between a variable and a parameter as these are both mentioned in this lesson. Variables are either prompts (with or without default values) where users can specify values, or they are hidden so that the analytical engine derives the values. Variables are processed at run-time.
The technical implementation (design-time) of variables is handled using either CDS parameters or special annotations. In this course we focus on the use of parameters to generate variables (hidden and displayed). We do not cover special annotations used to handle variables.
This example shows how we generate a variable which must be filled with a single value before the analytical query can be executed.

The value help for the variable is derived from the foreign-key association of the field AirlineID that is defined in the cube.
Below we see the analytical query results. Notice that the variable appears as a user prompt for AirlineID.

By implementing a variable, the analytical query is reusable because the filter for airline is no longer hard-coded. Instead, the business user is able to select the filter value at run-time to evaluate any airline.
If a from and to value is required, you can use an annotation that defines the parameter as having an interval.
Here is the code to implement a simple variable using an interval:

Notice in the CDS syntax it might appear that in the where clause FlightYearMonth = $parameter.p_YearMonth selects only a single value. But in fact, at run-time the variable that is derived from this parameter requires a low and high value. At run-time, the value of field FlightYearMonth must be between the range provided by the variable.
As well as interval, there are other settings that control the behavior of variables:
- @AnalyticsDetails.variable.selectionType:
- #SINGLE - Single value variable (this is the default selection type)
- #INTERVAL - Interval variable
- #RANGE - Select option variable (Including/Excluding, Contains Pattern)
- #HIERARCHY_NODE - hierarchy node variable
- @AnalyticsDetails.variable.multipleSelections: true - multiple selections are possible
- @AnalyticsDetails.variable.mandatory: false - variable is optional
- @Consumption.hidden: true - variable doesn’t have a prompt at runtime, it must be filled with a default value or by derivation
Parameters are often used in analytical query filters but they can also be used in other places such as:
- WHERE
- CASE WHEN
- in formulas
- in binding parameters of FROM
- in filter of association
- In scalar function (for example, currency_conversion, calendar_operation)
- in selected annotations