Forum Discussion

Milejdi8's avatar
Milejdi8
Frequent Visitor
4 years ago
Solved

Need custom column to return value based on comparison of dates and other columns

I've been struggling with this for some time and I'm not even sure this is possible.  I have a query with the list of orders, as in table example below. I am trying to create a custom column base...
  • Knighthawk's avatar
    4 years ago

    Hi Milejdi8,

     

    I have provided code below that I believe will provide the results that you are looking for (of course you will need to adjust the "Source" to match your data table):

     

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Customer", type text}, {"Product", type text}, {"Ordered", Int64.Type}, {"Delivered", Int64.Type}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
        #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 1, 1),
        #"Merged Queries" = Table.NestedJoin(#"Added Index1",{"Index"},#"Added Index1",{"Index.1"},"Added Conditional Column",JoinKind.LeftOuter),
        #"Expanded Added Conditional Column" = Table.ExpandTableColumn(#"Merged Queries", "Added Conditional Column", {"Delivered"}, {"Delivered.1"}),
        #"Sorted Rows" = Table.Sort(#"Expanded Added Conditional Column",{{"Index", Order.Ascending}}),
        #"Added Conditional Column" = Table.AddColumn(#"Sorted Rows", "Custom", each if [Index] = 0 or ([Delivered] = null and [Delivered.1] <> null)  then "Original Order" else "duplicate"),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Conditional Column",{"Date", "Customer", "Product", "Ordered", "Delivered", "Custom"})
    in
        #"Removed Other Columns"

     

     

     

     

     

     

    I hope this helps!  🙂