Forum Discussion
pvgadvogados
9 years agoFrequent Visitor
New column based on calculations between from same and others tables
I have 3 tables and I need to create a column in the last table with some conditions: TABLE 1 Date Exchange ID_EXCHANGE Exchange Rates 25/04/2017 1 3,25 25/04/2017 2...
dkay84_PowerBI
Microsoft Employee
9 years agoI'm sure a DAX ninja will solve this more elegantly than I have, but I used the query editor to do a few joins to get the appropriate exchange rate based on date and currency type, then added a custom column for the conditional math.
Here is the M code (for tables 1 just add the code after the "Source" or "Changed Type" line, as my data was hard coded based on your example):
// Table1
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtE3MtU3MjA0V9JRMgRiYz0jU6VYHVQJI7CEMUzcDFWDKYY4RL2hEUzCHFWDBYY4VIOlUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Date Exchange" = _t, ID_Exchange = _t, #"Exchange Rates" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date Exchange", type date}, {"ID_Exchange", Int64.Type}, {"Exchange Rates", type number}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type",{"ID_Exchange"},Table2,{"ID_Exchange"},"NewColumn",JoinKind.LeftOuter),
#"Expanded NewColumn" = Table.ExpandTableColumn(#"Merged Queries", "NewColumn", {"ID_Currency"}, {"ID_Currency"})
in
#"Expanded NewColumn"
// Final Value
let
Source = Table.NestedJoin(Table3,{"Date Sales", "ID_Currency"},Table1,{"Date Exchange", "ID_Currency"},"NewColumn",JoinKind.LeftOuter),
#"Expanded NewColumn" = Table.ExpandTableColumn(Source, "NewColumn", {"Exchange Rates"}, {"Exchange Rates"}),
#"Added Custom" = Table.AddColumn(#"Expanded NewColumn", "Final Value", each if [ID_Currency] <> "R" then [Value]*[Exchange Rates] else [Value])
in
#"Added Custom"