To work directly with the Model View in Power BI (often referred to as Diagram View) through the Query View (or DAX Query Editor), where you define the actual structure of your tables, measures, and relationships programmatically, you can leverage DAX to define and modify the model itself. While the core idea of manipulating tables and creating measures programmatically was explained in the previous steps, the distinction here lies in how Query View connects your queries to the actual Power BI Data Model.
In Power BI, there are specific limitations when using DAX within the Query View, as this mode is often intended for querying and less so for model manipulation. However, you can still control much of the model (like table creation and measures) using DAX and Power Query M in conjunction.
Expanding on Model Creation: Using DAX to Create Tables within the Power BI Model
In the Model View, you can create tables that remain a part of your data model. Let’s take this a step further using Power Query and DAX.
1. Creating Tables Directly in Power BI Model (Model View)
You can create new tables that are part of the Power BI data model using DAX. These tables are not static outputs but rather dynamic objects that Power BI can use in any visual or report. Here’s an example of how to do this:
Example: Creating a Calculated Table in Power BI
-- A calculated table that summarizes sales by product
ProductSalesSummary =
SUMMARIZE(
SalesTable,
SalesTable[ProductName],
"Total Sales", SUM(SalesTable[SalesAmount]),
"Average Sales", AVERAGE(SalesTable[SalesAmount])
)
- SUMMARIZE aggregates data and creates a table on the fly.
- This table, ProductSalesSummary, is added to the Model View and can be used like any other table in Power BI, including for relationships and further calculations.
2. Modifying the Power BI Model with Relationships
Once tables are created, you’ll want to establish relationships between them. In the Model View, these relationships are critical for building complex, interconnected datasets. Here’s how to do it programmatically.
Example: Defining Relationships in DAX
-- Creating a relationship between two tables
EVALUATE
USERELATIONSHIP(
SalesTable[ProductID],
ProductTable[ProductID]
)
In this case, USERELATIONSHIP allows you to define the relationship directly between your tables, ensuring the correct connections are made in your model.
3. Calculated Columns vs. Measures in the Data Model
In Power BI, it’s important to distinguish between calculated columns and measures:
- Calculated Columns are added to your tables and calculated row by row.
- Measures are aggregations or calculations that are evaluated based on the context of the report.
Let’s expand on both types of fields you can create:
Example: Calculated Column
-- Create a calculated column to calculate profit margin
ProfitMargin =
(SalesTable[SalesAmount] - SalesTable[Cost]) / SalesTable[SalesAmount]
This column will appear in your Model View and can be used for further calculations and visualizations.
Example: Measure
-- Create a dynamic measure for profit margin that reacts to the filter context
TotalProfitMargin =
DIVIDE(
SUM(SalesTable[SalesAmount] - SalesTable[Cost]),
SUM(SalesTable[SalesAmount])
)
This measure will dynamically calculate the total profit margin based on the context of the report’s filters.
4. Visualizing the Data
After setting up your data model (including relationships, calculated tables, and measures), you can use the Data Model to build visuals. Although visuals are typically created using the drag-and-drop interface in Power BI, understanding how these visuals interact with your DAX code can give you more confidence in controlling the output.
For example, when using a SUMMARIZE table (as shown above) in a bar chart, the visuals are automatically updated based on the underlying DAX code.
Key Considerations for Working with the Model in Power BI
- Performance Implications: Be mindful that DAX calculations (especially calculated columns) can affect performance since these are calculated during data refreshes. Measures are generally more efficient because they are evaluated at runtime.
- Testing in Query View: Although the Model View handles the actual structure of your data, you can test your DAX code in the Query View to verify that your logic is correct before committing it to the model.
- Calculated Tables vs. Imported Data: Use calculated tables sparingly. While they’re useful for generating on-the-fly data, they can be less efficient than importing clean, pre-aggregated data from your source.
Power BI Workflow Summary:
To give you a clear step-by-step approach, here’s a more detailed summary workflow, designed for coding within the Query View while updating the Model View in Power BI:
| Step | Description |
|---|---|
| 1. Data Source Import | Use Power Query or native connectors to import raw data into the Power BI model. |
| 2. Table Creation | Use DAX (DATATABLE, ADDCOLUMNS, etc.) to create tables that appear in your model. |
| 3. Create Relationships | Define relationships between tables using USERELATIONSHIP in DAX or through the Model View UI. |
| 4. Add Calculated Columns | Enrich tables by creating calculated columns in the Data Model using DAX expressions. |
| 5. Create Measures | Write dynamic calculations (measures) that are aggregated and calculated based on the context. |
| 6. Create Visuals | Use the data from the model in Power BI visuals such as charts, graphs, and KPIs. |
| 7. Test and Debug | Use DAX Studio or the built-in Query View to test and refine your queries and calculations. |
This workflow focuses on treating Power BI as a more code-driven environment, similar to SQL Server Management Studio or Visual Studio, allowing for the most control over your data model without relying solely on the UI.
By understanding how DAX can be used to manipulate the Model View, you’ll be able to manage your data as efficiently as writing code in a traditional programming environment.

Leave a comment