Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Jeanxyz
Power Participant
Power Participant

can't create a udf function

I have created a udf in a direct lake semantic model. I can add the udf to semantic model. However, when I evaluate the udf, it shows error about going beyond the 1 mn row limit. This should not happen because in my evaluation expression, I only picked ticker "AAPL". If the filter works properly, it shouldn't hit the 1 mn row limit.

 

Screenshot 2025-10-24 124907.png

 

 

 

 

Interestingly,  I have manully created the same measure without udf, it works in the front end report. Here my measure [20-day close avg_test(M)] should be equivalent to MOV_AVG_UDF(19). But for some reason, the filtering logic works differently in a udf vs a manually created measure.

20-day close avg_test(M) = round(averagex(
window(-19,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)

Screenshot 2025-10-24 124431.png

 

 

 

 

 

1 ACCEPTED SOLUTION
Jeanxyz
Power Participant
Power Participant

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)))

Screenshot 2025-10-24 145227.png

video about parameter and context transition (min 19):  https://www.youtube.com/watch?v=60jUmTxpxbw&t=815s

View solution in original post

9 REPLIES 9
Ahmedx
Super User
Super User

I think it should be written like this
or just remove expr
Screenshot_1.png

vojtechsima
Super User
Super User

Hey, @Jeanxyz ,

try to pass the table reference as a lazy parameter with tbl expr:
summarizeTable: TABLE EXPR 

and then call your function with the SUMMARIZE defined as a parameter.

sorry, I don't understand your suggestion. Here is the DAX query for your reference.

 

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", MOV_AVG_UDF(19)

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,) =>

I tried both. Below is the error message.

Screenshot 2025-10-24 144040.png

Screenshot 2025-10-24 144259.png

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)

Jeanxyz
Power Participant
Power Participant

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)))

Screenshot 2025-10-24 145227.png

video about parameter and context transition (min 19):  https://www.youtube.com/watch?v=60jUmTxpxbw&t=815s

Jeanxyz
Power Participant
Power Participant

Tried val ( i think it shold be lower case?). It's the same error message, exceed 1 mil row limit.

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. 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.