When you have really large data models loaded in powerbi.com there are challenges around quickly loading gigs of data into a data model. This is particularly challenging when working with data models in production. When changes are made which require a full load of data tables this can take hours to refresh. This means when a production change needs to be made, this should be done in low use times, evenings or over the weekend. What if we could greatly increase the load speed? but, we can!!
Below is a high level architecture and movement of the Power BI data models across the blob storage account.
The Solution
The high level solution goes something like this:
Create two workspaces, one in prod and one for pre-loading data
Connect both workspaces to premium
Turn both premium capacities to large dataset capacities
Link azure storage to both workspaces
Publish a modified data model into the pre-load workspace, this contains the changes to the data model.
kick off a full load of the data model in the pre-load workspace
Open SSMS connect to the backup the pre-load workspace XMLA endpoint
Backup the pre-loaded model to azure blob storage using SSMS
Open azure bob storage, copy and paste the .abf file into the prod workspace
Use SSMS to connect to the prod workspace XMLA endpoint
Use the restore feature from SSMS to load the .abf file back into the model in production
Instructions
Below are the step by step instructions as show throughout the experience.
When you link your Power BI workspaces you will see inside the Blob storage a folder for powerbi and another folder for power-bi-backup. Note, it is important that both workspaces the pre-loading data workspace and the prod workspace point to the same blob storage account.
For each workspace PowerBI.com will create a single folder for each workspace. See below we have two workspaces for this demo.
We can use the XMLA endpoint found in the workspace settings to connect and see the partitions within our data model. For this example we have a pre-loaded dataset called nyctaxi. This dataset was deployed into a workspace and fully refreshed loading all the data into all the partitions.
Below is a the same dataset, with the exception there are no partitions preloaded. We assume we are going to use the backup created from the above dataset and then load the partitions into the Prod model.
Note: in this example we are pushing all the partitions and all data in the partitions into production. Usually your production model will already have partitions. Thus, it is easier to show our model adding partitions for this demo. If you really want to confirm the data is correctly being restored from backups you would look at the row counts found in each partition.
Here we log into the pre-loaded data set using SSMS.
Right Click on the data model that you are going to backup. Select the option labeled Back Up… in the drop down menu.
After clicking Back Up… a menu will appear letting you to name the backup file.
It is recommended to allow the file to be overwritten and to apply compression to the files. Once your settings have been configured, then you can Click the OK button at the bottom of the window to accept the changes.
After the backup completes we will be able to see the .abf file inside the blob storage folder.
Using the copy and paste buttons form the azure portal you move the files between the workspace folders.
Below is the copied model .abf file from the demo-synapse (pre-loaded data) into the Prod workspace named (Demo – Test Models).
In SSMS we connect to the production workspace. Again Right click on the data model and select Restore… shown in the drop down menu.
The restore menu will appear. In this menu clicking on the Browse button will bring up another window letting the user to select the data model from the storage account.
Note: on this screen you will need to select Allow database overwrite and it is optional to select if you want to Overwrite security
Below is a screen shot of the selection screen for picking the model
Once the restore has completed we can go back to our production model and see we now have partitions loaded.
If you like the content from PowerBI.Tips please follow us on all the social outlets. Stay up to date on all the latest features and free tutorials. Subscribe to our YouTube Channel. Or follow us on the social channels, Twitter and LinkedIn where we will post all the announcements for new tutorials and content.
Introducing our PowerBI.tips SWAG store. Check out all the fun PowerBI.tips clothing and products:
This month we did a webinar with the Microsoft team on how to build different custom visuals in Charts.PowerBI.Tips a version of Charticulator.
If you haven’t seen Charts.PowerBI.Tips this video explains the program, all the functions of each button and building demos. We walk through all the buttons on the tool bar and what they do. Finally we build multiple different charts. Check out this webinar if your curious about building custom visuals with no code.
I want to give a super big thank you to the Microsoft team especially Kelly Kaye for making this video happen.
Thanks for watching and have a Wonderful Day!
If you like the content from PowerBI.Tips please follow us on all the social outlets. Stay up to date on all the latest features and free tutorials. Subscribe to our YouTube Channel. Or follow us on the social channels, Twitter and LinkedIn where we will post all the announcements for new tutorials and content.
Introducing our PowerBI.tips SWAG store. Check out all the fun PowerBI.tips clothing and products:
This article examines using the advanced editor in Power Query to better handle when things go wrong. It will also allow custom actions to be triggered using Power Automate, allowing the rest of the data to be refreshed while alerting the owner by email of data source errors.
Note that this article requires edit Power Query (M) code. If you want to review this, consider reading this article: Query Editor – Editing M Code
Data Sources
Data can be messy. Thankfully, Power Query has given us an
excellent and efficient way to extract, transform and load the data to
manipulate it to the way we want it. It is extremely versatile and can connect
to a huge host of data sources. These can include third party sources as well
as internal data.
While beneficial, we need to ask what happens if the third-party source suddenly shuts down or changes its access policies. What if there is bad data recorded in the on-premise excel sheet or databases? An error in any Power Query step can stop the entire report refreshing. We can opt to receive alerts on the service, but these can be unspecific and require us to dig deep into the report.
The technique laid out here allows to receive a specific alert of the exact step the error occurred. What’s more, we can ensure it won’t break our queries and continue to refresh the rest of the data.
Step 1 – Base Query
First we need to set up the query that we want to error handle. For this example, I’m going to send a web request to get some information about the Microsoft stock price. For more information on this API or to pull other stock data, check out this article.
Open Power Query and Select the New Source > Web on the Home ribbon.
This will automatically return a JSON object and parse it for us.
Note: This link returns 5 years of daily daily historical stock price.
For simplicity, I will just return the meta data to ensure the API call is working. The automatic parsing will return a table with clickable elements. To explore into the JSON we, click through the following steps:
chart:Record > result:list > Record > Meta:Record
Note: See sample of nested structure below for chart:Record
Once we have expanded all the way down to the Meta level, Press the Convert Into Table Button found on the newly appeared Convert ribbon.
Here is the final code, which you can see by Clicking the Advanced Editor button on the Home ribbon revealingthe advanced editor:
let
Source = Json.Document(Web.Contents("https://query1.finance.yahoo.com/v8/finance/chart/MSFT?range=5y&interval=1d")),
chart = Source[chart],
result = chart[result],
result1 = result{0},
#"meta" = result1[meta],
#"Converted to Table" = Record.ToTable(#"meta")
in
#"Converted to Table
Rename this “Stock Query” by editing the name in the properties pane on the right.
Step 2 – Create the flow
Next we create the Power Automate flow that will alert us something is wrong. Navigate to the Power Automate website. Once logged in, Click on the new Instant flow button.
Give the flow an appropriate name. For the trigger, select the option When an HTTP request is received. Next press thebutton Create to make the flow.
Once we save the flow will supply us with a URL. This URL will trigger the flow any time it is visited. You can use any action you want, but I will have the flow send me an email to let me know the refresh failed.
I’ll search for the item Send an email (V2). Make sure you fill in the email address you want to send it to, and write an appropriate message and subject.
That’s our flow! Hit Save. After you have saved, click on the When a HTTP request is received step to expand it. You’ll see that a URL has been generated. Copy the link using the Copy button on the right. You’ll need this to enter it into Power BI.
Step 3 – Make the Flow Trigger Query
Next, set up the query in Power BI to call this flow. In Power Query, make a new query by selecting New Source > Web. Paste in the Web URL you copied in the previous step. Open the advanced editor. Inside you see the code uses the Web.Contents() function. You’ll need to copy this code in a later step.
This will send an email as it runs the query. For testing, if you press the Refresh Preview icon, you can trigger the actions to run again. If you don’t want to wait for the email or chose a different action, you can check if the flow ran by checking it on the power automate site. Click My Flows on the left, open the flow and scroll down to Runs. Press the refresh button on the runs section to check when the flow ran.
Step 4 – Set up the Error Handling Code
Now we need to add the logic to have the Power Automate run
on error, which is going to take a little coding.
Back in Power Query, start by adding a blank query by clicking New Source > Blank Query on the Home Ribbon. Next, open the Advanced Editor and the code should look like this:
Now we will employ the try … otherwise statement. This is Power Query’s error handing statement. We can add a placeholder for now. Replace the step:
Source = ""
with the following code:
Source = try 1+1 otherwise “error”
How this works
Any code between the try and otherwise keywords will be attempted to be executed. If this is successful, the result will be returned and the next step will be evaluated, in this case the number 2. If this returns an error, however, the result will be discarded and the query after the word otherwise will be returned, in this case the word “error” .
We can add this statement as the Source step. I’ll also wrap both statements in parenthesis as this will come in handy as we add more steps. It’s important to keep good syntax to make it readable, so here is my code:
As 1+1 is
valid, this will return the value 2. If you change the 1+1 to 1+“A”, this is not valid so it will return the word error.
Now we can see its working, delete everything between the parenthesis in both the try and otherwise statement to set up for the next step. Do not worry if you get an error as we will be adding code to it.
Step 5 – Update the Error Handling Code
Now we’ve got the placeholder set up we can copy our previous code into each step. Open the Stock Query we made in steps 1 and 2. Click Advanced Editor and copy the entire code. Now, come back to the try..otherwise query. Open the advanced editor in this step and make sure you delete anything between the parenthesis in the try statement if you did not before. Paste the entire code you just copied.
Next, go to the Flow Error Query, open the advanced editor and copy all the text. Go back to the try..otherwise and paste everything between the two parenthesis for the otherwise statement.
Step 6 – Tidying the Code Up
The URLs are prone to change so it is code practice to separate them as a variable. To do this, you can simply state them at the beginning of the query as a step. Here, add the URLs to variables called WebURL and FlowURL. To add WebURL, at the beginning of query after the let keyword, add the line:
Don’t forget the comma at the end. Do the same with the FlowURL. Then, replace the URLs with the variable name.
Additionally, it will help to change the word Source outside the try..otherwise to Output. This makes it easier to follow, as we have several steps called source. Also update it after the keyword in.
Output:
Final code to copy (you will need to update to your correct URLS):
let
flowURL ="https://prod-68.westus.logic.azure.com:443/workflows/ RestofURL",
webURL = "https://query1.finance.yahoo.com/v8/finance/chart/MSFaaT?range=5y&interval=1d",
Output =
try
(
let
Source = Json.Document(Web.Contents(webURL)),
chart = Source[chart],
result = chart[result],
result1 = result{0},
#"meta" = result1[meta],
#"Converted to Table" = Record.ToTable(#"meta")
in
#"Converted to Table"
)
otherwise
(
let
Source = Web.Contents(flowURL)
in
Source
)
in
Output
Optional Step 7.1 – Adding POST Parameter – Flow
Now we have set up our workflow, it may be useful to reuse this on multiple datasets. Instead of setting up a different flow for each dataset, we can allow the flow to accept inputs, such as the dataset name.
Navigate back to Power Automate site and on the flow set up previously, click edit.
Open the step and paste in the following code into Request body JSON.
Next, expand advanced options and change it to POST.
This will create a variable called datasetName. This is the name of the dataset we will pass from Power BI. We can use datasetName in the email to inform us which dataset had an error. The variable will appear in a list when you click to edit the email message – click on the name to add it.
In addition, I added the expression utcNow(). You can find this by searching in the Expression tab of the pop up. This just displays the time it is sent. Thus we can see in the email when it failed. Notice the variable from the first step is green, while the expression is pink. Personalize this message as you wish.
Optional Step 7.2 – Adding POST Parameter – Power BI
The final step is to pass this in from Power BI. In our query, open the advanced editor and add three more parameters before webURL and flowURL:
dataset name is the name of the datset we pass into the flow
and ultimately appear in the email. Right now it’s set to Stock Query, but this
is what we can edit for each dataset we use this query to pass a different name.
content and headers build the correct format to pass into
the API call. This will create a POST request and provide the correct format.
Now we can edit the Web.Contents(webURL) step to include our parameters:
Final code to copy (you will need to update to your correct URLS):
let
datasetName = "Stock Query",
content = " { ""datasetName"": """ & datasetName & """}",
headers = [ #"Content-Type"="application/json"],
flowURL ="https://prod-68.westus.logic.azure.com:443/workflows/RestofURL",
webURL = "https://query1.finance.yahoo.com/v8/finance/chart/MSFaaT?range=5y&interval=1d",
Output =
try
(
let
Source = Json.Document(Web.Contents(webURL)),
chart = Source[chart],
result = chart[result],
result1 = result{0},
#"meta" = result1[meta],
#"Converted to Table" = Record.ToTable(#"meta")
in
#"Converted to Table"
)
otherwise
(
let
Source = Web.Contents(flowURL, [Headers = headers, Content=Text.ToBinary(content)])
in
Source
)
in
Output
Limitations and Considerations
This technique uses premium Power Automate features and a valid license is required. However, only one service account license is needed.
This query has been designed to return blank data if the query fails. This could break your model if this data is required.
This blog does not examine securing the Power Automate endpoint, so be aware if the URL was discovered people could execute the end action (in this case sending an email).
If you like the content from PowerBI.Tips please follow us on all the social outlets to stay up to date on all the latest features and free tutorials. Subscribe to our YouTube Channel. Or follow us on the social channels, Twitter and LinkedIn where we will post all the announcements for new tutorials and content.
Introducing our PowerBI.tips SWAG store. Check out all the fun PowerBI.tips clothing and products:
This post will walk through how to pull an estimated household income from a US address. It will be completed all in the Power Query Editor. We will convert this to a function to reuse on any address we want.
This is the second part in a series on free API calls. The first part demonstrated how to pull daily stock price from Yahoo! Finance, which is available here.
Note: The content in this blog was first presented at the Power Platform Summit North America on October 18th, 2019.
Introduction to the Data
We cannot get exact income of a US address, but the US census releases data which is aggregated at different groups of households (called Geography Hierarchies). There are different levels of Geography Hierarchies, explained on the census website. The lowest level of granularity available for this data is Block Groups. This is a small group of blocks, and usually consists of about a few hundred to over 1000 people. A block group is expressed as a code.
It is unlikely we will have the actual bock group code, but rather a street address. In order to retrieve information, we need to find which block group the address falls into. The first step is to convert the address into geographic coordinate, a process called Geocoding. You are probably familiar with using this feature – for example when you enter an address into a map app in order to get directions.
The census.gov website offer a free service to do this (US Census TIGER). However, in my experience the match rate (percentage of addresses geocoded) is not as good as some other services – this means that some of the addresses you enter will not be found. There are many other companies that offer Geocoding services, such as Bing, who often provide better match rates. However, these tend to come at a cost. These can be used instead, but for this example we will focus on the free US Census TIGER service.
Create an API Key
The first step will be to sign up for an API key from the census website. API keys allow organizations to monitor usage and can stop overloading their databases with requests. Some can be used to charge for paid API calls, however the census API is free and only requires an email address. If you do not work for an organization, you can write “none”.
Back in Power BI, on the home tab in Power Query, click Home, New Source, Blank Query.
In the formula bar, Paste in the API key value you received. Rename the query to P_APIKEY. This will store the API key so it can be changed, but will not set it as a parameter and require it to be entered by the user every time they use the function.
Setting up Parameters
In the power query window, under the Home ribbon, Click the bottom half of the Manage Parameters button. From the drop down Select the option New Parameter.
Name the parameter P_Address. Change the Type field to Text. Enter 15010 NE 36th St in the Current Value input box.
Repeat this step 3 more times, so you will have 4 parameters in total. Use the below table for the names and default values:
Name
Default Value
P_Address
15010 NE 36th St
P_City
Redmond
P_State
WA
P_ZIP
98052
Function Part 1: Geocoding
On the home tab, Click New Source > Web. Switch to the advanced tab, Then in the open dialogue box, first Click the button Add part. This will add a new box. Repeat this to add 9 web parts. Afterwards, locate the first window and Enter part 1 of the URL. In the second box, Change the abc symbol to a parameter. Fill in the boxes like below:
Note: Do not enter the rows that begin with P_ directly (rows 2,4,6,8). Switch the type to parameter and pick from the list.
This will return a result: Record. Click on the Record value to drill into it. In the convert tab, click To Table to transform to a table. We have extra information here, but we are only interested in the blockgroup data. Filter the column Name to include the following rows only: “BLKGRP” , “COUNTY”, “STATE” , “TRACT” (make sure you keep the correct columns with these exact names).
Now we have the correct columns, but for our function to waork we want them in one row. Highlight the Name column, navigate to the transform tabe and click Pivot Column. The symbol is:
Expand the Advanced Options, and change the Aggregate Value Function to Don’t Aggregate.
If you are following with the Microsoft head office, your data should look like this:
Now that we have the address geocoded, we can find out census information.
Function Part 2: Returning Household Income
To add the second API call, we can take advantage of the custom column. It is actually possible to use Power Query (M) code in the custom column. Click Add Column then Custom Column.
The code needed for the column is below. You can copy and paste this directly into the “Custom column formula” :
Breaking this code down: – Json.Document tells Power BI the value being returned is in JSON format, and to decode this to a list – Web.Contents will tell Power BI we are going to be sending a web (api) query – https://api.census.gov/data/2018/pdb/blockgroup?get=Tot_Population_CEN_2010,avg_Agg_HH_INC_ACS_12_16 is our base URL will some parameters. The parameters we’re giving are to return population and average household income -The second half of the URL takes in the location from the current row. anything in [Square Brackets] is a column. P_APIKEY is the parameter we set up earlier, that holds our API key
This will add a new column holding a list of lists. Click on the word list to drill into it
This will bring to the second level, two lists. Afterwards, Transform to a table by clicking on the “To Table” button in the ribbon, under list tools > Transform. After it is in table format, we expand the values to take them out the list. The data has two lists, the first is the column headers and the second is the values, so we need to transform a little to see the data in a nice format. Firstly, expand the values by clicking the expand button and select Extract Values.
This will expand to one column, separated by a character of our choice. I’m going to use the carat symbol (^), as I’m confident this will not be a character already in the data. To do this, change the first box to –Custom– then type in the carat symbol.
After extracting the data from the lists, we can split it into columns. We will split on the delimiter we added, the carat symbol (^). Click on Home Tab, Split Column, By Delimiter.
Same as before, change the first box to –Custom– then type in the carat symbol. Leave the default setting of “At each occurrence of the delimiter”.
Now it is split, promote the first row to headers by going to the Transform tab and use the “Use first row as headers”.
Finalizing the Function
As a result, this returns lots of columns. Highlight the Tot_Population_CEN_2010 and avg_Agg_HH_INC_ACS_12_16 column, right click and select “Remove other columns”. Rename the two columns to “Population” and “Household Income” by double clicking the column header and typing the new name. Finally, highlight both columns, enter the Transform tab and click “Detect Data Type” to convert the data type.
Optional: If you want to add the original address in, Click Add Column then Custom Column. Enter the below code, which will concatenate the address to one value. You can name the column Address.
This query uses a parameter which enables us to can convert it to a function. To do this, right click on the query in the Queries pane on the left and select make function.
Now we have a function where we can input any address and return the estimated household income, as well as population of this average is taken from(n value). To check multiple addresses, you can add your function to any list. This can be found in Add Column ribbon, then Clicking the button Invoke Custom Function. This will return a table for each row. Before expanding, it is important to handle errors, otherwise it could break the query. One option is to Right Click the column header, and select the Replace Errors option, and Type the text null.
Final Function
For those who like M code, here is the final function. You can copy and paste this directly into the advanced editor (See this article on how to do this).
let
Source = (P_Address as any, P_City as any, P_State as any, P_ZIP as text) => let
Source = Json.Document(Web.Contents("https://geocoding.geo.census.gov/geocoder/geographies/address?street=" & P_Address & "&city=" & P_City & "&state=" & P_State & "&zip=" & P_ZIP & "&benchmark=Public_AR_Census2010&vintage=Census2010_Census2010&layers=13&format=json")),
result = Source[result],
addressMatches = result[addressMatches],
addressMatches1 = addressMatches{0},
geographies = addressMatches1[geographies],
#"Census Blocks" = geographies[Census Blocks],
#"Census Blocks1" = #"Census Blocks"{0},
#"Converted to Table" = Record.ToTable(#"Census Blocks1"),
#"Filtered Rows1" = Table.SelectRows(#"Converted to Table", each ([Name] = "BLKGRP" or [Name] = "COUNTY" or [Name] = "STATE" or [Name] = "TRACT")),
#"Filtered Rows" = Table.SelectRows(#"Filtered Rows1", each ([Name] = "BLKGRP" or [Name] = "COUNTY" or [Name] = "STATE" or [Name] = "TRACT")),
#"Pivoted Column" = Table.Pivot(#"Filtered Rows", List.Distinct(#"Filtered Rows"[Name]), "Name", "Value"),
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Custom", each Json.Document(Web.Contents("https://api.census.gov/data/2018/pdb/blockgroup?get=State_name,County_name,Tot_Population_CEN_2010,avg_Agg_HH_INC_ACS_12_16&for=block%20group:" & [BLKGRP] & "&in=state:" & [STATE] & "%20county:" & [COUNTY] & "%20tract:" & [TRACT] & "&key=" & P_APIKEY))),
Custom = #"Added Custom"{0}[Custom],
#"Converted to Table1" = Table.FromList(Custom, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Extracted Values" = Table.TransformColumns(#"Converted to Table1", {"Column1", each Text.Combine(List.Transform(_, Text.From), "^"), type text}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Column1", Splitter.SplitTextByDelimiter("^", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3", "Column1.4", "Column1.5", "Column1.6", "Column1.7", "Column1.8"}),
#"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column1.1", type text}, {"Column1.2", type text}, {"Column1.3", type text}, {"Column1.4", type text}, {"Column1.5", type text}, {"Column1.6", type text}, {"Column1.7", type text}, {"Column1.8", type text}}),
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"State_name", type text}, {"County_name", type text}, {"Tot_Population_CEN_2010", Int64.Type}, {"avg_Agg_HH_INC_ACS_12_16", Currency.Type}, {"state", Int64.Type}, {"county", Int64.Type}, {"tract", Int64.Type}, {"block group", Int64.Type}}),
#"Removed Other Columns" = Table.SelectColumns(#"Changed Type1",{"Tot_Population_CEN_2010", "avg_Agg_HH_INC_ACS_12_16"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Other Columns",{{"Tot_Population_CEN_2010", "Population"}, {"avg_Agg_HH_INC_ACS_12_16", "Houshold Income"}})
in
#"Renamed Columns"
in
Source
If you like the content from PowerBI.Tips please follow us on all the social outlets to stay up to date on all the latest features and free tutorials. Subscribe to our YouTube Channel. Or follow us on the social channels, Twitter and LinkedIn where we will post all the announcements for new tutorials and content.
Introducing our PowerBI.tips SWAG store. Check out all the fun PowerBI.tips clothing and products:
To celebrate joining the Power BI Cat team next week, I thought I would update a DAX game I built last November (2018) which was a DAX-based maze game in Power BI that allowed you to navigate a character through a series of user-created maps in a top-down view.
Like the 2D version, the report imports a CSV file which it uses to generate the map. I pretty much used exactly the same code to import the CSV file and pivot the coordinates to a format for suitable for processing in DAX.
All you need to create/edit your own custom maps is to open and edit a CSV file using your preferred editor and place an X character where you would like a wall to be and an I character for the insight (or exit). Excel is great for this particular task as you can size and align the columns nicely to easily see where walls will be.
The following image shows the data1.csv file opened in MS Excel, with all columns set to the same width of 15-pixels. Once modifications are complete, save the file as CSV (not xlsx) in the folder the PBIX file uses to import from.
Once again, I’d like to thank Margarida Prozil for providing a custom control to manage the navigation. This is an updated version of the D-PAD called a 3D-Pad. You can grab a copy from her GitHub repo if you are keen to work with this control.
The control has four arrows. The Up/Down arrows move you back and forward through the map, while the left/right arrows spin you 45 degrees. This is different behaviour to the D-Pad in the 2D version of the maze.
I’d also like to thank Mike Carlo at PowerBI.Tips for providing the wonderful background image and the overall visual design of the game. It was Mikes idea for the evil laugh on the intro page. This started to drive me a bit nutty as it would play everytime I saved the file.
The Game Logic
Like the 2D game, this version uses SVG as the method to draw the graphics to the screen. Each time an arrow is clicked, a new view needs to be generated based on the interaction, so a calculated measure takes into account the new position or perspective and generates a fresh SVG to be displayed using the Image control by CloudScope.
The SVG image displayed in the Image Control is a nested set of mini SVG polygons that draw the outline of various shapes into an outer SVG file. The performance is pretty good and I have still yet to hit any limitations on text size to store the final SVG set.
The 3D world
The first challenge was to figure out how to convert coordinates in a table in the data model into an image that looks like you are walking through a maze.
The map I use in the uploaded PBIX file is 26 steps and 36 steps long, so it isn’t big. If a step has been designated as a wall, it effectively has 4 walls (panels) around its perimeter, and when a series of X values in the CSV run next to each other, a longer wall effect is created.
Each of the panels for a step in the board has 5 points. I initially created these panels as 100 x 100 walls, but I found it added the extra point at the top to help me work out which way was up when I was first playing with the 3D to 2D projection code.
The Calculated table called 3D Worlds takes the basic X/Y coordinates from the CSV file and generates a 3D world of X/Y/Z points for every panel on every all in the maze.
The image below shows all X/Y/Z coordinates for 1 step in the CSV map. The first column shows there are four panels. Think of these as like north, east, south and a west facing wall. Each wall as five points. These five points define each corner of the wall in terms of a 3D world and the X, Y and Z columns for each row specify exactly where each point should be in a three-dimensional plane.
3D to 2D projection
The next challenge is to take the 3D coordinates and convert this information into an image that represents what you should see based on your location in the map, along with the direction you are looking. There was no way I was going to figure the maths for this out from scratch, and after a few internet searches and a bit of reading, I found this article to have all the information I needed.
The key information is in the section on Perspective Projection, including the algorithm I ported into the DAX measure in the game.
The basic principle is to define a 2D plane (or screen) called a viewport that sits a specific distance from the eyeball. Then an imaginary line is calculated between the eyeball and every X/Y/Z point from the 3D World table. If any of these lines pass through the viewport, they can be plotted onto the viewport at a specific 2D x/Y coordinate.
The code I used from the Wikipedia article is the following:
The Main Map calculated measure contains all the DAX code to convert the 3D data points to 2D coordinates using SVG.
The section of code in the Map Map calculation that matches first the algorithm from the Wikipedia article is here :
I’m not going to pretend to understand this any more than it reminds me of math classes from when I was 15, working out SIN, COSINE equations. I swore then I would never use them for real, so I guess I owe my old maths teacher an apology.
The good news is I didn’t need to understand the code in the end. Once I’d added it to the DAX calculated measure and started generating SVG polygons, it’s quickly looked the way I wanted.
I probably spent less than 2 hours getting this aspect of the game to work. There was a little fine-tuning while I worked out what the various elements were, but this was much easier than I anticipated.
A nested set of CONCATENATEX functions in the Main Map calculated column, loop through every object defined in the 3D World table and converts points to 2D versions. A series of filters are applied to stop plotting any graphics that fall outside the 2D viewport, such as walls that are behind you (taking into account the direction you are looking).
The objects are drawn from furthest to nearest to make sure far away objects do not appear if they are covered by a nearer object. This also means portions of further away objects will appear as expected if they can be partially seen.
Other filters are applied to stop panels of walls being plotted when they simply cannot be seen.
Mini Map
A separate Mini Map calculated measure generates a non-3D version of the map and gets added to a separate Image Viewer custom control to show your position in the map. A small red triangle shows your position and direction and a small white square shows the exit. In this case, it’s inside the letter E.
3D Dpad
Margarida Prozil supplied me with an updated 3D control that sits on the top of three separate columns in the database. X and Y (rows and columns) control the square you currently occupy, while the third column is V (for view perspective). X and Y represent how many squares wide, or long the map is. V represents the angle in steps of 45 degrees and there are 8 possible values (0, 45, 90, 135, 180, 225, 270 and 315). The example map is 26 squares wide, 36 squares long so combined with the 8 views, mean the control can be set to 7,488 possible values. A custom control can only manage about 30,000 points, so take this into account for larger custom maps.
Summary
I’m pretty happy with the 3D effect and can see it can be used with other X/Y/Z based 3D coordinates plotted to a 2D view plane. When I was debugging this, I had slicers for height on the screen so I could use a slider to create an effect of flying up in the air – and the maze still rendered as expected. Power BI seems to cope with the workload pretty well, despite not really optimised for this kind of work. In future versions, I may add more objects and detail to push the engine harder and see where breaking points are.
There probably aren’t too many business use-cases in Power BI for this type of report aside from educational. Feel free to have a look through the PBIX file where you may pick up some useful ideas, tips and tricks in the DAX.
There is a small bug at the start of the game when the bookmark drops you into the maze and you need to click the up arrow a few times to get going. Once you are on your way, it’s pretty good. I’ll try to get that resolved in the next few days.
Update: This tool has been deprecated as of 2024-11-27. You can now find this as a downloadable HTML file at the following Github page.
In April of 2018 the Microsoft team released the ability to edit the Linguistic schema in Power BI desktop. For those who are not aware of the linguistics, essentially, this is the code that drives how Power BI can interpret your data model when you use Q & A. The linguistic schema is defining how the computer is able to figure out the best visual relating to your question. In Power BI desktop you can double click on the white space of a report page and then the Q & A prompt appears. Then type a statement into the Q & A box, this in turn generates a visual.
In both the Desktop program and in the PowerBI.com service, Q & A is an impressive feature. By default the Power BI desktop creates a linguistics schema about the data model. However, there are some details that the linguistics schema can’t detect. This is where you come in. In the Power BI Desktop you can download the Linguistics file, make any number of changes or additions to the file and then re-upload the file back to Power BI Desktop. But, there is a slight catch. The downloaded files can be quite large and a little difficult to navigate. PowerBI.Tips to the rescue.
Lingo is a web app that allows you to upload your linguistics schema into an easy to use editor. It includes search, code validation, and code blocks that you can use to make writing code easier. Check out the video below to see how it works:
For the full details on the linguistics schema visit the following article from Microsoft. A sample Power BI file, Linguistics model, and Linguistics Spec can be downloaded here. Well that about wraps it up, thanks for reading and happy coding.
If you like what you learned about today and want to stay updated, please follow me on Twitter, Linkedin, and YouTube for the latest updates.
This week I encountered an issue when working with multiple queries in my data model. Here is the source files in case you want to follow along.
Here’s what happened. I had a PBIX file that had four queries in it, one file for the summer the Olympic metal count for the following years, 2000, 2004, 2008, and 2012.
After a bit of working I figured that my desktop screen was going to get to cluttered if I continued to collect Olympic metal data. Thus, I moved my excel files which were my source data into a folder called Olympic Medals.
By doing this I broke all the links for all four files. This was discovered when I tried to refresh my queries and noticed that all the queries failed. Power BI gave me a nice little message notifying me that there was a data source error.
DataSource.Error: Could not fine the file:
To fix this I had to open the query editor and change each file’s location to the new folder that I just made. Seeing that this is not an efficient use of my time, I decided to spend more time to figure out a way to make a variable that would be my file location for all my queries.
Lets begin by making a new blank query by clicking on the bottom half of the New Source button on the Home ribbon. Then click the item labeled Blank Query.
With the new query open type in the file location where you will obtain all your working files. For me my file location was on my desktop, thus the file location is listed below. Rename the new query to Folder.
Note: Since we are working on building a file structure for Power BI to load the excel files you will want to be extra careful to add a “\” back slash at the end of the file location.
Next on the query for Medals 2000, we click the Source under the applied steps window on the right. This will expose the code in the formula bar at the top of the window.
Note: If you don’t see the formula bar as I have illustrated in the image above, you can turn this feature on by click the View ribbon and checking the box next to the words Formula Bar. This will expose the formula bar so you can edit the source step.
This is where the magic happens. We can now insert our new blank query into this step. Our current file contents looks like the following:
Not only does this shorten our equation, it now uses the folder location we identified earlier and then we can pick up the file name 2000 Medals.xlsx. This makes is very easy to add additional queries with the same steps. Also, if you move your files to a new folder location, you only have to change the Folder query to reflect the new file location. To test this make a new folder on your desktop called New Folder. Move all the Olympic medal files to the new folder. Now in Power BI Desktop press the Refresh on the Home ribbon. This should result in the Data.Source.Error that we saw earlier. To fix this click the Edit Queries on the Home ribbon, select the Folder query and change the file directory to the new folder that you made on your desktop. It should look similar to the following:
Once you’ve modified the Folder query, click Close & Apply on the Home ribbon and all your queries will now reload. Success!!
Hope this tutorial helps and solves some of the problems when moving data files and storing information for Power BI desktop. Please Share if you like the tutorials. Thanks.
This tutorial will produce a measure that will dynamically calculate a percent change every time an item is selected in a visual. The previous tutorial can be found here. In the previous tutorial we calculated the percent change between two time periods, 2014 and 2013. In practice it is not always desirable to force your measure to only look at two time periods. Rather it would be nice that your measure calculations change with changes in your selections on visuals. Thus, for this tutorial we will add some dynamic intelligence to the measures. Below is an example of what we will be building:
First here is the data we will be using. This data is the same data source as used in the previous % change tutorial. To make things easy I’ll give you the M code used to generate this query. Name this query Auto Production.
Note: the code shown above should be added as a blank query into the query editor. Add the code using the Advanced Editor. Another tutorial showing you how to add advanced editor code is here.
Once you’ve loaded the query called Auto Production. The Field list should look like the following:
Next add a Table with Production and Year. this will allow us to see the data we are working with. When you initially make the table the Year and Production columns are automatically summed, thus why there is one number under year and production.
Rather we want to see every year and the production values for each of those years. To change this view click on the triangle in the Values section of the Visualizations pane. This will reveal a list, in this list it shows that our numbers are aggregated by Sum change this to Don’t Summarize.
Now we have a nice list of yearly production levels with a total production at the bottom of our table.
Next we will build our measure using DAX to calculate the percent changes by year. Our Calculation for % change is the following:
% Change = ( New Value / Old Value ) - 1
Below is the DAX statement we use as our measure. Copy the below statement into a new measure.
I color coded the DAX expression between the two equations to show which parts correlated. Note we are using the DIVIDE function for division. This is important because if we run into a case where we have a denominator = 0 then an error is returned. Using DIVIDE allows us to return a zero instead of an error.
Next add our newly created measure as a Card.
Change the % Change measure format from General to Percentage, do this on the Modeling ribbon under Formatting.
Next add a slicer for Year.
Now you can select different year and the % change will automatically change based on our selection. The % change will always select the smallest year’s production and the largest year’s production to calculate the % Change. By Selecting the Year 2013 and 2007, the percent change is 19.15%. The smallest year is 2007 and the largest is 2013.
If we select a year between 2013 and 2007 the measure will not change.
The measure will only change when the starting and ending years are changed. By selecting the year 2014, the measure finally changes.
Pretty cool wouldn’t you say? Thanks for taking the time to walk through another tutorial with me.
Want to learn more about PowerBI and Using DAX. Check out this great book from Rob Collie talking the power of DAX. The book covers topics applicable for both PowerBI and Power Pivot inside excel. I’ve personally read it and Rob has a great way of interjecting some fun humor while teaching you the essentials of DAX.
In this tutorial we’ll learn how to copy and paste queries to and from the Query Editor. When your working in Power BI Desktop often you will need to share and model the data before it can be applied to the visual. In my experience you’ll need to add a calculated column or break out a date such as 1/5/2016 into the the Year 2016 or Month 01, components to properly display a visual.
We will start off with from a prior example where we build a shaded region map. The tutorial to create this Power BI Desktop file is located here.
If you want to cheat and download the final PBIX file you can download and open the zipped file here: Regional Filled Map Example
This file was made in Power BI Desktop April 2016 version, 2.34.4372.322, download the latest version from Microsoft Here.
Open the zip file that you downloaded and extract the file inside labeled Regional Filled Map Example. Open the file. Once you’ve opened the file on page 1 of the you see a map of the united states that looks similar to the following.
Now we well enter the query editor. Click on the Edit Queries on the Home ribbon. You opened the Query Editor. In this window we shape and model the data so we can properly visualize it on the pages. Couple of things to notice here. Every time you press a button on the ribbon, the query editor generates an Applied Step. Each step writes a line of M code which transforms the data as it is loaded into the computer’s memory. In this case we have (7) seven steps starting at Source and ending with Changed Type1.
We want to expose the code that is begin generated at every step behind the scenes. Click on the View ribbon and then click on the button called Advanced Editor.
Opening this window reveals the M language code that is generating each Applied Step we saw earlier.
Note: some of the steps we saw earlier such as Filtered Rows had a space in it. In the query editor any applied step had a space in the name gets the added #”” around the applied step name. Thus, in the query editor Filter Rows would be #”Filtered Rows”. The hashtag and the quotations define the complete variable. If you changed the name of the applied step to FilteredRows, with no space. In the Advanced Editor you’d only see the step labeled as FilterRows, no hastag or quotations needed.
Now that the M language is revealed you can made modifications to the code. In cases where you want to make a function you would do so in the Advanced Editor. For our example today select all the code and copy it to the clipboard using the keyboard shortcut CTRL+C. Click Done to close the window.
Now lets copy that code into a brand new query. Click the Home ribbon, then click New Source, scroll all the way to the bottom of the list and select Blank Query. Click Connect to start a blank query.
A new Query will now open up. Click the View ribbon, then click Advanced Editor. A blank editor window will open.
Paste the code we copied earlier into this window. Now the new Query1 should look like the following:
Click Done and the new query will now load. It is that simple, now we have two identical queries.