Close Menu
    Facebook LinkedIn YouTube WhatsApp X (Twitter) Pinterest
    Trending
    • CFTC seeks injunction in Kalshi Rhode Island dispute
    • As AI Expands, Erin Brockovich Taps Communities to Map Data Center Concerns
    • Direct-to-Cell Technology: Enabling Satellite Connectivity for Legacy Devices
    • How small businesses can leverage AI
    • Robots-Blog | Humanoide Robotik aus Deutschland: igus bringt neuen Serviceroboter auf den Markt
    • GM reimagines Hummer off-roader with California ideas unit
    • London’s DEScycle secures over €10 million in grant funding to scale critical metals recovery platform
    • How to Edit, Merge, and Split PDFs With Free Online Tools
    Facebook LinkedIn WhatsApp
    Times FeaturedTimes Featured
    Tuesday, June 2
    • Home
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    • More
      • AI
      • Robotics
      • Industries
      • Global
    Times FeaturedTimes Featured
    Home»Artificial Intelligence»A Real-World Example of Using UDF in DAX
    Artificial Intelligence

    A Real-World Example of Using UDF in DAX

    Editor Times FeaturedBy Editor Times FeaturedOctober 27, 2025No Comments9 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp Copy Link


    Introduction

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

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

    On this piece, I’ll display how you can use this function with a real-world instance: calculating a forecast primarily based on inflation charges.

    You will notice how you can create a easy perform and deal with a extra complicated situation.

    Situation

    Let’s think about an organization that desires 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 recognized month-to-month gross sales quantity to calculate the long run revenue for the remainder of the yr.

    The consumer should have the ability to set an inflation price and see how the numbers change.

    Put together the information mannequin

    Now, it depends upon whether or not I begin with a brand new Energy BI file and cargo the information or add this performance to an current 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 Writer)

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

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

    You may both create a dummy perform, which is able to robotically improve the compatibility stage, or use Tabular Editor to set it to not less than 1702:

    Determine 2 – Use Tabular Editor to improve the compatibility stage of the database (Determine by the Writer)

    You may enter 1702 within the marked subject and put it aside.

    I’ll display how you can create a easy UDF later on this piece.

    Please go to the Microsoft documentation to be taught 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 have the ability to choose the inflation price, I add a parameter to the information mannequin:

    Determine 3 – Add a parameter to the information mannequin for a numeric vary (Determine by the Writer)

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

    Determine 4 – Organising the parameter for a proportion vary (Determine by the Writer)

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

    After just a few seconds, the brand new slicer was robotically added to the report web page.

    Nevertheless it’s displaying solely the decimal numbers.
    I need to change the quantity format to see percentages:

    Determine 5 – Format the column of the parameter to a proportion (Determine by the Writer)

    Now the slicer reveals the quantity as wanted:

    Determine 6 – The slicer with the numbers as a proportion (Determine by the Writer)

    Now it’s prepared to make use of.

    Write the primary perform

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

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

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

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

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

    Now I can set a reputation.

    For this primary one, I set “ReturnRate”.

    That is the code for the perform:

    (
        Price : DECIMAL VAL
    )
    
    =>
    Price

    Inside the brackets, I outline the Enter parameter.

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

    On this case, I return the Enter parameter.

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

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

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

    After 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 displaying that the Measure that calls the capabilities returns the chosen inflation price (Determine by the Writer)

    OK, that is an elementary perform, however it’s solely for instance the way it works.

    Write the actual perform

    You might need observed 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 got two modes to go parameters:

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

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

    Right here is the entire code for the perform MonthlyInflation:

    (
        Price : 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 Outcome = IF(MonthDiff<=0
                        ,InputVal
                        ,(1 + ( Price * MonthDiff ) ) * LastValueWithData
                        )
    
                                        
    RETURN
        Outcome
    

    The primary parameter of the perform is as earlier than.

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

    Inside the perform, I can use the parameter identify to vary the filter context and different issues. I need to set the parameter as EXPR after I have to work on this method throughout the perform.

    The perform performs the next steps:

    1. I get the best MonthKey and retailer it within the variable CurrentMonth
      The content material is the month of the present filter context within the numerical kind YYYYMM.
    2. I get the most recent month of the present yr with a worth from the enter parameter (measure) and retailer it into the variable LastMonthWithData
    3. I subtract the present month from the most recent month with knowledge 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 worth from the enter variable
    5. If not, the filter context (Month) is sooner or later, and we are able to calculate the end result.

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

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

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

    That is the end result for an inflation price of three%:

    Determine 9 – Results of utilizing the brand new UDF to calculate the income per thirty days primarily based on the chosen inflation price (Determine by the Writer)

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

    The wonder is that I can go any DAX expression to the measure that I need.

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

    Determine 10 – Outcome with the Complete Gross sales when including up On-line and Retail Gross sales when calling the perform.

    The measure for that is the next:

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

    Effectively, that’s very simple.

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

    What’s the purpose?

    So, that’s the purpose with UDFs?

    Many of the issues proven right here will also be completed with Calculation teams.

    Effectively, that’s true.

    However utilizing a UDF is far 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.

    This can be a rising assortment of model-independent UDFs containing logic that can be utilized in any knowledge 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 straight known as like every other DAX perform.

    Attempt it out to be taught extra concerning the potentialities of UDFs.

    Conclusion

    are an amazing addition to the toolset in Energy BI and Material.

    I’m positive it is going to develop into more and more essential to know how you can work with UDFs, as their potential will develop 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’s 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 group 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 may obtain the ContosoRetailDW Dataset without cost from Microsoft here.

    The Contoso Knowledge can be utilized freely below the MIT License, as described in this document. I modified the dataset to shift the information to modern dates.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Editor Times Featured
    • Website

    Related Posts

    Escaping the Valley of Choice in BI

    June 2, 2026

    Ensuring Data Integrity with Cryptographic Hashing and the Ethereum Blockchain

    June 1, 2026

    RAG Is Not Machine Learning, and the ML Toolkit Solves the Wrong Problem

    June 1, 2026

    How to Combine Claude Code and Codex for Maximum Coding Power

    June 1, 2026

    It’s the Lessons We Learned Along the Way. Or, Is It?

    June 1, 2026

    Proxy-Pointer RAG: Eliminating Wasteful Entity & Relations Extraction in Knowledge Graphs

    May 31, 2026

    Comments are closed.

    Editors Picks

    CFTC seeks injunction in Kalshi Rhode Island dispute

    June 2, 2026

    As AI Expands, Erin Brockovich Taps Communities to Map Data Center Concerns

    June 2, 2026

    Direct-to-Cell Technology: Enabling Satellite Connectivity for Legacy Devices

    June 2, 2026

    How small businesses can leverage AI

    June 2, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    About Us
    About Us

    Welcome to Times Featured, an AI-driven entrepreneurship growth engine that is transforming the future of work, bridging the digital divide and encouraging younger community inclusion in the 4th Industrial Revolution, and nurturing new market leaders.

    Empowering the growth of profiles, leaders, entrepreneurs businesses, and startups on international landscape.

    Asia-Middle East-Europe-North America-Australia-Africa

    Facebook LinkedIn WhatsApp
    Featured Picks

    Kambi becomes Glitnor Group’s new sportsbook provider

    October 10, 2025

    Pebble’s Bringing Its Round Watch Back, This Time With Revamps

    January 2, 2026

    Food firms scramble to meet the high-protein craze

    October 7, 2025
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    Copyright © 2024 Timesfeatured.com IP Limited. All Rights.
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us

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