sh-notion/notion_data_team_no_files/PBI Switch table from Import to DirectQuery 1210446ff9c98027b620f441f083a588.md

38 lines
3 KiB
Markdown
Raw Normal View History

2025-07-11 16:15:17 +02:00
# PBI: Switch table from Import to DirectQuery
## Situation
Generally, we like to have all queries pointing at the DWH from PBI reports in `DirectQuery` mode. This is preferable for a number of reasons, which I wont cover here to keep things brief.
It might happen that sometimes, for some reason, a report ends up being shipped to production with some query to DWH being set in `Import` mode. Most common one is just not noticing (been there, done that, no worries).
## Issue
This used to be a massive hassle because:
- Once the query is created, PBI wont allow you to change from one mode to the other.
- This means you can only delete the existing query/table and recreate it.
- Doing this means that everything thats built on top of of this query (measures, visuals, etc) will go to hell and youll have to rebuild it manually. On any report thats not trivial, this is time consuming and extremely error prone. A clear no-no.
## Solution
The solution is to do some good old hacking. You can change the query mode by modifying the source files of the PBI report. This is not supported by PBI and is clearly experimental, so be careful and triple-check once youve done it to make sure nothing blew up.
Steps to achieve:
1. Lets imagine we are working on report `MySweetReport` which contains the query `MyAwfulQuery`. The query is currently in `Import` mode and we want so switch it to `DirectQuery`.
2. You are either working on a git branch already. If not, definitely create one.
3. Make sure your report is NOT open in PBI Desktop.
4. On your branch, make sure to commit or revert all pending changes. Basically, you want to do this from a stable report situation, so you can rollback decently if shit hits the fan.
5. Once your git state is blank, look for the report youre working with in the repo, and open the file `MySweetReport.dataset/model.bim`.
6. This monster JSON has a gazillion things, so let me explain a bit the structure before we pull out our surgery knife:
1. The root element contains a key called `tables` . `tables` contains an array, with one entry for every query you have in your PBI report. Each entry has a `name`, which should match the name you can see in PBI Desktop.
2. Each `table` entry will have another key inside called `partitions`, which contains an array. Ive always found this array to only have one element. Im not sure under which circumstances it might contain multiple elements. If thats what you see, I cant help you, youre on your own, good luck 🫡.
3. The only entry within `partitions` will have an entry named `mode`. This can either be `import` or `directQuery`.
7. To switch from `import` to `directQuery`... Just replace `import` with `directQuery`.
8. Make a commit just with this change so you can easily isolate it in git.
9. After this, review the report and triple-check everything is working as expected.
Done!
If you encounter different situations, things dont work as described here, you find out more hacky tricks around this… Feel free to enrich this page.