Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Combine two tables based on date ranges.

I'm not sure if anyone is able to assist to combine two tables based on different date ranges or having a lookup in dax to add values to an existing table based on date ranges. This has to do with fo...
  • AlienSx's avatar
    3 years ago

    Anonymous make sure you use correct dates (you have 31st of April in your data). Also make sure you have dates in rates table earlier than min date in GL table. Replace your_rates_table and your_GL_table in code below with correct references to your tables.

     

    let
        // this is your table with rates. Had to trim ccy column later.
        s_rates = your_rates_table,
        date_type = Table.TransformColumnTypes(s_rates, {{"Effective Date", type date}}, "en-AU"),
        trimmed = Table.TransformColumns(date_type,{{"From Currency", Text.Trim, type text}}),
        // this is your table with amounts
        before = your_GL_table,
        b_type = Table.TransformColumnTypes(before, {{"GL Date", type date}}, "en-AU"),
        // make a record with ccy name fields and list of dates and rates sorted by date
        rates = 
            Table.Group(
                trimmed, "From Currency", 
                {{"rates", each List.Sort( List.Zip({_[Effective Date], _[Multiplier]}), {(x) => x{0}, Order.Descending})}}
            ),
        rec = Record.FromList(rates[rates], rates[From Currency]),
        // add Multiplier column.
        after = 
            Table.AddColumn(
                b_type, "Multiplier",
                each [a = Record.FieldOrDefault(rec, [Base Currency], null),
                b = a{List.PositionOf(a, [GL Date], Occurrence.First, (x, y) => x{0} <= y)},
                c = if b = -1 then null else b{1}][c]
            )
    in
        after