Close Menu
    Trending
    • Why the Sophistication of Your Prompt Correlates Almost Perfectly with the Sophistication of the Response, as Research by Anthropic Found
    • From Transactions to Trends: Predict When a Customer Is About to Stop Buying
    • America’s coming war over AI regulation
    • “Dr. Google” had its issues. Can ChatGPT Health do better?
    • Evaluating Multi-Step LLM-Generated Content: Why Customer Journeys Require Structural Metrics
    • Why SaaS Product Management Is the Best Domain for Data-Driven Professionals in 2026
    • Stop Writing Messy Boolean Masks: 10 Elegant Ways to Filter Pandas DataFrames
    • What Other Industries Can Learn from Healthcare’s Knowledge Graphs
    ProfitlyAI
    • Home
    • Latest News
    • AI Technology
    • Latest AI Innovations
    • AI Tools & Technologies
    • Artificial Intelligence
    ProfitlyAI
    Home » A Real-World Example of Using UDF in DAX
    Artificial Intelligence

    A Real-World Example of Using UDF in DAX

    ProfitlyAIBy ProfitlyAIOctober 27, 2025No Comments9 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Introduction

    The function of user-defined capabilities was launched as a primary preview model with the September 2025 launch.

    This function allows us to encapsulate enterprise logic in capabilities, which could be referred to as like every other normal capabilities.

    On this piece, I’ll display find out how to use this function with a real-world instance: calculating a forecast based mostly on inflation charges.

    You will note find out how to create a easy operate and deal with a extra complicated state of affairs.

    Situation

    Let’s think about an organization that wishes to forecast its revenue with an inflation simulation.

    They wish to simulate how totally different inflation charges have an effect on their month-to-month revenue.

    For simplicity, we ignore seasonality and use the final identified month-to-month gross sales quantity to calculate the long run revenue for the remainder of the yr.

    The consumer should be capable of set an inflation price and see how the numbers change.

    Put together the info mannequin

    Now, it depends upon whether or not I begin with a brand new Energy BI file and cargo the info or add this performance to an present one.

    The very first thing to do is to activate the Preview function:

    Determine 1 – Allow the Preview function for user-defined capabilities (Determine by the Creator)

    You is likely to be pressured to restart Energy BI Desktop after enabling it.

    For an present Energy BI file, we have to set the proper compatibility degree to create user-defined capabilities (UDFs).

    You possibly can both create a dummy operate, which can routinely improve the compatibility degree, or use Tabular Editor to set it to not less than 1702:

    Determine 2 – Use Tabular Editor to improve the compatibility degree of the database (Determine by the Creator)

    You possibly can enter 1702 within the marked area and reserve it.

    I’ll display find out how to create a easy UDF later on this piece.

    Please go to the Microsoft documentation to study extra about creating a brand new UDF in Energy BI Desktop. You’ll find the hyperlink within the references part on the finish of this text.

    Including the speed choice

    Because the consumer should be capable of choose the inflation price, I add a parameter to the info mannequin:

    Determine 3 – Add a parameter to the info mannequin for a numeric vary (Determine by the Creator)

    After clicking on “Numeric vary” I fill out the shape:

    Determine 4 – Establishing the parameter for a share vary (Determine by the Creator)

    Since I wish to management the share, I set the vary to -0.02 to 0.05, which corresponds to -2% to five%.

    After a number of seconds, the brand new slicer was routinely added to the report web page.

    However it’s exhibiting solely the decimal numbers.
    I have to change the quantity format to see percentages:

    Determine 5 – Format the column of the parameter to a share (Determine by the Creator)

    Now the slicer reveals the quantity as wanted:

    Determine 6 – The slicer with the numbers as a share (Determine by the Creator)

    Now it’s prepared to make use of.

    Write the primary operate

    First, let’s create a UDF to return the chosen Charge.

    I desire writing it in Tabular Editor, as a result of its DAX editor is way sooner than Energy BI Desktop.

    However you’ll be able to create it within the DAX Question view in Energy BI Desktop as properly.

    In Tabular Editor, I am going to the Capabilities Node, right-click on it, and choose “New Person-Outlined operate”:

    Determine 7 – Create a brand new UDF in Tabular Editor (Determine by the Creator)

    Now I can set a reputation.

    For this primary one, I set “ReturnRate”.

    That is the code for the operate:

    (
        Charge : DECIMAL VAL
    )
    
    =>
    Charge

    Inside the brackets, I outline the Enter parameter.

    After the => I can enter the DAX code for the operate.

    On this case, I return the Enter parameter.

    Now, I create a measure to make use of this Operate:

    Get Inflation price = ReturnRate([Inflation rate Value])

    The measure [Inflation rate Value] was created once I created the parameter to pick out the Inflation price.

    Once I add a card and assign the brand new measure to it, I’ll see the chosen worth from the slicer:

    Determine 8 – Two examples exhibiting that the Measure that calls the capabilities returns the chosen inflation price (Determine by the Creator)

    OK, that is an elementary operate, nevertheless it’s solely as an instance the way it works.

    Write the true operate

    You might need seen the key phrase VAL within the parameter’s definition.

    As you’ll be able to learn within the two articles beneath in additional element, we’ve two modes to cross parameters:

    • VAL: Go the content material of the parameter as is.
    • EXPR: Go the parameter as an expression, which can be utilized throughout the operate like a normal Measure.

    Within the following operate, I take advantage of each of them.

    Right here is the entire code for the operate MonthlyInflation:

    (
        Charge : DECIMAL VAL
        ,InputVal : EXPR
    )
    =>
    
    VAR CurrentMonth =  MAX( 'Date'[MonthKey] )
    
    VAR LastMonthWithData = CALCULATE(
                                    LASTNONBLANK( 'Date'[MonthKey]
                                        , InputVal
                                    )
                                , ALLEXCEPT( 'Date', 'Date'[Year] )
                            )
                            
    VAR LastValueWithData = CALCULATE(InputVal
                                            ,ALLEXCEPT('Date', 'Date'[Year])
                                            ,'Date'[MonthKey] = LastMonthWithData
                                        )
                                
    VAR MonthDiff = CurrentMonth - LastMonthWithData
    VAR End result = IF(MonthDiff<=0
                        ,InputVal
                        ,(1 + ( Charge * MonthDiff ) ) * LastValueWithData
                        )
    
                                        
    RETURN
        End result
    

    The primary parameter of the operate is as earlier than.

    The second parameter would be the expression of the enter measure.

    Inside the operate, I can use the parameter identify to alter the filter context and different issues. I have to set the parameter as EXPR once I have to work on this approach throughout the operate.

    The operate performs the next steps:

    1. I get the very best MonthKey and retailer it within the variable CurrentMonth
      The content material is the month of the present filter context within the numerical type YYYYMM.
    2. I get the most recent month of the present yr with a price from the enter parameter (measure) and retailer it into the variable LastMonthWithData
    3. I subtract the present month from the most recent month with information to get the distinction. This would be the issue to calculate the inflation price. The result’s saved within the variable MonthDiff
    4. If MonthDiff is smaller than or equal to 0, then the filter context (Month) incorporates a price from the enter variable
    5. If not, the filter context (Month) is sooner or later, and we are able to calculate the outcome.

    What I’m doing right here is to multiply the chosen inflation price by the variety of months because the final month with information (LastMonthWithData).

    Now, I can create a measure to dynamically calculate the forecast month-by-month based mostly on the chosen inflation price:

    On-line Gross sales With Inflation =
        MonthlyInflation([Inflation rate Value], [Sum Online Sales])

    That is the outcome for an inflation price of three%:

    Determine 9 – Results of utilizing the brand new UDF to calculate the income monthly based mostly on the chosen inflation price (Determine by the Creator)

    The blue-marked months comprise precise information, and the red-marked months are calculated based mostly on the chosen inflation price.

    The sweetness is that I can cross any DAX expression to the measure that I would like.

    For instance, I can add On-line Gross sales with Retail Gross sales:

    Determine 10 – End result with the Whole Gross sales when including up On-line and Retail Gross sales when calling the operate.

    The measure for that is the next:

    Whole Gross sales With Inflation =
        MonthlyInflation([Inflation rate Value], [Sum Online Sales] + [Sum Retail Sales])

    Properly, that’s very simple.

    I do know that the calculation could be very simplistic, however I used this instance to showcase what could be executed with UDFs.

    What’s the purpose?

    So, that’s the purpose with UDFs?

    A lot of the issues proven right here can be executed with Calculation teams.

    Properly, that’s true.

    However utilizing a UDF is way simpler than utilizing a Calculation Merchandise.

    Furthermore, we are able to write model-independent UDFs and reuse them throughout a number of fashions.

    Check out Extend Power BI with DAX Lib.

    It is a rising assortment of model-independent UDFs containing logic that can be utilized in any information mannequin.

    Different differentiating factors between UDFs and Calculation Objects are:

    • UDFs can’t be grouped, however Calculation Objects could be grouped in Calculation Teams.
    • Calculation Objects don’t have parameters.
    • UDF could be instantly referred to as like every other DAX operate.

    Attempt it out to study extra concerning the potentialities of UDFs.

    Conclusion

    are an incredible addition to the toolset in Energy BI and Cloth.

    I’m positive it should change into more and more vital to know find out how to work with UDFs, as their potential will change into extra obvious over time.

    As we’re within the early phases of the introduction of this function, we have to keep tuned to see what Microsoft will do subsequent to enhance it.

    There are some restrictions on this function. You discover them right here: Considerations and limitations of DAX user-defined functions.

    There may be sufficient room for enchancment.

    Let’s see what’s coming subsequent.

    References

    Right here, the Microsoft documentation for user-defined capabilities: Using DAX user-defined functions (preview) – Power BI | Microsoft Learn.

    That is the SQL BI article that explains the function in nice element: Introducing user-defined functions in DAX – SQLBI.

    A set of free to make use of model-independent UDF: Extend Power BI with DAX Lib.

    Like in my earlier articles, I take advantage of the Contoso pattern dataset. You possibly can obtain the ContosoRetailDW Dataset at no cost from Microsoft here.

    The Contoso Information can be utilized freely beneath the MIT License, as described in this document. I modified the dataset to shift the info to up to date dates.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow to Apply Powerful AI Audio Models to Real-World Applications
    Next Article Vibe Coda AI-appar i Google AI Studio
    ProfitlyAI
    • Website

    Related Posts

    Artificial Intelligence

    Why the Sophistication of Your Prompt Correlates Almost Perfectly with the Sophistication of the Response, as Research by Anthropic Found

    January 23, 2026
    Artificial Intelligence

    From Transactions to Trends: Predict When a Customer Is About to Stop Buying

    January 23, 2026
    Artificial Intelligence

    Evaluating Multi-Step LLM-Generated Content: Why Customer Journeys Require Structural Metrics

    January 22, 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    What misbehaving AI can cost you

    April 5, 2025

    Agentic AI vs Generative AI: Key Differences for Enterprises

    December 2, 2025

    Strawberry webbläsare med inbyggda AI-assistenter för webbautomatisering

    April 28, 2025

    Mechanistic interpretability: 10 Breakthrough Technologies 2026

    January 12, 2026

    What is AI Image Recognition? How It Works & Examples

    April 4, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    Most Popular

    Empirical Mode Decomposition: The Most Intuitive Way to Decompose Complex Signals and Time Series

    November 22, 2025

    This benchmark used Reddit’s AITA to test how much AI models suck up to us

    May 30, 2025

    How are MIT entrepreneurs using AI? | MIT News

    September 22, 2025
    Our Picks

    Why the Sophistication of Your Prompt Correlates Almost Perfectly with the Sophistication of the Response, as Research by Anthropic Found

    January 23, 2026

    From Transactions to Trends: Predict When a Customer Is About to Stop Buying

    January 23, 2026

    America’s coming war over AI regulation

    January 23, 2026
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 ProfitlyAI All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.