Regression Testing: DAX User Defined Functions

Introduction

User Defined Functions is a new feature in PowerBI currently in public preview. There is a lot of buzz in the air regarding this because it opens new scenarios with modularity and reusability of the code. For more details about DAX UDF’s you can check for example following blogs posts.

As mentioned in second article, one very important aspect of modularity in code is Testing and that’s where CAT comes in. We are evangelists of Power BI testing for a long time and now this topic becomes even more important! 

We are hearing all the time Testing is Difficult and expensive. But guess what, it is not anymore. You can have your PowerBI functionality under the tests in few minutes. 

So how CAT can help you with a testing of your Power BI model and especially new UDF functionality?

UDF are in preview so first you need to enable this preview feature: Power BI -> File -> Options and Settings -> Options -> Preview Features ->”DAX user defined functions” 

Now let’s do a simple new UDF which calculates percentage from Product Category and Productsub-Category perspective. We will use function in % version of the measures. 

Screenshot of DAX code defining a ProductCatPercentageFromTotal function with REMOVEFILTERS applied to product category and subcategory

Percentage measure definition using function: 

Screenshot of a DAX measure calculating internet total sales percentage with a ProductCatPercentageFromTotal function

Here is a simple test table report: 

Screenshot of sales results tables showing internet total sales and sales percentage by product category and subcategory

Ok so I have my report. How do I put this functionality under repeatable tests in 10 minutes?
 

1) Get CAT Studio from app store. 

2) Open CAT Studio and Create Data source – Connection to your open Power BI desktop Report. (Very simple CAT studio will show you list of open PB Reports) 

3) Create Test and Write test queries (I have a test query in my tables in screenshot done already, right?). In our case, I’ll create two tests. 

       a) Usage of the Function by Product Category 

       b) Usage of the Function by Product Subcategory 

4) Run tests 

5) Refactor Function 

6) Run tests 

7) Refactor Function 

8) … 


Hard to imagine?
 

Here is an example of two simple test:

Screenshot of CAT Studio showing DAX regression tests with direct function logic and hardcoded calculation results

You might think this is a silly test. Of course to write a clever and simple tests is not always trivial. CAT can do very good work for regression testing of Power BI but more on that maybe in next blog. 

Now after seeing a relationship between Product Category and Product Subcategory In my model: 

Screenshot of a Power BI data model used for DAX regression testing

I might think maybe I can optimize function and remove filter only from Product Subcategory so I change my function like this: 

Screenshot of DAX code showing a ProductCatPercentageFromTotal function with REMOVEFILTERS highlighted

Question for you: Will this function still work correctly when used together with Product Category?

To test it I can just re-run my CAT tests which compare the results of two queries, and I have my answer in 2 seconds!

Portrait of an author featured in Joyful Craftsmen blog

I have around 20 years of experience with MS data platform. In last couple years I’m passionate mainly about PB/SSAS modeling, DAX and using AI with data. Proud father of three boys who teach me to live 🙂

Roman Lánský
Data Architect