Forum Discussion

Arnault_'s avatar
Arnault_
Icon for Resolver III rankResolver III
7 years ago
Solved

MIN date using M

Dear All, My business case is the following : I have orders identified by a specific order_id and each order can be received in several sequences (reception_id) with different reception dates. I wa...
  • Anonymous's avatar
    Anonymous
    7 years ago

     

    Hi,

     

    I have pasted your data into an excel sheet and loaded it into power bi and edited the Power Query to identify the first reception date. Given below is the Power Query.

     

    let
        Source = Excel.Workbook(File.Contents("C:\Users\R\Orders.xlsx"), null, true),
        Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"order_id", type text}, {"reception_id", type text}, {"reception_date", type date}}),
    
    //added codes starts here..
        TempTable = #"Changed Type",
        Orders = #"Changed Type",
    //this function X filters the table based on the parameter "order_id"
    
        X = (y as table,z as text ) as table => Table.SelectRows(y,each Record.Field(_,"order_id")=z),
    
    //Adds the column 'first_reception' to the table.
    
        AddColumn = Table.AddColumn(
            Orders,
            "first_reception",
            each Record.Field(_,"reception_date") = List.Min(  // comparison of current row's reception date with the minimum of all receptions dates of that order.
                Table.Column( //Table.Column converts the column to a list which is passed on to List.Min()
                    X(TempTable,Record.Field(_,"order_id")),"reception_date" // filters the table bsed on current record's order_id and passing it on to Table.Column() funciton
                )
            )
        )
    in
        AddColumn