Variables can be used anywhere, including in expressions. Variables can hold an entire expression, but they can also contain parts of it.
To use a variable that contains part of an expression, simply use the dollar-sign expansion within the expression. Say you had the following variable created in the script:
LET vCurrentYear = year(today());
SET vMySalesField = '[Sales]';
You could create a chart that calculates sales for the current year using the variable vCurrentYear:
sum(if([Year] = $(vCurrentYear), $(vMySalesField))
This expression returns the sales for the current year.
Variables can also contain the whole expression, which makes it very easy to maintain if the same expression is used in different charts within an application. The following script code shows an example:
SET vExpression = count(distinct [Customers]);
After a script reload, the vExpression variable will hold the expression as a string that can be directly used in any chart expression as follows:
$(vExpression)