testing
2 TopicsAutomated Unit Testing
Hi Forum, Inspired by this article, I'd like to create automated unit tests to test measures within my Power BI Semantic Model (Dataset). I'm planning to: - Use Powershell to connect to and run through the tests - DAX scripts to query the Semantic Model itself Since these are unit tests, I want to contain "fake" data within the test itself (not have to worry about the underlying database changing). A few approaches I've attempted. 1. Overwrite Existing Table for Test (Doesn't work; PBI won't let me overwrite the actual_table in the semantic model) DEFINE TABLE actual_table = UNION(ROW("order","A1"),ROW("order","B1")) -- attempt to update as a "local variable" MEASURE 'Measures Table'[COUNT_ORDERS] = DISTINCTCOUNT('actual_table'[order])) EVALUATE ROW("COUNT_ORDERS",'Measures Table'[COUNT_ORDERS]) Error: Table '<ccon>actual_table</ccon>' cannot be created because a table or variable with the same name already exists. 2. Update Table Reference (Pretty sure PBI was designed purposely not to do this) DEFINE TABLE test_table = UNION(ROW("order","A1"),ROW("order","B1")) MEASURE 'Measures Table'[COUNT_ORDERS] = DISTINCTCOUNT('actual_table'[order])) -- somehow update table reference 'actual_table' to 'test_table' EVALUATE ROW("COUNT_ORDERS",'Measures Table'[COUNT_ORDERS]) 3. SWITCH between actual table and test table (I don't like this approach because I'd have to duplicate the SWITCH across all measures) DEFINE TABLE test_table = UNION(ROW("order","A1"),ROW("order","B1")) MEASURE 'Measures Table'[COUNT_ORDERS] = SWITCH(2, 1,DISTINCTCOUNT('actual_table'[order]), 2,DISTINCTCOUNT('test_table'[order]) ) EVALUATE ROW("COUNT_ORDERS",'Measures Table'[COUNT_ORDERS]) Do you have any alternative recommendations? Your help is greatly appreciated.2.2KViews0likes1CommentSharing: How to create test data using DAX!
Hi, In this article, I will show how to generate sample data and how to add this to your data model. The result is a sales table and related dimensions: This is the code to generate the sales table: Sales = VAR _tbl = SELECTCOLUMNS ( CROSSJOIN ( ROW ( "test", DATE ( 2020, 10, 3 ) ), GENERATESERIES ( 1, 60, 1 ) ), "StartDate", [test], "Increment", [Value] ) VAR _dates = ADDCOLUMNS ( _tbl, "NewDate", [StartDate] + [Increment] ) RETURN GENERATE ( SELECTCOLUMNS ( { "Vendor 1", "Vendor 2", "Vendor 3", "Vendor 4", "Vendor 5" }, "Vendor", [Value] ), SELECTCOLUMNS ( ADDCOLUMNS ( CROSSJOIN ( _dates, GENERATESERIES ( 1, 10, 1 ) ), "open_time", [NewDate] + NORM.INV ( RAND (), 0.5 + ( [Increment] / 100 ), 0.08 ), "PRODUCT", CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING ) & CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING ) & CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING ), "sales_amount", NORM.INV ( RAND (), [Value], [Value] / 10 ) ), "sales_date_time", [open_time], "PRODUCT", [PRODUCT], "sales_amount", [sales_amount] ) ) You can add the above code by navigating to the ribbon in Power BI, select 'Modelling' to then select 'New Table'. Paste the above code in the editor. Explanation In the next part, I will break down the working of the code. First, we generate a series of dates: We started with a default date and added an incremental column, that column sets the number of days that we will add to the default data. Next is adding vendors: SELECTCOLUMNS ( { "Vendor 1", "Vendor 2", "Vendor 3", "Vendor 4", "Vendor 5" }, "Vendor", [Value] For each vendor, all the dates (generated above) are added and a timestamp is added: SELECTCOLUMNS ( ADDCOLUMNS ( CROSSJOIN ( _dates, GENERATESERIES ( 1, 10, 1 ) ), "open_time", [NewDate] + NORM.INV ( RAND (), 0.5 + ( [Increment] / 100 ), 0.08 ), A product is added by randomly selected characters from the UNICHAR array: "PRODUCT", CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING ) & CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING ) & CONVERT ( UNICHAR ( RANDBETWEEN ( 65, 90 ) ), STRING ), A sales amount is added: "sales_amount", NORM.INV ( RAND (), [Value], [Value] / 10 ) Finally, the required columns are added: "sales_date_time", [open_time], "PRODUCT", [PRODUCT], "sales_amount", [sales_amount] ) With the above steps, we have created the fact table. With that table in place we can add the dimension tables by simply adding a table with DAX like below: dimProducts = VALUES(Sales[PRODUCT]) As a final step, we add the relationships in the data model. That is hopefully on familiar grounds. You can have fun with generating data and creating art from it as well 🙂 , see below: The above image is from a response that I wrote when helping out somebody with a DAX challenge. Hope that you find it useful. An example is attached. Kind regards, Steve.2.5KViews1like0Comments