Forum Discussion
can't create a udf function
- 10 months ago
After some trying, I figured it out. Parameters are unlike measures, context transition doesn't take place automatically. So I need to force the parameter to take into consideration of the evaluation context by adding calculate(). Below is the correct DAX query
DEFINE FUNCTION MOV_AVG_UDF = (d:int64 expr) => round(averagex( window(d,REL,0,REL, summarize(fact_stocks,Dim_Date[Date],dim_stocks[Ticker]), orderby(Dim_Date[Date]), partitionby(dim_stocks[Ticker])), calculate(average(fact_stocks[Close]))), 2) EVALUATE ADDCOLUMNS( FILTER( VALUES(Dim_stocks[Ticker]), Dim_stocks[Ticker] IN {"AAPL"} ), "udf", calculate(MOV_AVG_UDF(-19)))video about parameter and context transition (min 19): https://www.youtube.com/watch?v=60jUmTxpxbw&t=815s
sorry, I don't understand your suggestion. Here is the DAX query for your reference.
Hey, Jeanxyz ,
DEFINE
FUNCTION MOV_AVG_UDF = (d:int64 expr, tbl: table expr,) =>
round(averagex(
window(d,REL,0,REL, tbl, orderby(Dim_Date[Date]), partitionby(dim_stocks[Ticker])), calculate(average(fact_stocks[Close]))),
2)
alternatively define the table as anyRef:
FUNCTION MOV_AVG_UDF = (d:int64 expr, tbl: AnyRef expr,) =>- Jeanxyz10 months agoPower Participant
I tried both. Below is the error message.
- vojtechsima10 months agoSuper User
Right, my bad, it doesn't recognise the columns Date, since you pass it the table later. You may try what Ahmedx suggested.
btw, you should pass it as you did previously with the SUMMARIZE. - Ahmedx10 months agoSuper User
pls try
FUNCTION MOV_AVG_UDF = (d:int64 VAL) =>
round(averagex(
window(d,REL,0,REL, summarize(fact_stocks,Dim_Date[Date],dim_stocks[Ticker]), orderby(Dim_Date[Date]), partitionby(dim_stocks[Ticker])), calculate(average(fact_stocks[Close]))),
2)- Jeanxyz10 months agoPower Participant
Tried val ( i think it shold be lower case?). It's the same error message, exceed 1 mil row limit.