Skip to main content
Announcements
Introducing a new Enhanced File Management feature in Qlik Cloud! GET THE DETAILS!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to sum based on dates

Hello,

I have the need to sum a value based on the closest matching date to another.  I have a score that is pulled at a date and tied to a customer but not directly tied to a transaction.  The date the score is pulled is stored and the date the transaction executed is stored.  Since the score is not tied to the transaction and to the customer my data is not showing properly.  So I would like to put some set analysis in so that I only grab the score from the closest date prior to the execution date.

From the dummy data below I would only want to see the first row, I have tried a few things but no luck.  Any help would be much appreciated.

Trans IDScore ScoreDateExecution DateAmount
12344503/31/20154/15/2015200
12345601/20/20154/15/2015200
1 Solution

Accepted Solutions
sunny_talwar

Try with these slight modifications:

Table:

LOAD [Trans ID],

    Score,

    ScoreDate,

    [Execution Date],

    Amount,

    [Execution Date] - ScoreDate as Range

FROM

[https://community.qlik.com/thread/217587]

(html, codepage is 1252, embedded labels, table is @1);

Left Join (Table)

LOAD [Trans ID],

  Min(Range) as Range,

  1 as Flag

Resident Table

Group by [Trans ID];

I should have just added this to my first post, apologize for not doing this earlier.

View solution in original post

16 Replies
sunny_talwar

May be create a flag in the script and use the flag in the set analysis condition:

Table:

LOAD [Trans ID],

    Score,

    ScoreDate,

    [Execution Date],

    Amount,

    [Execution Date] - ScoreDate as Range

FROM

[https://community.qlik.com/thread/217587]

(html, codepage is 1252, embedded labels, table is @1);

Left Join (Table)

LOAD Min(Range) as Range,

  1 as Flag

Resident Table;

Capture.PNG

You might need to Group by more dimensions, but the idea will remain the same

Not applicable
Author

That is a good idea but unfortunately the script is very massive and this is a one off request, I was hoping this was going to be simple and not require a lot of changes.

sunny_talwar

So you want to do this on the front end?

Not applicable
Author

Yes, that would be best.

sunny_talwar

May be using this set analysis:

{<ScoreDate = {"=([Execution Date] - ScoreDate) - (Min(TOTAL ([Execution Date] - ScoreDate))) = 0"}>}

Not applicable
Author

That didn't produce any results for me.  I actually just pulled the data that is duplicating out of QlikView and put it into excel and then created a new QlikView file to try your script option.  Unfortunately that only generated one Flag for 1 transaction and wasn't done for each transaction. 

How could we modify that so that it created a flag for each transaction?  If it did that then it would work perfect.

sunny_talwar

That's what I mentioned in my 1st response that you might need to Group by Trans ID or something else. Are you looking to get one combination of ScoreDate, Execution Date per Trans ID?

sunny_talwar

Try with these slight modifications:

Table:

LOAD [Trans ID],

    Score,

    ScoreDate,

    [Execution Date],

    Amount,

    [Execution Date] - ScoreDate as Range

FROM

[https://community.qlik.com/thread/217587]

(html, codepage is 1252, embedded labels, table is @1);

Left Join (Table)

LOAD [Trans ID],

  Min(Range) as Range,

  1 as Flag

Resident Table

Group by [Trans ID];

I should have just added this to my first post, apologize for not doing this earlier.

Not applicable
Author

That is very good.  Only one more thing.

How can we make it so the flag only generates on a positive value?