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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
DanielCruzO
New Member

Get a value from another table base on date range

Hi Everyone. 

I am struggeling with the following example.

I have a table with Currency Exchange rates assigned base on dates, the format of my table is the following:

i have a column named "Starting_date" and my dates are the following:

DanielCruzO_1-1688054285616.png

for example, if any transaction is done in 2022-04-01 or before the next date which is 2022-05-01, the rate that we should get is 1.2658.

i need this lookup in another table, where my transactions dates could fall in between any of those dates and retrive the correct currency exch rate.

could anybody help me with this code? i am getting errors in all the ones i have tried.

 

thank you!  

 

1 REPLY 1
edhans
Super User
Super User

Try this code @DanielCruzO 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtM31DcyMDJW0lEKDXZRitWJVjLRNzJGFzPVNzRFEYsFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Currency = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    AddExchangRate =
        Table.AddColumn(
            #"Changed Type",
            "Exchange Rate",
            each
                let
                    varCurrency = [Currency],
                    varStartDate = Date.StartOfMonth([Date]),
                    varEndDate = Date.EndOfMonth([Date])
                in
                Table.SelectRows(
                    ExchangeRates,
                    each [Currency] = varCurrency
                        and [Date] >= varStartDate
                        and [Date] <= varEndDate
                )
        ),
    #"Expanded Exchange Rate" = Table.ExpandTableColumn(AddExchangRate, "Exchange Rate", {"Rate"}, {"Rate"})
in
    #"Expanded Exchange Rate"

 

Here is the code for the "ExchangeRate" table referred to.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtU31DcyMDJW0lEKDXYBkoZKsTrRSmbowkZgYXN0YWOwsIm+EaqwiVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Currency = _t, Rate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Rate", Currency.Type}})
in
    #"Changed Type"


Here is how it looks:

Exchange Rate table:

edhans_0-1688056090602.png

Final Transaction Table:

edhans_1-1688056110643.png

What it does is stores the Currency type in varCurrency. Then it takes whatever date is i the transaction table and creates a start and end date for the month. so Row 2 has April 23, 2023. This will put April 1, 2023 in varStartDate and April 30, 2023 in varEndDate.

 

I then added a new column with a Table.SelectRows against the Exchange Rate with the filters to select the proper currency and the date ranges.

That brings in a table:

edhans_3-1688056272890.png

Just expand the Rate columnn from the Exchange Rate column.

Note: this will not perform well as you get into the tens or hundreds of thousands of rows. Power Query isn't great at table scans. DAX does it faster. Much MUCH faster. It is designed for it.

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors