Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Discover how organizations are unlocking new revenue streams: Watch here
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Fields as variable in expressions

Hello,

I have a transactions table that contains fields with keyfigures as gross sales, net sales, margin.

I would like to create a listbox where the analyser just has to select the keyfigure he wants to use in the charts.

The chart expressions would then contain the variable in stead of the field e.g. Sum(varKeyFigure) in stead of Sum(Gross_Sales)

How can I create this in the script?

Any help is appreciated

1 Solution

Accepted Solutions
Not applicable
Author

You can use variables in your expressions with a dollar sign expansion.

This should work:

Sum($(varKeyFigure))


I would probably just Inline Load all of your Key Figures into a separate (unlinked) table.

LOAD * INLINE [
KeyFigures
Gross_Sales
];


And then set your variable value to:

=ONLY(KeyFigures)


That will work as long as one (and only one) selection is made in the key figures box. You can handle multiple selections or no selections as well, but that depends on the needs of the application.

View solution in original post

4 Replies
Not applicable
Author

You can use variables in your expressions with a dollar sign expansion.

This should work:

Sum($(varKeyFigure))


I would probably just Inline Load all of your Key Figures into a separate (unlinked) table.

LOAD * INLINE [
KeyFigures
Gross_Sales
];


And then set your variable value to:

=ONLY(KeyFigures)


That will work as long as one (and only one) selection is made in the key figures box. You can handle multiple selections or no selections as well, but that depends on the needs of the application.

Not applicable
Author

Completely agree with NMiller, it is easier to maintain and no need for macro in this case.

Just don't forget to check always one Selected value box for this field.

Not applicable
Author

Gentlemen,

This worked fine.

Thanks a lot!

michael_anthony
Creator II
Creator II

Hi, I'm trying to do something very similiar, but need to include multiple fields, eg. Tonnes and Gross_Sales. I have a Field called KeyFigures with the list of relevant field names and have included that in the Chart as a Dimension.

I can use an Only(KeyFigures) function in the chart expression to return the field name, but can't get it to recognise that with in the Sum, ie. Sum(Only(KeyFigures)). I have tried various $ sign formats without success. It does work where only one KeyFigure is selected, but not multiple.

You suggested above that "can handle multiple selections or no selections as well", but not sure if I need to do something else to make it work.

Thanks.