Forum Discussion

zjdaher's avatar
zjdaher
Frequent Visitor
3 years ago
Solved

Convert Sales from local currency to Dollar with missing dates

Hello,

i am new to power BI and would need some help with the below.
when the user click on the dollar slicer we need to convert our sales from local currency to Dollar currency based on the sales date.

 

to make it simple i have the following tables:

  • Date: normal calendar table which has column date
  • Sales: has sales value [Sold Value] and sales date [Posting Date]
  • Currency table: has [Currency Code], [Starting Date] and [Rate], this table has missing dates (for example holidays)

 

we managed to create it and it's calculating the rates fine and if the user doesn't choose the slicer it displays them in local currency, however in days where there's no exchage rate it giving infinity.

i understand i need to get the rate where 'Currency Rates'[Starting Date] <= 'ITEM-STOERE-SALES'[Posting Date] but how?

appreciating your help

 

 

Currency Sales =
IF(
    ISFILTERED('Currency Rates'[Currency Code]),
    SUMX('ITEM-STORE-SALES','ITEM-STORE-SALES'[Sold_Value]/
    LOOKUPVALUE('Currency Rates'[Relational Exch_ Rate Amount],
    'Currency Rates'[Currency Code],SELECTEDVALUE('Currency Rates'[Currency Code]),
    'Currency Rates'[Starting Date],'ITEM-STORE-SALES'[Posting Date]
    )  
    )
    , SUM('ITEM-STORE-SALES'[Sold_Value])
)
  • hello, i ended up repopulating the exchange table with missing dates, with rate of missing dates taking the value of previous date. the exchange table looks like this now:

     

    with tmp(plant_date) as
    (
    select cast('20210101' as datetime)
    union all
    select plant_date + 1
    from tmp
    where plant_date < '20301231'
    )

    select 'US2' as [Currency Code],dt as [Starting Date],max([Relational Exch_ Rate Amount]) over (partition by grp) [Relational Exch_ Rate Amount]
    from
    (
    select coalesce([Starting Date],plant_date) dt,[Relational Exch_ Rate Amount],
    SUM(CASE WHEN [Relational Exch_ Rate Amount] IS NOT NULL THEN 1 ELSE 0 END) OVER(
    ORDER BY plant_date
    ROWS UNBOUNDED PRECEDING) grp
    from tmp left join (select [Currency Code],[Starting Date],[Relational Exch_ Rate Amount]
    from Till.dbo.[UCCM$Currency Exchange Rate] where [Currency Code]='US2')
    #t on tmp.plant_date=#t.[Starting Date]
    ) as der
    option (maxrecursion 0)

3 Replies

  • zjdaher's avatar
    zjdaher
    Frequent Visitor

    hello, i ended up repopulating the exchange table with missing dates, with rate of missing dates taking the value of previous date. the exchange table looks like this now:

     

    with tmp(plant_date) as
    (
    select cast('20210101' as datetime)
    union all
    select plant_date + 1
    from tmp
    where plant_date < '20301231'
    )

    select 'US2' as [Currency Code],dt as [Starting Date],max([Relational Exch_ Rate Amount]) over (partition by grp) [Relational Exch_ Rate Amount]
    from
    (
    select coalesce([Starting Date],plant_date) dt,[Relational Exch_ Rate Amount],
    SUM(CASE WHEN [Relational Exch_ Rate Amount] IS NOT NULL THEN 1 ELSE 0 END) OVER(
    ORDER BY plant_date
    ROWS UNBOUNDED PRECEDING) grp
    from tmp left join (select [Currency Code],[Starting Date],[Relational Exch_ Rate Amount]
    from Till.dbo.[UCCM$Currency Exchange Rate] where [Currency Code]='US2')
    #t on tmp.plant_date=#t.[Starting Date]
    ) as der
    option (maxrecursion 0)

  • Usually you have monthly exchange rates and you create a "CurrencyYearMonth" composite key that you then use to link to your fact table.  If your exchange rates have start dates then this becomes a lot more complex.

     

    Please provide sample data that covers your issue or question completely.
    https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216
    Please show the expected outcome based on the sample data you provided.

    https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523