Mostrando las entradas con la etiqueta Measures. Mostrar todas las entradas
Mostrando las entradas con la etiqueta Measures. Mostrar todas las entradas

domingo, 8 de enero de 2017

HOW TO DEFINE A MEASURE TABLE IN POWER BI DESKTOP

In this post I show you a simple trick to make your Power BI model more organised and more readable. I call it creating a “Measure Table”. Let me explain. The story is that I was working on a model with lots of tables. The database schema was NOT a proper star schema so there were a bunch of measures spread into lots of different tables. On top of that we’ve created lots of calculated measures with different home tables which made it really hard to find a particular measure or calculated measure. I thought, well, when it is that hard to find the calculated measures at development time how hard it could be for a customer to find, understand and use the measures we created. The visibility of the calculated measures could be an issue when we have lots of measures in lots of different tables. You will soon feel the issue in customer training sessions when you need to navigate between lots of different tables to find a calculated measure.
Consider you create a Power BI model with direct connect to a SSAS Multidimensional instance. You will immediately notice that all measure groups have a special calculation icon (Measure Group Icon in Power BI) rather than a normal table icon (Table icon in Power BI) which makes the measure groups more recognisable for the end users. For instance, you can easily find any calculated measure related to “Internet Sales” under the “Internet Sales” measure group.
Measure Groups in SSAS Multidimensional Dirct Connect
I know, we can search and find the measures very easily, but, our model would be more organised and more user friendly if we can put all measures in one or more tables which contain just related calculated measures and nothing else. For instance, we can create a measure table for time intelligence calculations and name it “Sales Time Intelligence Measures” and put all  calculated measures like “Sales YTD”, “Sales LYTD”, “Sales Period Over Period” on it. It will make your model nice and clean, easy to use and easy to learn for your customers. It will also help you to train your customers more easily.
In this article I’ll connect to a SQL Server instance and will use the famous Adventure Works database. I also show you how to get the job done in both “Import” and “DirectQuery” modes as there are some limitations applied to the DirectQuery mode which makes it harder to do what we want.
Lets start.

How It Works

The idea is adding a new table with just one column having just one value in that column. Then we need to hide that column. This makes the whole table get hidden as there is no columns to show. Then we use this table as “Home Table” for all related calculated measures. Remember, we do NOT hide the table, but, just the column. In this case Power BI Desktop recognises the table as a measure table so it uses the measure group icon (Power BI Measure Group Icon) for it.

Create a Measure Table in Import Mode

  • Open Power BI Desktop
  • Get data
  • Connect to a SQL Server Database (Adventure Works as a sample)
Power BI get data from SQL Server 01
  • Select a desired table (FactInternetSales in our case)
  • Click “Select Related Tables” button
  • Click “Load”
Power BI get data from SQL Server 02
  • Click “Import” then OK
Power BI Get data from SQL Server Import mode
  • Click “Enter Data” from Home tab
Power BI Enter Data
  • Enter a value for “Column1”
  • Enter a name for the table then click “Load”
Power BI Create Table
  • Expand the new table from the “Fields” pane then hide the “Column1” column
Power BI hide columns
  • This will hide the whole table as there is no visible columns to show
  • Click “New Measure” from “Modeling” tab from the ribbon to create a new calculated measure
Power BI new measure
  • Type a desired DAX expression to make the new calculated measure then press enter
Power BI new measure DAX
  • This will create the calculated measure in the first table in the “Fields” pane
Power BI new measure 02
  • Click on formula bar again to enable the measure properties
  • Click “Home Table” and pick the newly created table from the list
Power BI new measure change home table
  • As you can see the table appears on the “Fields” pane again with the calculated measure
Power BI Task pane
  • As you can see the tables still has the normal table icon, but, if you toggle right the fields pane then toggle left it the icon will be refreshed
Power BI Toggle Task pane
You can create more tables like “Resellers Sales” and so on then move the calculated measures to the corresponding tables. In some cases you can hide the whole fact tables if you have created all calculated measures you need or when your customer won’t need to see any measures from the fact tables.
Power BI Table of Measures

Create a Measure Table in DirectQuery Mode

Basically we need to do the same process, but, this time we want to connect to a SL Server database in DirectQuery mode. So to get data we do exactly the same things:
Open Power BI Desktop=> Get Data=> Connect to a SQL Server database and so on.
It is important that you enter the database name at the first step of getting data.
Power BI get data from SQL Server 02
But, you’ll immediately notice that the “Enter Data” button is disabled in DirectQuery mode.
Power BI Enter Data disabled in DirectQuery
It’s OK. There is always an alternative way to achieve the goal.
As I mentioned before, we need to enter the database name when getting data. This is important as we’ll reuse the connection in the next steps and if we haven’t enter the database name you’ll get the following message:
“Connecting to tables from more than one database is not supported in DirectQuery mode”
Power BI connectin to different datanases is disables in DirectQuery
OK, here is the trick.
After you connected to the SQL Server database in DirectQuery mode follow the steps below:
  • Click “Edit Queries” from the ribbon
Power BI Edit Query
  • Click “Recent Sources” then click the most recent one
Power BI reuse existing source
  • Select a table. It doesn’t matter which table you select as we’ll modify it during the next steps. Click OK
Power BI Navigator
  • This will add the new table in the “Queries” pane (in my sample it is “AdventureWorksDWBuildVersion”)
Power BI Query Editor
  • Click “Advanced Editor” from “Home” tab from the ribbon
Power BI Advanced Editor
Power BI Advanced Editor 02
  • All you need to do is to add a simple query like “[Query=”SELECT 1 AS NEW_COLUMN”]” to the source
Power BI Advanced Editor 03
  • Now you have to comment out/remove the next line as well as the output in the “in”
  • Put “Source” in the “in” block
  • Do not forget to remove the comma “,” from the end of the “Source” line
  • Click “Done”
Power BI Advanced Editor 04
  • You should see a table with one column
Power BI new table in Query Editor
  • Rename the query then click “Close & Apply”
Power BI new table in Query Editor 02
OK, now we have a table with just one column. From here the rest of the process is just like what we’ve done before.
Power BI new table in Fields pane
  • Hide the “New_Column” column
  • Add a new measure then change the “Home Table” to newly created table
  • Toggle right and left the task pane and you’re done
Power BI Table of Measures 02
You might think
“Well… this was a bit long procedure. What if I want to create more than one measure table? Do I need to redo the whole process again and again?”
The answer is NO, you do NOT have to redo the process for each measure table you want to add to the model. All you need to do is to create a reference query from the query you already created.
To do so:
  • Click “Edit Query” from the ribbon to open “Query Editor”
Power BI Edit Queries 02
  • Right click on the query you created previously and click “Reference”
Power BI reference table
  • Rename the reference table then “Close & Apply”
Power BI reference table 02
  • From here you need to follow the same process as explained before
Power BI Table of Measures 03
  • All done!

Fixing Measure Madness - DAX


Measure Tables
Measure Tables

Often times when you’re working with large data models you will have multiple tables with many relationships.  It could be complex maybe you’ve seen something like the following:

Large Data Model
Large Data Model – Photo Credit ( www.biinsight.com )

Once all the tables have been loaded the manic measure building begins to support all the different visuals.  A couple of sums here, a number of calculates over there, and boom, a beautiful report.  You stand back and survey the work and realize you’ve built measures all over the place, in different tables, maybe even stuck a couple of measures in the wrong place.  Whoops.

Tons of Measures
Tons of Measures

Maybe we should think about cleaning things up a bit, if only there was a way to group the measures.  How do I group my measures?  I’m glad you asked.  With a little trickery we can make a measure table.  Let’s begin.
First we will load a little data.  For this tutorial we will simply copy and paste in some data.
Note: For the full tutorial on manually entering in data visit this page.
On the Home ribbon click the Enter Data button.  Copy in the table below into the Create Table window.  Rename the table Sales Data and click Load to exit.
SalesmanItemUnit SalesRevenue
Salesman 3Item 44051357
Salesman 1Item 33391649
Salesman 1Item 33151332
Salesman 3Item 34181531
Salesman 1Item 34821633
Salesman 2Item 44481676
Salesman 1Item 43911432
Salesman 2Item 13411539
Salesman 3Item 14191482
Salesman 2Item 44141610
Salesman 1Item 43511670
Salesman 3Item 34491795

Manually Enter Data
Manually Enter Data

Upon loading our data table we now have the following fields.

Fields of Data
Fields of Data

Now, let’s make a measure that calculates the revenue per unit.  On the Home ribbon click the New Measure button and enter the following DAX measure.
Revenue Per Unit = SUM('Sales Data'[Revenue]) / SUM('Sales Data'[Unit Sales])
Next, make a table with the following fields.

Salesman Table
Salesman Table

Great! but, as we all know this is how the measure madness begins.  From here we refine and finesse the data to craft the data story, and end up with tons of additional tables and measures.
Pro Tip: You can use the search window at the top of the Fields window to help you find buried measures or fields of data. 

Using Search in Fields
Using Search in Fields

Let’s make the measure table.  Start by clicking Enter Data on the Home ribbon.  Rename the new table to My Calcs, and rename Column1 to Calcs.   You don’t have to re-name column1, but since I’m OCD about my data I like to rename the column to the same name as the table.  Then click Load to exit the screen.

Measure Table Load
Measure Table Load.

We now have a new table labeled My Calcs with one column labeled Calcs.  Next highlight the measure we created Revenue Per Unit.  Then on the Modeling ribbon change the home table from Sales Data to My Calcs.  This will move the measure.

Home Table for Measure
Home Table for Measure

Right click on the Calcs column in the My Calcs table and then select Hide.

Hide Calcs Column
Hide Calcs Column

Next Save and then reopen the document (it’s a Microsoft thing I guess).  After the document has reopened the My Calcs table has changed it’s icon from a table to a Measure icon.

Completed Measures Table
Completed Measures Table

For kicks and giggles add the following measure to the My Calcs table.
Total Revenue = SUM('Sales Data'[Revenue])
Ok, one more.
Total Unit Sales = SUM('Sales Data'[Unit Sales])
There you go.  A very straight forward approach to cleaning up all the random measures in your data model.  I have found that when others team members are working with your data model this helps other people understand which fields have been calculate and which ones were imported via a query.  This also helps you group logical calculations, further creating clarity within your data model.
If you want to read up more on making measure tables check out this great site (also linked below).  In addition to walking you through creating a measure table it also explains how to make a measure table when using direct query mode.  As the article explains, while you are in direct query mode you are unable to manually enter data.  Nice job, Soheil Bakhshi, well done.