Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
grajmca_sgp123
Creator
Creator

exchange rate calculation on Master calendar table

Hi All,

I have two tables Orders and Currency table. Orders values are in local currency format, I want to convert this local currency to USD format by using Currency table. Here below are the sample tables

Orders:  we have values for every date

OrderDateSalesLocal Currency
6/15/2014100EUR
8/22/2014120EUR
9/14/201480EUR
11/11/2014200EUR
12/2/2014150EUR
1/14/2015300EUR
2/28/201550EUR
6/25/201570EUR
8/15/2015100EUR

Currency table: we have exchange rate values for every quarter.

From DateCurrencyExachange rate
6/2/2014EUR0.55
9/1/2014EUR1.24
12/1/2014EUR0.00123
3/1/2015EUR0.34
6/1/2015EUR0.5

I want to join these tables based on From Date and Order Date fields, but I do not have all FromDate values in Currency table  so we need to create master calendar data for From Date. I am fine with Master Calendar table I know how to create it but my requirement is ,if OrderDate fall between FromDate quarter dates i need to pick that date related exchange rate and multiple with sales.

Example:

OrderDateSalesLocal Currency
6/15/2014100EUR
8/22/2014120EUR
9/14/201480EUR
11/11/2014200EUR
12/2/2014150EUR
1/14/2015300EUR
2/28/201550EUR
6/25/201570EUR
8/15/2015100EUR

for example in above table first order(6/15/2015) fall between 6/2/2014 and 9/31/2014(From date field),So i need to pick exchange rate related to FromDate quarter and calculate sales like Sum(100 * 0.55)

For 3rd order(9/14/2014) fall between 9/1/2014 and 12/1/2014. So sum value is Sum(80*1.24)

same way for other date also.



output looks like this.


OrderDateSales
6/15/2014100*0.55
8/22/2014120*0.55
9/14/201480*1.24
11/11/2014200* 1.24
12/2/2014150*0.00123
1/14/2015300* 0.00123
2/28/201550* 0.00123
6/25/201570*.05
8/15/2015100*.50



please let me know your inputs here,Thanks in advance

1 Solution

Accepted Solutions
pokassov
Specialist
Specialist

Yep. Your Orders table have to have fields  Dateq,Currencyq.

See attach.

View solution in original post

10 Replies
pokassov
Specialist
Specialist

Hello!

1. change your currency table:

NullAsValue [FromDate], [Till Date];

t1:

load

     *,

     date(previos(From Date)-1)               As [Till Date]

resident [Currency table]

order by [From Date] desc;

drop table [Currency table];

2. Use intervalmatch.


left join (Orders)

intervalmatch (OrderDate)

load FromDate], [Till Date]

Resident t1;


left join (Orders)

load FromDate], [Till Date], [Exachange rate]

Resident t1;


drop table t1;


drop fields [FromDate], [Till Date];


grajmca_sgp123
Creator
Creator
Author

Thank you for your quick reply let me try

Anonymous
Not applicable

HI Rajesh,

Please try:

Exchange_Rates:

LOAD * Inline

[

From Date,Currency,Exchange rate

6/2/2014,EUR,0.55

9/1/2014,EUR,1.24

12/1/2014,EUR,0.00123

3/1/2015,EUR,0.34

6/1/2015,EUR,0.5

];

NoConcatenate

Temp:

     Load [From Date],

     Currency,

     [Exchange rate]

     Resident Exchange_Rates ;

DROP Table Exchange_Rates;

     MinMaxFromDate:

     Load Min([From Date]) as MinDate,

     Max([From Date]) as MaxDate resident Temp;

     Let vMinDate = Peek('MinDate',-1,'MinMaxDate') - 1;

     Let vMaxDate = Peek('MaxDate',-1,'MinMaxDate')    ;

     Join (Temp)

     Load Date(recno()+$(vMinDate)) as Date Autogenerate vMaxDate - vMinDate;

     Exchange_Rates:

     NoConcatenate Load [From Date],

    Currency,

          If( IsNull( [Exchange rate] ), Peek( [Exchange rate] ), [Exchange rate] ) as [Exchange rate]

          Resident Temp

          Order By [From Date] ; /* so that above values can be propagated downwards */

     Drop Table MinMaxFromDate, Temp;

    

Temp:

Load Distinct Currency as Currencies

Resident Exchange_Rates;

For i=0 to FieldValueCount('Currencies')-1

Let vCurrency=Peek('Currencies', $(i), 'Temp');

Exchange_Rate_Map:

Mapping Load [From Date] as OrderDate,

[Exchange rate]

Resident Exchange_Rates

Where Currency = '$(vCurrency)';

Next

Drop Tables Temp,Exchange_Rates;

Sales:

LOAD *,Sales * [Exchange rate] as Amount;

LOAD *,

ApplyMap('Exchange_Rate_Map',OrderDate) as [Exchange rate];

LOAD * Inline

[

OrderDate,Sales,Currency

6/15/2014,100,EUR

8/22/2014,120,EUR

9/14/2014,80,EUR

11/11/2014,200,EUR

12/2/2014,150,EUR

1/14/2015,300,EUR

2/28/2015,50,EUR

6/25/2015,70,EUR

8/15/2015,100,EUR

];

Regards

Neetha

grajmca_sgp123
Creator
Creator
Author

Hi Sergey,

Looks we have close to your solution but joining should be based on OrderDate & CurrencyCode(Concatinate field) instead of OrderDate .

So how can we achieve this.

pokassov
Specialist
Specialist

Hi!

You can add key into intervalmatch

Inner Join IntervalMatch (Date,CurrencyCode) LOAD FirstDate, LastDate, CurrencyCode resident Currency.

And take in mind you should create interval data for each Currency. Something like this:

load

    *,

  if(previous([Currency Code])=[Currency Code],date(previos(From Date)-1)              As [Till Date]

resident [Currency table]

order by

[Currency Code],

[From Date] desc;

grajmca_sgp123
Creator
Creator
Author

Hi Sergey,

i am getting blank in exchange rate field. Kindly find the attached file for script which provided,correct me if i miss any thing.

pokassov
Specialist
Specialist

Hi!

Can you check with your qvd resources?

grajmca_sgp123
Creator
Creator
Author

we are getting field not found error

Field not found

Left Join (Orders)

IntervalMatch (Dateq,Currencyq) LOAD DateStart, DateEnd, Currencyq resident temp

pokassov
Specialist
Specialist

Yep. Your Orders table have to have fields  Dateq,Currencyq.

See attach.