Skip to main content
Announcements
Qlik Connect 2025: 3 days of full immersion in data, analytics, and AI. May 13-15 | Orlando, FL: Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to obtain the latest date from exisiting dataset

Hi all,

I had a table as below:

Forecast_TypeForecast_DateForecast_AmtForecast_run_date
Inward01/03/201410022/02/2014
Inward01/03/201410223/02/2014
Inward01/03/201410424/02/2014
Inward01/03/201410625/02/2014
Inward01/03/201410826/02/2014
Inward02/03/201420023/02/2014
Inward02/03/201420224/02/2014
Inward02/03/201420425/02/2014
Inward02/03/201420626/02/2014
Inward02/03/201420827/02/2014
Inward03/03/201430024/02/2014
Inward03/03/201430225/02/2014
Inward03/03/201430426/02/2014
Inward03/03/201430627/02/2014
Inward03/03/201430828/02/2014

Let's say if I want to obtain the Forecast_Amt for the Forecast_Date based on the latest Forecast_run_date, can anyone advice how should I go about writing the expression in the straight table for that? I tried using the expression

 

=if(Forecast_Type='Inward' and max(Forecast_run_Date),sum(Forecast_Amt))

but it doesn't work. An example of output which I want to have when Forecast_Date is 01/03/2014 is 108.@@

1 Solution

Accepted Solutions
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

In a Straight Table with Dimension of Forecast_Date I would use the expression:

=FirstSortedValue(Forecast_Amt, -Forecast_run_date)

-Rob

http://masterssummit.com

http://robwunderlich.com

View solution in original post

8 Replies
paul_scotchford
Specialist
Specialist

Hi

Load your table as normal but add a flag e.g. 1 as fTxn (for every row in the load) then

the below variable assignment will show the latest ... you can build on this example ...

vMaxDataDate =date(max(if(fTxn=1,Forecast_run_date)))

Then sum via set analysis ... (we do this type of sum on dates all the time here)

sum({<Forecast_run_date, Forecast_Type, Forecast_Type={'Inward'}, Forecast_run_date={'=$(vMaxDataDate )'}>} Forecast_Amount)

Not applicable
Author

Hi Paul,

Thanks for your reply. May I check with you, when you mention about adding a flag for every row in the load, do you mean that I had to add it inside my dataset manually or I just need to write an expression to add the flag to add in into the dataset? Thanks!

paul_scotchford
Specialist
Specialist


Hi

Yes you load script is where its done, we load from QVD's , hence if you did the same your

load would look like the below format..

fact:

LOAD 

    Date as FactDate

field1 .

field 2.

. and so on

  , 1 as fTxn

FROM

   QVD\fact.qvd

if you do directly from SQL to QVD or QVW you could just add the line to your sql ...

select

     field 1, 2, 3 etc

     1 as fTxn

from schema.table

Get the gist of it ?

cheers

Not applicable
Author


Hi Paul,

I tried to use the flag to load, but there is a problem, when I tried to define

vMaxDataDate =date(max(if(fTxn=1,Forecast_run_date)))

and load the script but there is an error in scripts message shown. I am wondering if you are able to show me  an example based on my table above? Many thanks!

paul_scotchford
Specialist
Specialist

The vMaxDataDate is a variable so you can define that anywhere either manually in the Variables browser or

in the Loader.

can you post your load script ?

Not applicable
Author

  Here it is

vMaxDataDate =date(max(if(fTxn=1,Forecast_run_date)))

LOAD

Forecast_Type,
Forecast_Date,
Forecast_Amt,
Forecast_run_date,

1
as fTxn

FROM

C:\......\TEST20MAR.xls
(
biff, embedded labels , table is [Sheet1$]);

Thank you

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

In a Straight Table with Dimension of Forecast_Date I would use the expression:

=FirstSortedValue(Forecast_Amt, -Forecast_run_date)

-Rob

http://masterssummit.com

http://robwunderlich.com

Not applicable
Author

Hi Rob,

Many thanks! It works!!