Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
DimaMD
Solution Sage
Solution Sage

Calculate Exchange rate for every day

Hello community! I need help in solving following problem

 

We have a table with currency exchange rate for every day, but in some cases fields with exchange rate in some days are empty.

 

I use those exch. rates to calculate different currencies in specific days. But when cell of rate is empty we can't make calculation, so in that cases we need to take exchange rate from the closest day where cell have data. How can propery fill those empty cells by taking the closest filled cell of specific currency, where the date of filled cell is closest to empty one? How can I write such calculateble collum? 
file exemple


Screenshot_2.jpg

image (7).png


__________________________________________

Thank you for your like and decision

__________________________________________

Greetings from Ukraine

To help me grow PayPal: embirddima@gmail.com
1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@DimaMD  can you try out this measure, seems to be working for me

 

 

m2 = 
VAR _1 =
    ADDCOLUMNS (
        Costs,
        "dt",
--grabs the date from Exchange Rate on Currency+Date Partition
            VAR _dateFromExchangeRate =     
                CALCULATE (
                    CALCULATE (
                        MAX ( 'Exchange rate'[Date] ),
                        TREATAS (
                            SUMMARIZE ( Costs, Costs[Date], Costs[Currency] ),
                            'Exchange rate'[Date],
                            'Exchange rate'[Currency]
                        )
                    )
                )
            RETURN
--if the dateFromExchangeRate is blank, grabs the immediately preceding
-- date from Exchange Rate that has valid Exchange Rate ~ LASTNONBLANK 'Exchange rate'[Date]
-- done on currency partition
-- if the dateFromExchangeRate is not blank returns dateFromExchangeRate
                IF (
                    _dateFromExchangeRate = BLANK (),    
                    MAXX (                               
                        FILTER (                         
                            'Exchange rate',             
                            'Exchange rate'[Currency] = EARLIER ( [Currency] )
                                && 'Exchange rate'[Date] <= EARLIER ( [Date] )
                        ),
                        'Exchange rate'[Date]
                    ),
                    _dateFromExchangeRate
                )
    )
VAR _2 =
    ADDCOLUMNS (                                    
        _1,  
--looks up the 'Exchange rate'[Exchange rate] based on the date obtained in the previous step                                       
        "val",
            MAXX (
                FILTER (
                    'Exchange rate',
                    'Exchange rate'[Currency] = EARLIER ( [Currency] )
                        && 'Exchange rate'[Date] = EARLIER ( [dt] )
                ),
                'Exchange rate'[Exchange rate]
            )
    )
RETURN
    SUMX(_2,[val]*[Costs])

 

 

The above generates this

smpa01_1-1637775336083.png

 

if you look at this block

smpa01_2-1637775395087.png

 

The mesaure grabbed 26.33 from 2021-10-29 and multiplied that 

smpa01_3-1637775466835.png

The pbix is attached

 

 

 

 

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

6 REPLIES 6
smpa01
Super User
Super User

@DimaMD  can you try out this measure, seems to be working for me

 

 

m2 = 
VAR _1 =
    ADDCOLUMNS (
        Costs,
        "dt",
--grabs the date from Exchange Rate on Currency+Date Partition
            VAR _dateFromExchangeRate =     
                CALCULATE (
                    CALCULATE (
                        MAX ( 'Exchange rate'[Date] ),
                        TREATAS (
                            SUMMARIZE ( Costs, Costs[Date], Costs[Currency] ),
                            'Exchange rate'[Date],
                            'Exchange rate'[Currency]
                        )
                    )
                )
            RETURN
--if the dateFromExchangeRate is blank, grabs the immediately preceding
-- date from Exchange Rate that has valid Exchange Rate ~ LASTNONBLANK 'Exchange rate'[Date]
-- done on currency partition
-- if the dateFromExchangeRate is not blank returns dateFromExchangeRate
                IF (
                    _dateFromExchangeRate = BLANK (),    
                    MAXX (                               
                        FILTER (                         
                            'Exchange rate',             
                            'Exchange rate'[Currency] = EARLIER ( [Currency] )
                                && 'Exchange rate'[Date] <= EARLIER ( [Date] )
                        ),
                        'Exchange rate'[Date]
                    ),
                    _dateFromExchangeRate
                )
    )
VAR _2 =
    ADDCOLUMNS (                                    
        _1,  
--looks up the 'Exchange rate'[Exchange rate] based on the date obtained in the previous step                                       
        "val",
            MAXX (
                FILTER (
                    'Exchange rate',
                    'Exchange rate'[Currency] = EARLIER ( [Currency] )
                        && 'Exchange rate'[Date] = EARLIER ( [dt] )
                ),
                'Exchange rate'[Exchange rate]
            )
    )
RETURN
    SUMX(_2,[val]*[Costs])

 

 

The above generates this

smpa01_1-1637775336083.png

 

if you look at this block

smpa01_2-1637775395087.png

 

The mesaure grabbed 26.33 from 2021-10-29 and multiplied that 

smpa01_3-1637775466835.png

The pbix is attached

 

 

 

 

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hi, @smpa01 
The mesure works correctly and  helped me a lot.

 

I need to make some more calculations, and for that purpose we need to make a collum which will display all cells in USD. We need to multiply mesure M2 with USD currency, so we made a collum with USD currency, but the problem is when we have a row in table, that displays EUR, PLN etc, the cell stays empty. We need to fill all empty cells, no matter what currency was in a row, with USD


__________________________________________

Thank you for your like and decision

__________________________________________

Greetings from Ukraine

To help me grow PayPal: embirddima@gmail.com

@DimaMD  Can you please create a new thread on this?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Hi @smpa01 New post here


__________________________________________

Thank you for your like and decision

__________________________________________

Greetings from Ukraine

To help me grow PayPal: embirddima@gmail.com

Hi, @smpa01 
it's something an incredible, measure works
I am very grateful to you for your help


__________________________________________

Thank you for your like and decision

__________________________________________

Greetings from Ukraine

To help me grow PayPal: embirddima@gmail.com
amitchandak
Super User
Super User

@DimaMD , the first select date the conversion

 

example new column

Var _1 = maxx(filter(Rate, Rate[Currency] = Cost[Currency] && Rate[Date]<= Cost[Date]) , Rate[Date])
return
maxx(filter(Rate, Rate[Currency] = Cost[Currency] && Rate[Date] =_1) , Rate[Date])

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors