Forum Discussion
Custom Column to have a record of calculation from multiple columns
- 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 AddColI hope this is helpful
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
Hi, I have my values a nullable
But while running the above query I am getting this error:
- m_dekorte2 years ago
Resident Rockstar
Hi pranay_singh
OPTION 1: deal with nulls, prevent errors
"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."
NetworkDays = (StartDate as nullable datetime, EndDate as nullable datetime, optional HolidayList as list) as number =>
Within the function body you can determine how to deal with nulls, for example wrapping a conditional statement :
NetworkDays = (StartDate as nullable datetime, EndDate as nullable datetime, optional HolidayList as list) as number =>
if StartDate = null then null /* just for illustration */
else if EndDate = null then 0 /* just for illustration */
else let
/* your function body goes here*/
in
OPTION 2: handle errors
An alternative is to leave everything as is and incorporate error handling by wrapping try-otherwise around the function invocation, for example. This allows you to specify a default value in case an error is encountered, to illustrate:
try NetworkDays([Start], [End], Holidays) otherwise null
I hope this is helpful