Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Custom Column, add value from unrelated table based on date range

I'm trying to add a custom column to a table based on a date range in another table.  the tables are not related. I can do this in DAX but need it to be in the Query Editor so i can use the updated t...
  • lbendlin's avatar
    5 years ago

    Use Table.Addcolumn with a custom ColumnGenerator function that uses your logic to return rows from the second table that satisfy your condition, and then pick the required column (Label).

     

    Here is an example for the Table 1 query based on your definition for Table 2:

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31TcyMDJQitUBcQwtEDwjfRgnFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
        #"Added Custom" = Table.AddColumn(Source, "Lookup", (k)=> 
        Table.SelectRows(#"Table 2",
          each (Date.From(k[Date])>= Date.From([Start Date]) and Date.From(k[Date])<= Date.From([End Date])))
        ),
        #"Expanded Lookup" = Table.ExpandTableColumn(#"Added Custom", "Lookup", {"Label"}, {"Label"})
    in
        #"Expanded Lookup"