Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.
I have a table called Indicators withpivoted MPs, i.e. 1,2,3... 8 and this list will expand so MP9, 10...etc.
i want to create a new column in the same Indicators table that calculate the max of the values for columns 1...8 which is taken from another table called Open_MPs which has recent values.
So basically i need a way to List.Max with list of columns fetched from another table. Ca someone help me?
Appreciate it guys!
Solved! Go to Solution.
Table.AddColumn(
Indicators, "max",
(x) =>
List.Max(
Record.FieldValues(
Record.SelectFields(x, Open_MPs[Open_MPs])
)
)
)
Hi, @AlienSx
let functions =
[Sum="List.Sum",Max="List.Max", Min="List.Min", Average="List.Average",Latest Value="List.Last"],
result = Function.Invoke(
Expression.Evaluate(Record.Field(functions, [calculationMethod]), #shared) ,
{List.RemoveNulls(Record.FieldValues(Record.SelectFields(_,Open_MPs[OpenMPs])))}
)
in result
Stéphane
= Table.AddColumn(Pivot_MPs, "Result", each
try Function.Invoke(
Record.Field(
[Sum=List.Sum, Maximum value=List.Max, Minimum value=List.Min, Average=List.Average, Latest value=List.Last, Manual value=List.Last],
[calculationMethod]??"Manual value"),
{List.RemoveNulls(Record.FieldValues(Record.SelectFields(_,Open_MPs[openMPs])))})
otherwise null)
Stéphane
Thank you @AlienSx this works perfectly, i also further developed it to include multiple calculations as below
@Bassil74 also, try this instead
let
functions =
[Sum = "List.Sum",
Maximum value = "List.Max",
Minimum value = "List.Min",
Average = "List.Average",
Latest value = "List.Last"],
result =
Expression.Evaluate(
Record.Field(functions, [calculationMethod]) &
"(List.RemoveNulls(Record.FieldValues(Record.SelectFields(_, Open_MPs[openMPs]))))"
)
in
result
This is working perfect. Now something new introduced, how to add to include to this code if [calculationMethod] = null then to run List.Last
= Table.AddColumn(Pivot_MPs, "Result", each
try Function.Invoke(
Record.Field(
[Sum=List.Sum, Maximum value=List.Max, Minimum value=List.Min, Average=List.Average, Latest value=List.Last, Manual value=List.Last],
[calculationMethod]),
{List.RemoveNulls(Record.FieldValues(Record.SelectFields(_,Open_MPs[openMPs])))})
otherwise null)
= Table.AddColumn(Pivot_MPs, "Result", each
try Function.Invoke(
Record.Field(
[Sum=List.Sum, Maximum value=List.Max, Minimum value=List.Min, Average=List.Average, Latest value=List.Last, Manual value=List.Last],
[calculationMethod]??"Manual value"),
{List.RemoveNulls(Record.FieldValues(Record.SelectFields(_,Open_MPs[openMPs])))})
otherwise null)
Stéphane
I want to feedback on teh above, i found my query, which connects to API source, became very slow running the above part, it takes more than 15 mins to makethe calculations of ~ 200 records so i wonder if there is more efficient method than the above?
I tried the the reviewed code but it is throwing this error
Expression.Error: [1,1-1,10] The name 'List.Last' doesn't exist in the current context.
Details:
[List]
@Bassil74 my bad, looks like it's not that easy as I expected. Have to get back to school again and learn more about environments in M. Disregard my code, please.
Hi, @AlienSx
let functions =
[Sum="List.Sum",Max="List.Max", Min="List.Min", Average="List.Average",Latest Value="List.Last"],
result = Function.Invoke(
Expression.Evaluate(Record.Field(functions, [calculationMethod]), #shared) ,
{List.RemoveNulls(Record.FieldValues(Record.SelectFields(_,Open_MPs[OpenMPs])))}
)
in result
Stéphane
This is working code, thank you @slorin . except that i gort this error message due to null values in the field [calculationMethod]:
We cannot convert the value null to type Text.
Details:
Value=
Type=[Type]
Use try ... otherwise
Solution without Expression.Evaluate
try Function.Invoke(
Record.Field(
[Sum=List.Sum, Max=List.Max, Min=List.Min, Average=List.Average, Latest=List.Last],
[calculationMethod]),
{List.RemoveNulls(Record.FieldValues(Record.SelectFields(_,Open_MPs[OpenMPs])))})
otherwise null
Stéphane
Thank you Stéphane, iam getting error at the try step when tried to implement your proposal
... each
let
try ...
Thank you AlienSx, well done
Table.AddColumn(
Indicators, "max",
(x) =>
List.Max(
Record.FieldValues(
Record.SelectFields(x, Open_MPs[Open_MPs])
)
)
)