Skip to main content
Announcements
Live today at 11 AM ET. Get your questions about Qlik Connect answered, or just listen in. SIGN UP NOW
hic
Former Employee
Former Employee

Several aspects of the Qlik search mechanism has been described in previous posts. There is however one that has not been covered: Search in dual fields, e.g. dates. This post will try to explain the basics.

When making searches in text fields, you can search either by using a normal search or by using a wildcard search, and when you search in numeric fields you can use a numeric search. But what about dual fields, like dates, where you have both a textual and a numeric representation?

The answer is displayed in the picture below.

search h.png

Normal searches and wildcard searches are straightforward and need not be explained. Numeric searches are also possible and do pretty much what you expect them to.

You should however note that the search string in a numeric search must contain the correct formatted date. It is in most cases not possible to use the numeric value of the date. E.g. you cannot search for 42005 when you want Jan 1st 2015, even though this is the value of the date.

The same logic is used in Set Analysis, which means that a correct Set Analysis expression with a date could look like this:

Sum( {$<Date={"<=2015-02-28"}>} Amount)

Often you want the Set Analysis expression to be dynamic, and then you need to put a dollar expansion with an aggregation function inside it. One case is that you want to compare the selected month with the preceding month. In principal, the solution is something similar to the following:

Selected (Last possible) month:     Sum( {$<Month={"$(=Max(Month))"}>}  Amount )
2nd Last month:                                    Sum( {$<Month={"$(=Max(Month)-1)"}>}  Amount )

The Max(Month) will calculate the last possible month, and the dollar expansion will enter this value into the expression before the expression is parsed.

How the expression looks after the dollar expansion can be seen in the column header of a QlikView table. The above formulas have been used in the table below. Note that the dollar expansions with Max(Month) have been replaced with numbers.

QV Table.png

So far, so good.

However, the above formulas will not work. First, if you have created the Month using the Month() function, the field is cyclic which means that December of one year has a higher numeric value than January the following year, although it comes before January. Hence, the Max() function will not respect the order of months belonging to different years.

Secondly, the Month field has a dual value. This means that the Max(Month) will return a numeric when you need the textual value (‘Dec’) in the Set analysis expression.

One solution is to use a sequential month instead, and format it the same way everywhere:

Script:

Date(MonthStart(Date),'MMM-YY') as Month,

Expressions:

Sum( {$<Month={"$(=Date(Max(Month),'MMM-YY'))"}>} Amount )
Sum( {$<Month={"$(=Date(AddMonths(Max(Month),-1),'MMM-YY'))"}>} Amount )

Here the field Month is a date - the first day of the month - but formatted with just month and year. In other words: A number that equals roughly 42000 and is formatted as ‘Jan-15’. The same formatting is applied inside the dollar expansion. Note the column headers below.

QV Table2.png

Often it is practical to put the calculation of the Set analysis condition in variables. This way, the formula is kept in one place only and the Set analysis expressions become simpler and easier to read:

Script:

Set vLastMonth=      "=Date(Max(Month),'MMM-YY')";
Set v2ndLastMonth= "=Date(AddMonths(Max(Month),-1),'MMM-YY')";
Date(MonthStart(Date),'MMM-YY') as Month,

Expressions:

Sum( {$<Month={"$(vLastMonth)"}>} Amount )
Sum( {$<Month={"$(v2ndLastMonth)"}>} Amount )

Note that the variable definitions start with equals signs. This way they will be recalculated at every click.

Summary: Format the dates used inside Set analysis expressions, and use variables to simplify the expressions.

HIC

 

Further reading related to this topic:

The Search String

Data Types in QlikView

Cyclic or Sequential?

A Primer on Set Analysis

32 Comments
Anonymous
Not applicable

Hi Henric,

can you please help me understand the Dates here:-

I have set the following two variables, vCurrentMonth and vCurrentYear. I have checked each value in the Variable Overview:-

vCurrent_Month= Sep and vCurrent_Year= 2016

So I don't understand when using the variables in the following formulas only formula A works (using vMonth_Current).  The second formula using the vCurrent_Year picks up the same values as A.

A.  Count( {$<Date={vMonth_Current}>} EngagementType)

B. Count( {$<Date={vYear_Current}>} EngagementType)

cheers,

Laura

0 Likes
13,750 Views
hic
Former Employee
Former Employee

You seem to be mixing different data types.

Date is (usually) a dual with a numeric value that equals roughly 42000.

Month is (usually) a dual with a numeric value that is between 1 and 12.

Year is a numeric value that equals roughly 2000.

So, you need to first make sure that you are matching dates with dates and years with years.

Secondly, the expression {$<Date={vMonth_Current}>} will try to match the date with the string 'vMonth_Current'. You should instead use a dollar expansion and quotes to use the value of the variable: {$<Date={'$(vMonth_Current)'}>} .

HIC

0 Likes
13,750 Views
Anonymous
Not applicable

thanks Henric.

I understand the second bit but not the first.

In my calendar the year loads correctly.  Year(TempDate) As Year  I.e. this displays as 20016, 2015 etc.

But the variable I have created displays as DDMMYYYY -

LET $(vYear_Current) = Year(Today());  displays as

Else is there a document or utube or other you can please direct me to so I can understand this?

0 Likes
13,728 Views
hic
Former Employee
Former Employee

Assuming that you have

   LET vYear_Current = Year(Today());
   LET
vDate_Current = Date(Today());

then you can use

   {$<Year={‘$(vYear_Current)’}>}
   {$<Date={‘$(vDate_Current)’}>}

but you cannot use

   {$<Date={‘$(vYear_Current)’}>}

as you do in your initial example

HIC

0 Likes
13,728 Views
swuehl
MVP
MVP

Laura, it seems to me that you are using a field vYear_Current in addition to your variable (because you are displaying that field in a list box.

Maybe the values of that field are indeed dates.

Besides that, you should double check your variable definition:

LET $(vYear_Current) = Year(Today());

The dollar sign expansion of vYear_Current variable is probably not what you want to do here.

P.S. It might be better if you could create a separate thread for your issue, where you can also attach e.g. your sample application or script.

0 Likes
13,728 Views
Not applicable

Hi Henric,

I am struggling with this table. I want to show the Current Month for the Current Year and for Prior Years show always December data. I have added the below calculated dimension using aggr and MaxMonth.

I am able to bring February but the same month is shown in other years.

How can I show the prior years with December data and current year with current month?

Dimension

Year =if(aggr((Sum({$<Year={'>=$(vSelectedYearM3) <= $(vSelectedYear)'},Quarter=,Month={'$(vMaxMonth)'}>}Amount)),Year,Month)>0,Year)

Month

Expression = (Sum({$<Year=,Quarter=,Month=>}Amount))/1000

Current Table:

Year Month Amount
2014Feb289474
2015Feb311880
2016Feb345527
2017Feb365164

Expected result:

YearMonthAmount
2014Dec286262
2015Dec342752
2016Dec363261
2017Feb365164

Very appreciated your help.

Thanks,

Marcelo

0 Likes
13,728 Views
pljsoftware
Creator III
Creator III

Hi Marcelo,

you can use a flag field. In script generate a field where all December are

marked as 1 and for the current year is 1 only for use last month.

In set analysis only in expression you can use that field to select what

you need.

Best regards

Luca Jonathan Panetta

Il giorno 12/mar/2017 11:09, "Marcelo Mtanous" <qcwebmaster@qlikview.com>

ha scritto:

Qlik Community <https://community.qlik.com/?et=blogs.comment.created>

Dates in Set Analysis

nuovo commento di Marcelo Mtanous

<https://community.qlik.com/people/marcmta81?et=blogs.comment.created> - Visualizza

tutti i commenti su questo post del blog

<https://community.qlik.com/blogs/qlikviewdesignblog/2015/09/28/dates-in-set-analysis?et=blogs.comment.created#comment-58589>

0 Likes
13,728 Views
Not applicable

It worked, many thanks Luca

0 Likes
13,728 Views
Anonymous
Not applicable

Hello,

Can you explain why when I follow the steps above and use them in my data I get this result?

Capture.PNG

My script and variables follow the posts instructions and my expression within the New Column, which should show the increase or decrease in Seat Count from one Month to the next is:

Sum({$<Month={"$(vLastMonth)"}>}[Seat Count])-Sum({$<Month={"$(v2ndLastMonth)"}>}[Seat Count])

I understand that it is inserting the part relative to the minus sign into the month before instead of using it as part of an aggregation.

Additionally how do I make it so that it performs the above calculation for all month, not just for the Max (June in this case)?

0 Likes
13,733 Views
hector_munoz
Specialist
Specialist

Thanks for sharing, Henric! Very clear!

0 Likes
13,733 Views