Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to load only the most recent price - one row per ID, Country, Source

Hi All,


Would you be so kind to help me with an issue I'm currently facing.

I'm new to QlikView but I tried couple of things I read on this forum such as

Max(Date) with group by but it's loading all rows not only the most recent one.

AllPrices:

IDCountrySourcePriceDate
1DEspar-express.de9003/01/2017
1DEspar-express.de7508/02/2017
1GBtesco.co.uk10015/01/2017
1GBtesco.co.uk25001/02/2017
2DEintersport.de32015/12/2016
2DEintersport.de42002/02/2017
2GBasda.co.uk16001/01/2017
2GBasda.co.uk22006/02/2017

DesiredOutcome:

IDCountrySourcePriceMostRecentDate
1DEspar-express.de7508/02/2017
1GBtesco.co.uk25001/02/2017
2DEintersport.de42002/02/2017
2GBasda.co.uk22006/02/2017

Script I currently have:

AllPrices:

LOAD   

          ID,

          Country,

          Source,

          Price,

          Date(Max(Date),'YYYY-MM-DD') as MostRecentDate

FROM

AllPrices.csv

(txt, codepage is 1252, embedded labels, delimiter is ',', msq)

group by ID, Country, Source, Price;

Thanks in advance for your help.

Regards,

Amko

1 Solution

Accepted Solutions
rahulpawarb
Specialist III
Specialist III

Hello Amko,

Trust that you are doing great!

Please refer below given draft version of script:

AllPrices:

LOAD ID,

     Country,

     Source,

     Price,

     Date(Date) AS Date

FROM

AllPrices.csv

(txt, codepage is 1252, embedded labels, delimiter is ',', msq);

INNER JOIN (AllPrices)

LatestPrices:

LOAD ID,

     Country,

     Source, 

     Date(Max(Date)) AS Date

Resident AllPrices

Group By ID,

     Country,

     Source;

Also refer the sample application attached herewith.

Regards!

Rahul

View solution in original post

9 Replies
dsharmaqv
Creator III
Creator III

rahulpawarb
Specialist III
Specialist III

Hello Amko,

Trust that you are doing great!

Please refer below given draft version of script:

AllPrices:

LOAD ID,

     Country,

     Source,

     Price,

     Date(Date) AS Date

FROM

AllPrices.csv

(txt, codepage is 1252, embedded labels, delimiter is ',', msq);

INNER JOIN (AllPrices)

LatestPrices:

LOAD ID,

     Country,

     Source, 

     Date(Max(Date)) AS Date

Resident AllPrices

Group By ID,

     Country,

     Source;

Also refer the sample application attached herewith.

Regards!

Rahul

adamdavi3s
Master
Master

The problem is that your price changes as well, that is why the group by doesn't work.

I knocked up this code which does the job

Table:

LOAD *, Hash128(ID, Country, Source,Date) as uniquecolumn

FROM

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

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

NoConcatenate

maxdate:

LOAD Hash128(ID, Country, Source,MostRecentDate) as keepid;

LOAD  DISTINCT

          ID, Country, Source,

          Date(Max(Date),'YYYY-MM-DD') as MostRecentDate

resident Table

group by ID, Country, Source;

NoConcatenate

finaltable:

load *

resident Table

where Exists (keepid,uniquecolumn);

drop tables Table, maxdate;

adamdavi3s
Master
Master

Yeah the above example is much simpler, my brain over complicated it!

antoniotiman
Master III
Master III

Hi Amko,

Temp:
LOAD ID,
Country,
Source,
Price,
Date
FROM
"https://community.qlik.com/message/1219395"
(html, codepage is 1252, embedded labels, table is @1);

Load ID,Country,Source,Max(Date) as Date,
FirstSortedValue(Price,-Date) as Price1
Resident Temp
Group By ID,Country,Source;

Drop Table
Temp;

Regards,

Antonio

Not applicable
Author

wow guys
you solved my nightmare

thank you Deepak, Rahul, Adam and Antonio

Not applicable
Author

Hi All,

Is it possible to have the same result in a straight table once all data has been uploaded?

  1. AllPrices: 
  2. LOAD ID,  
  3.      Country,  
  4.      Source,  
  5.      Price,  
  6.      Date(Date) AS Date 
  7. FROM 
  8. AllPrices.csv 
  9. (txt, codepage is 1252, embedded labels, delimiter is ',', msq);


since with the above example if I merge data with another table I don't have the most recent price anymore but all prices.

rahulpawarb
Specialist III
Specialist III

Hello Amko,

Yes, you can! If you refer the table AllPrices given in my prior response has most recent prices; you can add those fields to straight table.

Hope this will be helpful.

Regards!

Rahul

Not applicable
Author

Hi Rahul,

All is fine I was using 2 date columns

In allprices and latest prices date column wasn't of the same name so I didn't have unique rows.

Thanks again.

Regards,

Amko