Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
nachofernandez
Partner - Contributor III
Partner - Contributor III

AddMonths problem in Set Analysis

Hi,

i´m experiencing some problems related to the use of this function with set analysis.

My requeriment is to calculate the sum of sales in a three month period of time based in the Start Date of a Campaign.

I have this data:

idSalesDateSales
1201801010
12018020110.000
12018030140.000
12018040150.000

So, the result for Id=1 should be 50.000 when selecting 20180101

But i can´t get this value. My attempts are:

- sum({$<SalesDate={">=$(=20180101)<=$(=20180301)"}>}Sales)

    This goes ok. Result=50.000


- sum({$<SalesDate={">=$(=Date(Start_Date))<=$(=20180301)"}>}Sales)---->Start_Date=20180101 in this case.

    This goes ok. Result=50.000

- sum({$<SalesDate={">=$(Date(Start_Date))<=$(AddMonths(Date(Start_Date),2))"}>}Sales)

           --->AddMonths(Date(Start_Date),2)=20180301 in this case.

    This goes wrong. Result=100.000



It seems AddMonths function is not working. Any idea would be appreciated.


Thanks in advance.

Regards.

1 Solution

Accepted Solutions
nachofernandez
Partner - Contributor III
Partner - Contributor III
Author

I think I´ve found the solution.

It seems Set Analysis is not applicable to this situation. Just if a record or a date is selected, but with no selection it doesn´t work. Changing it for an if function, it works very well.

Thank you very much for all the answers. They helped me a lot.

View solution in original post

11 Replies
tamilarasu
Champion
Champion

Hi Nacho,

sum({$<SalesDate={">=$(=Date(Start_Date))<=$(=AddMonths(Date(Start_Date),2))"}>}Sales)

nachofernandez
Partner - Contributor III
Partner - Contributor III
Author

Thank you

but I´m afraid it doesn´t work either.

The result with that expression is 0.

But if i change AddMonths(Date(Start_Date),2) with 20180301 it goes perfect. Something´s wrong with AddMonths.

tamilarasu
Champion
Champion

Got it.


sum({$<SalesDate={">=$(=Date(Start_Date))<=$(=Date(AddMonths(Date#(SalesDate,'YYYYMMDD'),2),'YYYYMMDD') )"}>}Sales)


If your SalesDate field is already formatted as date format means you can simply try,


sum({$<SalesDate={">=$(=Date(Start_Date))<=$(=Date(AddMonths(SalesDate,2),'YYYYMMDD') )"}>}Sales)

nachofernandez
Partner - Contributor III
Partner - Contributor III
Author

No, sorry,

None of them work.

ruanhaese
Partner - Creator II
Partner - Creator II

There are other approaches you can take if your struggling with this.

(1) Add a field in the load script that highlights it as part of that 3 month group.

(2) Another simpler approach might be to try the RangeAvg function

RangeAvg - script and chart function ‒ QlikView

You can use this directly in your expressions

tamilarasu
Champion
Champion

I am not sure but it's working for me.

Capture.PNG

Try removing the label and see how expression is evaluated. If you still have any issues, please upload a small sample file.

jasonmomoa
Creator
Creator

Hello nachofernandez‌,

Whenever I try to do set analysis with dates, I always convert them to numerical values first. As we are performing logical operations (>= <=), it is best to work with integers rather than date formats that may cause you some nightmares.

Find below my notes.

Script:

LOAD * INLINE [

id, SalesDate,  SalesDateNum, Sales

1,  01/01/2018, 43101,        0

1,  01/01/2018, 43132,        10000

1,  01/01/2018, 43160,        40000

1,  01/01/2018, 43191,        50000

];


Let Start_Date = Num(Date(MakeDate(2018, 1, 1)))

Let End_Date  = Num(AddMonths($(Start_Date), 2))

Set Analysis (see the flow):

SUM( {<[SalesDateNum]={">= 43101 <= 43160"}>} [Sales])  --> 50k

SUM( {<[SalesDateNum]={">= 43101 <= $(End_Date)"}>} [Sales])--> 50k

SUM( {<[SalesDateNum]={">= 43101 <= $(=Num(AddMonths($(Start_Date), 2)))"}>} [Sales])  --> 50k (Notce $=(..) )

SUM( {<[SalesDateNum]={">= 43101 <=      Num(AddMonths($(Start_Date), 2)))"}>} [Sales])  --> 100k

Most probable, your error was due to the $(= ..) variable inside your SalesDate expression:

Not working: Sum({$<SalesDate={">=$(Date(Start_Date))<= $(AddMonths(Date(Start_Date),2))"}>}Sales)

Working:      Sum({<[SalesDateNum]={">= 43101 <= $(=Num(AddMonths($(Start_Date), 2)))"}>} [Sales])

You could replicate these numbers using your SalesDate in date format. But be aware that even though you type in 20180101, this may not be a numerical value but a date value. To prevent such discrepancies, I highly recommend you to use numerical values always.

Hope it helps 🙂

Saludos,

nachofernandez
Partner - Contributor III
Partner - Contributor III
Author

Thank you.

I´ve tried with num fields, but i get the same answer.

I attach a test document where you can see the problem.

I want the sum of sales for 3 months based on the Start Date Campaign.

For ID 1 the correct answer is 50.000. I only get this number if i use the exact date on the Set Analysis Formula.

jasonmomoa
Creator
Creator

In your test here, the Date_ID from your Sales inline table is not related to the Date_ID_num from your Calendar table.

You can create a simple table box with fields Date_ID, Date_ID_Num, ID and Sales. You'll see there is no relationship. Therefore, your last expression test using Num() dates in the set analysis won't work.

Try creating the very first input table with Dates(), rather than just typing the id in YYYYMMDD format.

Regards,