Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Only get the closest date

Hi everyone,


I have 2 tables:

LOAD
Sales_ID,

Agency,
Article_ID,
Agency&'_'&Article_ID as Key_Sales ,
Quantity,

Sales_Price,

Sales_Date,

Delivery_Date

from SALES.QVD

LOAD

Agency,

Article_ID,
Agency&'_'&Article_ID as Key_Sales ,
Price_modification_date,

Cost u price

from SALESPRICE.QVD


I link these tables with the key : Agency&'_'&Article_ID as Key_Sales
So i have a duplication for each modification price made

We sometimes modify our sales price but we can't directly know for a Sales_ID which price i have to apply.
I have to only catch the lower closest date

Example: I only need the red bold row on the following tab

Sales_IDArticleSales dateQuantityCost u priceprice modif datesales_pricetotal
00001STABILO11/10/2017122,5201/01/20134.9927.24
00001STABILO11/10/2017122,6801/01/20144.9927.24
00001STABILO11/10/2017122,4901/01/20164.9927.24
00001STABILO11/10/2017122,7201/01/20174.9927.24
00001STABILO11/10/2017122,4401/01/20184.9927.24

Total is : (Sales_price-Cost u price) x Quantity

      

01/01/2017 is the lower closest date based on the sale date. (It was the good price but it's not the current one)

How can I only catch for each SalesID the unique row associated based on this rule?

Thanks to everyone,
Floren

8 Replies
marcus_sommer

I would use the following approach of joining the SalesPrice table to the Sales table or creating a mapping-table for the merging of the tables. But this needs some further steps which are:

- extending the SalesPrice to a from- and a to-date which could be done per Peek() or Previous() ? within a sorted resident load of the data

- then using an intervalmatch to get for each day between from and to a record with a date: IntervalMatch

- combining Agency&'_'&Article_ID&'_'&NewDateField as Key_Sales

- using the extended key for the joining or mapping


and you will get the correct price for each sales-record.


- Marcus

Anonymous
Not applicable
Author

Thanks marcus for your answer,

I tried to get from date to date, but previous() gave me the opposite result.

Here is my script:

TMPSales:

Load

    *,

    Agency&'_'&Article_ID as Key_Sales,

    MakeDate([YearModifPrice],[MonthModifPrice],[DayModifPrice]) as [Start price modif date]

FROM SALESPRICE.QVD

SalesPrice:

LOAD

    Key_Sales,

    [Price modif date],

    IF(Key_Sales=Previous(Key_Sales), Previous([Start price modif date]), [Start price modif date]) as [End price modif date]

Resident TMPSales;


Result:

SalesPrice.PNG

What I expect is the opposite:

Key_salesStart price modif dateEnd price modif datePrice
300-33804023/01/201601/02/2016
300-33804001/02/201601/02/2017
300-33804001/02/201701/02/2017
300-33804018/09/201701/02/2018
300-33804001/02/2018-

I tried with Resident TMPSales order by [Start price modif date] desc; but it didnt works...

Result:

SalesPrice2.PNG

Did i miss something?

Thanks by advance


zebhashmi
Specialist
Specialist

try with Rank() on Date field

Anonymous
Not applicable
Author

Rank is a chart fonction not a script function right?
You mean i have to use it in my table result on date fields?

Anonymous
Not applicable
Author

I finally get what i needed using peek function.
So now i have from date and to date.

I have this structure now:

SalesPrice:

Key_Sales,

[Start price modif date],

[End price modif date],

[Price]


Sales:

Key_Sales,

[Sales date],

[Qty]



I tried to used matchInterval to link my SalesPrices table with my Sales table but how can I integrate this using my Key_Sales + matchInterval with [sales date] as you mentionned in your message Marcus?


marcus_sommer

I must admit that I personally nearly never use the intervalmatch else I use in the most cases a while-loop because it's more flexible in some ways and I seldom want to keep the intervalmatch-table else I usually want to merge the data to my other tables. Nevertheless I suggest the above mentioned link because HIC explained the logic behind the methods very well.

Therefore I would probably do the following:

SalesPrice:

load    

     Key_Sales,

     date([Start price modif date] + iterno() - 1) as [Sales date],

     [Price]

resident SalesPriceTemp while [Start price modif date] + iterno() - 1 <= [End price modif date];

Sales:

load

     Key_Sales,

     [Sales date],

     [Qty]

from Sales;


left join(Sales)

load * resident SalesPrice;


Instead the join you could also use a mapping whereby you would need to extend the Key_Sales with [Sales date].


- Marcus


Anonymous
Not applicable
Author

Hi!

It seems to work, I have to validate with the business! Thanks.

But I have 2.000.000 rows into sales price and it takes 2 hours to do this step:

SalesPrice:

load   

     Key_Sales,

     date([Start price modif date] + iterno() - 1) as [Sales date],

     [Price]

resident SalesPriceTemp while [Start price modif date] + iterno() - 1 <= [End price modif date];


Do you have an idea to improve this part?

Thanks again,


Florentcot

marcus_sommer

This could be speed up with an incremental load-approach - maybe with something like this:

SalesPrice:

     Key_Sales, [Start price modif date], [End price modif date], Price]

from SalesPrice.qvd (qvd);

     concatenate(SalesPrice)

load    

     Key_Sales,

     date([Start price modif date] + iterno() - 1) as [Sales date],

     [Price]

while [Start price modif date] + iterno() - 1 <= [End price modif date];

load Key_Sales, [Start price modif date], [End price modif date], [Price]

from SalesPrice where not exists(Key_Sales):

There are in general different ways how it could be applied but the main-idea is to transform only the new and/or changed record and loading the earlier ones from a qvd. Within the last two link-blocks here: Advanced topics for creating a qlik datamodel you will find various explanations and examples how incremental load-approaches could be implemented and how to keep the qvd-loadings optimized.

- Marcus