Forum Discussion

pranay_singh's avatar
pranay_singh
Frequent Visitor
2 years ago
Solved

Custom Column to have a record of calculation from multiple columns

Hello,   I have a question and looking for a solution. I am not sure even if its possible. I have a function which I use to calculate NetworkDays = (StartDate as datetime, EndDate as datetime, Hol...
  • m_dekorte's avatar
    2 years ago

    Hi pranay_singh,

     

    You can invoke this custom function on your table to add the Desired Column to your table.

    Before invoking it, replace NetworkDays with the actual name of your function query to calculate networkdays. Furthermore, make sure your input parameter types are nullable - if that is not already the case. 

     

    I would also suggest to load the Holidays list into memory and refer to that instead of calling Table.Column for each row within this function. Simply, create a new query called; HolidayList and assign that this value: List.Buffer(Table.Column(#"US Holidays","US Holidays")) then update the function logic below.

     

    (t as table ) as table =>
    let
        First = List.Select( Table.ColumnNames( t ), each Text.Contains(_, "__FIRST" )),
        Transform = List.Transform( First, each 
            let s = Text.Trim( Text.BetweenDelimiters(_, "_Planning__", " Real")) in 
            {_, Replacer.ReplaceText(Replacer.ReplaceText(_, "FIRST", "LAST"), " start ", " end "), Text.AfterDelimiter(s, "_")} 
        ),
        AddCol = Table.FromRecords( List.Transform( Table.ToRecords( t ), each _ & [Desised Column =
                List.Accumulate(
                    Transform,
                    [],
                    (s, a)=> Record.AddField(s, 
                        a{2}, 
                        /* replace NetworkDays with the actual name of your function query! */
                        /* make sure your parameters are of a nullable type*/
                        NetworkDays( Record.Field(_,a{0}), Record.Field(_,a{1}), 
                            Table.Column(#"US Holidays","US Holidays") /* HolidayList */
                        )
                    )
            )]
        ))
    in
        AddCol

     

    I hope this is helpful