Power BI testing has never been simpler
Why should you care
Bugs can be everywhere. It is great you tested the data in the previous stages, but if you omit testing of the “final destination”, you can still ship a solution with a big potential for problems. Do you hear the questions back in your head? Hesitations like “Did the data load correctly?”, “Is the new / changed relationship a problem for already existing measures?”, “When I fixed that one measure, didn’t I destroy a few others depending on that one?”, …
There are two options. Either you hope all is OK, or you have an evidence. The latter creates trust into your solution, to you and to your data audience.
Preparations
There are some preparations, but nothing big. Our previous blog describes what you need to do. TLDR: you need to change simple setting in a Power BI admin portal, create a service principal and give him access to your workspace.
Start simple
Start with automating one test only. Something simple, but something you care about. The magic of running the test with one command and immediately see a potential problem is addictive.
Prepare your CAT project
PowerShell
Install-Module CAT -AcceptLicense -Force -AllowClobber -Scope CurrentUser
PowerShell
Import-Module CAT
cd ~/Documents # or any other folder of your choice
mkdir AutomatedTestsForPowerBI # it is best practice to have each project in a separate folder
cd AutomatedTestsForPowerBI
New-CatProject AutomatedTestsForPowerBI
YAML
Data Sources:
– Name: My Power BI Dataset
Provider: Dax@1
ConnectionString: >
Provider=MSOLAP;
Data Source=powerbi://api.powerbi.com/v1.0/your-organization.com/your-workspace-name;
Initial Catalog=Your-Dataset-Name;
User ID=%YOUR_SERVICE_PRINCIPAL_NAME_ENVIRONMENT_VARIABLE_NAME%;
Password=%YOUR_SERVICE_PRINCIPAL_SECRET_ENVIRONMENT_VARIABLE_NAME%;
Up to now, it was boring, right? But once you have all this set (and it really does not take much time), you are all set and can start writing tests.
Add your first test and
run it
YAML
Tests:
– Name: Table dim_version contains data
Description: If the table is empty, the load process needs to be checked.
Suite: smoke tests
Data Source: My Power BI Dataset
Query: |
EVALUATE ‘dim_version’
Expectation: set is not empty
PowerShell
Invoke-CatProject ‘AutomatedTestsForPowerBI.cat.yaml’
Adding more tests
YAML
– Name: dim_version contains “Unknown” member
Description: |
Some metrics depend on unknown member. If it was removed or renamed,
it can create problems
Data source: My Power BI Dataset
Query: |
EVALUATE
FILTER ( ‘dim_version’, [VersionOfCat] = “Unknown” && [VersionOfCatID] = -1 )
Expectation: set rowcount
Expected RowCount: 1
DAX
— when dots are removed, the value is numeric (except for “Unknown”)
EVALUATE
VAR versions =
(
SELECTCOLUMNS (
FILTER ( ‘dim_version’, [VersionOfCat] <> “Unknown” ),
“VersionOfCat”, [VersionOfCat],
“IsError”, ISERROR ( VALUE ( SUBSTITUTE ( [VersionOfCat], “.”, “” ) ) )
)
)
RETURN
FILTER ( versions, [IsError] = TRUE () )
— this checks major versions, only zero is expected so far
EVALUATE
VAR majorVersions =
SELECTCOLUMNS (
FILTER ( ‘dim_version’, [VersionOfCat] <> “Unknown” ),
“VersionOfCat”, [VersionOfCat],
“MajorVersion”, LEFT ( [VersionOfCat], SEARCH ( “.”, [VersionOfCat] ) – 1 )
)
RETURN
FILTER ( majorVersions, [MajorVersion] <> “0” )
What next?
These were just first steps. In fact, CAT can do lots of other things, like:
- easily generate the tests from metadata (so that you don’t write the above simple test for each table separately)
- store results of tests in MS Excel, JSON, relational databases and other
- compare data between different systems (MS SQL server with Power BI, MS Excel with Postgres, …)
- …