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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
teresating886
Frequent Visitor

M function to look up a value in another table

Hi, 

I have a data table (1. Data table). I want to add a column in Power Query called "Exchange Rate", and the rate values come from the 2nd table - Currency Exchange Rate. 

 

Can anyone help me with the M function to do it please? Thank you.


1. Data table

Reporting DateAmount SpentCurrency
9/1/202252.1CAN
9/3/202210.06CAN
9/14/20225.74CAN
9/11/202268.9CAN
9/12/202277.3CAN

Data table M code:

 

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

 

2. Currency Exchange Rate 

CurrencyExchange_FromCurrencyCurrencyExchange_ToCurrencyCurrencyExchange_FromDateCurrencyExchange_ToDateCurrencyExchange_Rate
CA$US$9/12/20229/13/20220.7704
CA$US$9/9/20229/11/20220.7672
CA$US$9/8/20229/8/20220.7624
CA$US$9/7/20229/7/20220.7596
CA$US$9/6/20229/6/20220.7611
CA$US$9/2/20229/5/20220.7621
CA$US$9/1/20229/1/20220.7595

Currency Exchange Rate M Code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc5BCoAwDATAvwSPxTbRJs1RfIJ4Kv3/N0RBkmovyx6GZWuFfZsgwHncqVEjJaKnIr09zcJC0MJXF9PqMa0DLIaLw1l5gNmw+GXEASbD3N0YYTRM3Y38wyUuTmM3LdDaBQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CurrencyExchange_FromCurrency = _t, CurrencyExchange_ToCurrency = _t, CurrencyExchange_FromDate = _t, CurrencyExchange_ToDate = _t, CurrencyExchange_Rate = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"CurrencyExchange_FromCurrency", type text}, {"CurrencyExchange_ToCurrency", type text}, {"CurrencyExchange_FromDate", type date}, {"CurrencyExchange_ToDate", type date}, {"CurrencyExchange_Rate", type number}})
in
    #"Changed Type"

 

 

1 REPLY 1
Ashish_Mathur
Super User
Super User

Hi,

It is easier to get the result as a calculated column formula using the DAX language.  Would you be interested in that (instead of an M code)?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.