{"id":1282,"date":"2026-10-07T09:00:00","date_gmt":"2026-10-07T08:00:00","guid":{"rendered":"https:\/\/knitteddata.com\/?p=1282"},"modified":"2026-10-07T08:15:46","modified_gmt":"2026-10-07T07:15:46","slug":"make-it-a-pattern-user-defined-functions-in-power-bi","status":"publish","type":"post","link":"https:\/\/knitteddata.com\/index.php\/2026\/10\/07\/make-it-a-pattern-user-defined-functions-in-power-bi\/","title":{"rendered":"Make It a Pattern: User-defined Functions in Power BI"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">With user-defined functions, I believe we Power BI developers can finally call ourselves real programmers without hesitation. We have code (TMDL), we have functions, variables, and much, much more.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>What are those UDFs?<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Just the coolest feature in Power BI!<br>You know those measures you create to make your calculations cleaner, and you use them only to calculate other measures. You never put them in a visual. They are just there to calculate other stuff.<br>Now you can define functions with your DAX and use them in all your measures. And it gets better: they can take parameters. That means you can use the same function for different use cases.The best part is you get control over parameter evaluation: scalar now, or evaluated later in filter context.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why you might want them:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>They are a new way to make your semantic models cleaner and easier to maintain.<\/li>\n\n\n\n<li>They can improve performance for repeated logic in complex models.<\/li>\n\n\n\n<li>You can reuse them in different semantic models and projects.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>How to define UDFs and where they live<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">There are many ways to define UDFs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Option 1: Model view<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can do it from Model view. Go to:<br>Data -&gt; Model -&gt; Functions<br>Then click the (\u2026) next to Functions and select New function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is also where the functions live and where you can find all of your functions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MyVeryFirstFunction = (parameter1, parameter2) =&gt; parameter1 + parameter2<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1080\" style=\"aspect-ratio: 1920 \/ 1080;\" width=\"1920\" controls src=\"https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Video-Project-22.mp4\"><\/video><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Option 2: DAX Query view<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From the DAX Query view you can run a query like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEFINE FUNCTION MyVeryFirstFunction =\n&nbsp;(parameter1, parameter2) =&gt;&nbsp;\n&nbsp;parameter1 + parameter2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then update the model with changes. Else the Function will only exist in the current query. Use EVALUATE to test your function.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1080\" style=\"aspect-ratio: 1920 \/ 1080;\" width=\"1920\" controls src=\"https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Video-Project-23.mp4\"><\/video><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Existing functions also come with a handy quick queries menu under \u201c&#8230;\u201d -&gt; \u201cQuick queries\u201d, which makes it easier to work with them directly in the model. But only if you are in the DAX Query View.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1009\" height=\"793\" src=\"https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-114720.png\" alt=\"\" class=\"wp-image-1285\" srcset=\"https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-114720.png 1009w, https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-114720-300x236.png 300w, https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-114720-768x604.png 768w\" sizes=\"auto, (max-width: 1009px) 100vw, 1009px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Option 3: TMDL view<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Or from TMDL view, by running a script like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>createOrReplace\n&nbsp;&nbsp;&nbsp;Function `MyVeryFirstFunction` = ```\n&nbsp;&nbsp;&nbsp;(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameter1,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameter2\n&nbsp;&nbsp;&nbsp;)\n&nbsp;&nbsp;&nbsp;=&gt; parameter1 + parameter2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When working with TMDL View scripts, pay close attention to tabs and indentation, as small formatting inconsistencies can quickly lead to errors.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1080\" style=\"aspect-ratio: 1920 \/ 1080;\" width=\"1920\" controls src=\"https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Video-Project-24.mp4\"><\/video><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similar to DAX Query View, in the TMDL View there are also additional actions in the functions menu.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"757\" height=\"795\" src=\"https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-121010.png\" alt=\"\" class=\"wp-image-1287\" srcset=\"https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-121010.png 757w, https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-121010-286x300.png 286w\" sizes=\"auto, (max-width: 757px) 100vw, 757px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>What is a UDF made of?<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>FunctionName =\n&nbsp;&nbsp;&nbsp;\/\/\/ Function description\n&nbsp;&nbsp;&nbsp;(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\/ Parameter description\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Parameter : Type SubType ParameterMode\n&nbsp;&nbsp;&nbsp;)\n&nbsp;&nbsp;&nbsp;=&gt; FunctionBody<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here comes a description of every part, including what is required.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>FunctionName<\/strong><br>The name you give to the function.<br>Required.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Function description<\/strong><br>A comment that explains what the function does.<br>Optional, but highly recommended once you have more than a few functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Parameter list<\/strong><br>Everything inside the parentheses.<br>Optional. A function can have zero parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you define parameters, the parameter name is required for each parameter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Parameter<\/strong><br>The parameter name you reference inside the function body.<br>Required only if you define parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Type<\/strong><br>An optional hint that describes what kind of argument the parameter accepts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    AnyVal &#8211; Accepts either a scalar value or a table expression.<br>    Scalar &#8211; Accepts a single value, like a number, text, date, boolean, or a scalar expression returning one value.<br><strong>   <\/strong>Table &#8211; Accepts a table expression.<br>   AnyRef &#8211; Accepts a reference type (a generic \u201creference\u201d category).<br>   CalendarRef &#8211; Accepts a calendar reference.<br>   ColumnRef &#8211; Accepts a column reference.<br>   MeasureRef &#8211; Accepts a measure reference.<br>   TableRef &#8211; Accepts a table reference.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>SubType<\/strong><br>An optional hint that applies only when Type is Scalar.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    Variant<br>    Int64<br>    Decimal<br>    Double<br>    String<br>    DateTime<br>    Boolean<br>    Numeric<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you specify a SubType, your input will be cast to that subtype when the function is called. That gives you more predictable behavior than relying on implicit conversions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a simple example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEFINE\n&nbsp;&nbsp;&nbsp;FUNCTION AddInt64 =\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x : SCALAR INT64,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y : SCALAR INT64\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x + y\n\nEVALUATE\n{\n&nbsp;&nbsp;&nbsp;AddInt64 ( 3.5, 4.5 )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Because the parameters are declared as Int64, the inputs are cast to integers before the addition happens. In this example that means 3.5 becomes 4 and 4.5 becomes 5, so the result is 9. With the type DECIMAL it would result in 8.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ParameterMode<\/strong><br>An optional hint that controls when an argument is evaluated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">VAL<br>The argument is evaluated immediately at the call site, and the function receives the resulting value. This is useful when you want to freeze the result before the function applies its own filter context.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">EXPR<br>The argument is passed as an expression and evaluated inside the function body. This is useful when you want the function\u2019s internal filter context to affect how the argument is calculated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>FunctionBody<\/strong><br>The DAX expression after the arrow that returns the result.<br>Required.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Going deeper: questions I had when I first heard about UDFs<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Ok, we covered the basics of UDFs. Let\u2019s dig deeper. There are some questions I was interested in as I first heard of UDFs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>How to create a library for my organisation?<\/strong><br>For now there is no tool or feature for that. The best way is to create a Git repo with TMDL snippets, so they can be copied into TMDL View or DAX Query View in Power BI.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Can I create and change UDFs in the TMDL file?<\/strong><br>Yes. My code-loving heart made a happy little flip.<br>The functions in the Power BI Project are easy to find and they are all in one place. You can copy them there directly.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"505\" src=\"https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-121251-1024x505.png\" alt=\"\" class=\"wp-image-1288\" srcset=\"https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-121251-1024x505.png 1024w, https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-121251-300x148.png 300w, https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-121251-768x379.png 768w, https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-121251-1536x757.png 1536w, https:\/\/knitteddata.com\/wp-content\/uploads\/2026\/04\/Screenshot-2026-04-16-121251.png 1953w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Can I save them in a template?<\/strong><br>Again yes. You can create a template with your useful UDFs and have it on hand every time you create a semantic model from that template.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Can I use them when I am a report builder and cannot edit the semantic model?<\/strong><br>Sadly, when I connected to a semantic model with a brand new report I couldn\u2019t see any functions in the data source model. I also couldn\u2019t use a function when I was creating a measure in a report.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope it will change, because I see great potential in self service scenarios for that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So that\u2019s it for now. If you try UDFs, I would genuinely love to hear how you use them. What patterns do you build first, and what do you wish Power BI handled better?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>With user-defined functions, I believe we Power BI developers can finally call ourselves real programmers&hellip;<\/p>\n","protected":false},"author":2,"featured_media":1290,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[34,41,43],"class_list":["post-1282","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-power-bi","tag-data-modelling","tag-power-bi","tag-udfs"],"_links":{"self":[{"href":"https:\/\/knitteddata.com\/index.php\/wp-json\/wp\/v2\/posts\/1282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/knitteddata.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/knitteddata.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/knitteddata.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/knitteddata.com\/index.php\/wp-json\/wp\/v2\/comments?post=1282"}],"version-history":[{"count":2,"href":"https:\/\/knitteddata.com\/index.php\/wp-json\/wp\/v2\/posts\/1282\/revisions"}],"predecessor-version":[{"id":1310,"href":"https:\/\/knitteddata.com\/index.php\/wp-json\/wp\/v2\/posts\/1282\/revisions\/1310"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/knitteddata.com\/index.php\/wp-json\/wp\/v2\/media\/1290"}],"wp:attachment":[{"href":"https:\/\/knitteddata.com\/index.php\/wp-json\/wp\/v2\/media?parent=1282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/knitteddata.com\/index.php\/wp-json\/wp\/v2\/categories?post=1282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/knitteddata.com\/index.php\/wp-json\/wp\/v2\/tags?post=1282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}