Неофициальный форум пользователей Qlik Sense & Qlikview

Для разработчиков => Вопросы по Qlik Sense & QlikView => Тема начата: Pati от 26 декабря 2018, 04:37:53

Название: Перенос часто используемого выражения в переменную
Отправлено: Pati от 26 декабря 2018, 04:37:53
Коллеги, добрый вечер!
Есть выражение, которое используется во многих объектах в QlikView. И при необходимости внесения изменений, приходится это делать во всех объектах.
Прошу подсказать, как можно сделать так, чтоб можно было упростить этот процесс, чтоб можно было внеся в одном объекте изменения и оно применялось ко всем, где используется данное выражение.
Пробовала вынести в переменную, но там он выдаёт фиксированный результат, не позволяет привязываться к другим переменным в расчёте
Название: Re: Перенос часто используемого выражения в переменную
Отправлено: admin от 27 декабря 2018, 08:02:42
Привет.
На эту тему в официальном сообществе есть примеры.
Мне больше нравится вариант с формулами в таблице *.xls
Учтите, что при присвоении формулы переменной следует использовать SET !
Использование простое, через =$(vExpression)
Название: Re: Перенос часто используемого выражения в переменную
Отправлено: Hugo от 27 декабря 2018, 09:25:07
Пример из cookbook:
/*
"User Defined Functions" may be created in QV in two ways.

The first method is to define the function body in a QV variable. The function body is a single QV expression and may include parameters.
The function is called in the script using the dollar-sign expression syntax. Functions defined this way may be called from both script and chart expressions.
See "Dollar-Sign Expansions" in the QV Ref Guide for more information.

The second method is to define the function in the Macro module. The function defintion and body are VBScript statements and may use
any VBS statements or functions allowed in QV macros. Functions defined this way may only be called from script. This example does not demonstrate the
macro method. For a macro example, see the QV Cookbook example "Regular Expression pattern matching function".
*/

/*
I have a complex expression. For neatness and reusability, I'll define the expression in a QV variable
named "calculateDiscountedPrice". Note that my defintion contains parameter refererences. $1 is the first parm, $2 the second and so on.
The function expects a Price ($1) and a Customer ($2).
Comments may be included in the function definition. They will not be stored in the variable.
*/
SET calculateDiscountedPrice=
if($1 > 100 ,$1 * 0.75 // Anything over 100 get 25% discount   
,if($2 = 'A', $1 * 0.90  // Customer A gets 10% discount
,if($2 = 'B', $1 * 0.95  // Customer B gets 5% discount
, $1                     // Otherwise, no discount
)))
;

/*
I'll use the function in my LOAD statement to calculate the DiscountedPrice field.
Using the $() syntax causes the expression to be evaluated, much as the "=" does in a chart expression.
*/
sales:
LOAD *, $(calculateDiscountedPrice(Price, Customer)) as DiscountedPrice
;
LOAD * INLINE [
Customer, Price
A, 9.50
A, 10.0
B, 11.0
B, 12.0
C, 13.10
D, 120.0
]
;