Switch (PQL)
Controls the flow of logic by executing certain statements if their 'case' matches the supplied input value.
- Returned Output: Variant
- Library: PQL \ Semantic \ Logical
- Version: 2018.00.000
- Compatibility: Pyramid Query Language (PQL) data sources
Syntax
* Click on the function's arguments above for more details on the input values.
Comments
- The Variable can be a numeric value, a String, or a Boolean. Its value can be derived from a data point (or tuple) or from a parameter.
- The Case values are compared to the variable value provided.
- A Statement (or expression) is required for each case value (separated by a comma):
- The statements can evaluate to numeric, String, or Boolean values (including data points and tuples), or to list or set definitions for producing a list of model elements from a hierarchy.
- If the expression results are list or set definitions, the final resolved element list must come from the same model hierarchy.
- The last statement (the Default) is optional and does not need a case value. It will be the default expression if no cases match the variable.
- The statement expressions should return the same type of result: numeric, String, or Boolean values (and data points), or sets. Mixing result types can lead to errors.
- For details on how to employ and use this function see the semantic calculation overview.
Different Function types
- The common library offers a similar CASE function for non-model operations. For more information, see Common functions.
- The IF function is also similar to Switch.
Examples
This example looks at the number of "Blade" products. If the quantity is 100, the switch returns a list of countries - Australia, US, and Canada. If it is 200, it returns an alternative list of countries - France and Germany. If it is any other value, it returns the default list of countries - UK and Finland:
Switch( ([measures].[Quantity],[Product].[Product].[Blade]), 100, {[Customer].[Country].[Australia], [Customer].[Country].[United States], [Customer].[Country].[Canada]}, 200, {[Customer].[Country].[France], [Customer].[Country].[Germany}, {[Customer].[Country].[United Kingdom], [Customer].[Country].[Finland} )
This example looks at the value of an input parameter called variable "v1". If v1 is 1, it returns the values of sales plus 10%. If v2 is 2, it returns the values of sales plus 25%. Otherwise, it returns the value of sales minus net profit:
Switch( [variable].#[v1], 1, [measures].[sales] * 1.10, 2, [measures].[sales] * 1.25, [measures].[sales] - [measures].[net profit] )