Syntax & Examples

In this new Post of The Series, I will go over the details for creating a correct Set Expression. Also, I will provide some useful examples for you to get your Hands On right away.

Follow these steps to build up your set expression and use the correct syntax:

  1. First of all, you need to define what you want your expression to return. A good method to get it right would be to answer the following questions first:
    • What field will I use in my expression? (For example: ‘Sales’, containing the sales amount for every invoice.)
    • How will I aggregate the field? (It can be using Sum, Count, Avg, etc.)
    • What explicit selections do I need in my expression? Here you define a Field and its value(s). (For example: I want ONLY the ‘South’ Region. Another example would be to only include values associated with certain Year.)
    • Do I need to exclude/ignore some selections or values?
  2. After you’ve answered the questions, you can go on to compse the expression. If you want to Sum the Sales amount, you would start with something like:
    • Sum(Sales)
  3. Then,wheneedtoaddtheSetportion of the expression.:
    • This portion goes just after the first parenthesis, before the Field Name. The Set Expression will be enclosed in curly brackets: {set expression}.
    • After the first curly bracket we add either a dollar sign (which means the record set will be based on the current selections) or a number 1 (meaning we will use the full record set of all the records in the application). We will use the dollar sign to illustrate, since it is the most common, so you will now have {$}. Important to note is that the dollar sign can be ommited and the set expression will not be affected. It is good practice, however, to use it.
    • After the Dollar Sign, we define the fields that will play in our set expressions. All of these field-value definitions will be encolsed in less-than and grater-than symbols (< >).  The syntax is FieldName = {FieldValue}. If FieldValue is a literal or text, you should enclose it in single quotes. If you want to use a search string as the FieldValue, enclose it in double quotes. Here are some examples:
      • {$<Region = {‘South’}>} will result in a record set taking the current selections ($) where the Field Region has a value of ‘South’.
      • {$<Year = {2010}>} will give return a record set based on current selections ($) where the Year is 2010 EVEN if you select something else in the field Year.
      • {$<Year = {“20*”}>} will give you the record set based on the current selections where the Year matches the search string “20*”, meaning all years that begin with “20”.
      • {$<Year = {“>=2007”}>} Will return a record set where the Year is greater than or equal to 2007. Notice that here we are using a search string.
      • {$<Region = {‘South’, ‘North’}, Year = {2010}>} will give you a record set based on the current selections where Region is equal to South or North and Year is equal to 2010.
  4. Yourfinalexpressionshouldlooksimilar to:
    • Sum({$<Region ={‘South’}, Year = {2010}>} Sales)
  5. You can use variables instead of the hardcoded Field Value:
    • If your variable is a number, use it as Field = {$(MyVariable)}
    • If your variable is text, use it as Field = {‘$(MyTextVariable)’}
    • If your variable should be used as a search string, use Field = {“$(MySearchVariable)”}
  6. Also, you can create calculations to use them as Field Values:
    • Year = {$(=Max(Year))} It is just as if you would use a variable (described above), just with an equal sign. NEVER forget the equal sign here.

So, now you should be able to get up and running creating your set expressions. Remember: This is a basic introduction and there is A LOT more to know about Set Analysis, I will continue with the series to cover some more of it.

Keep Posted. Any comments are welcome!

Update 2019/04/25: If you found this useful, you may want to take a look at the Set Analysis Cheat Sheet and invaluable resource for Qlik Developers.

Qlik Set Analysis Cheat Sheet. By Miguel Ángel García

Qlik Set Analysis Cheat Sheet. By Miguel Ángel García

Update 2012/12/13: Barry Harmsen and I have just released our book QlikView 11 for Developers. While it is not free, it does offer a cost-friendly, guided way to learn QlikView and contains an entire chapter about Set Analysis. More information can be found by clicking here.


Subscribe to receive new posts like this one and updates right in your inbox

I consent to be contacted via email for the purpose of new articles, updates and marketing.