Model - Create measures using DAX Flashcards
What is DAX?
Data Analysis Expressions (DAX) is a formula expression language used in Analysis Services, Power BI, and Power Pivot in Excel.
What do DAX formulas include?
DAX formulas include functions, operators, and values to perform advanced calculations and queries on data in related tables and columns in tabular data models.
Why is DAX important?
Feature engineering: DAX allows you to augment the data that you bring in from different data sources by creating a calculated column that didn’t originally exist in the data source.
What is a calculated column?
Performed in Power BI’s data model
A calculated column is a column that you add to an existing table (in the model designer) and then create a DAX formula that defines the column’s values.
When a calculated column contains a valid DAX formula, values are calculated for each row as soon as the formula is entered.
Values are then stored in the in-memory data model.
How to create a calculated column?
(…) on a field and then New Column
Enter the formula (i.e., value on left side of = sign is the column name the information on the right hand side is the DAX expression)
What are the implications of calculated columns?
Calculated columns are materialized in the .pbix Power BI file extension, meaning that each time you add a calculated column, you are increasing the size of the overall file.
Having too many calculated columns will slow performance and will cause you to reach the maximum Power BI file size sooner.
What is best practice around calculated columns?
Use SQL: Using SQL language is an efficient way of creating a column because it would make the data source do the calculations for you. In Power BI, the calculated column would appear like any other column.
When you create a calculated column by using DAX, you do not need to refresh the dataset to see the new column. In the other methods, you would need a refresh to see changes. This process can be lengthy if you are working with a lot of data. However, this issue is irrelevant because, after columns have been created, they are rarely changed.
Does not Compress well: The DAX calculated column does not compress as well as the other methods. The other column types do get compressed, which makes the .pbix file smaller and the performance is usually faster.
Create Columns Early: Generally, the earlier you can create a column, the better.
Find alternatives first: It is not considered an optimal practice to use DAX for calculations if you can use a different mechanism.
What is a custom column?
Performed in Power Query
This allows you to create calculated columns using your existing data. You can give it a name and then build a formula by double clicking on fields in the field list.
What are the different ways to create a custom column? What Languages do they use?
Create the column in the source query when you get the data (n.b., Each data source would have a different technique for completing this action) = database language
Create a custom column in Power Query = M language
Create a calculated column by using DAX in Power BI desktop.
What is a measure?
Measures are dynamic calculation formulas where the results change depending on context.
Measures are used in reporting that support combining and filtering model data by using multiple attributes such as a Power BI report or Excel PivotTable or PivotChart.
Measures are created by using the DAX formula bar in the model designer.
When to use a measure?
You can build a measure without writing DAX code; Power BI will write it for you when you create a quick measure.
Aggregation operates over the entire dataset
Want to slice and dice the data by other criteria
How to create a quick measure?
(…) and then select quick measure
***Power BI creates the DAX measure for you and displays the DAX. This approach can be a helpful way to learn the DAX syntax.
How to create a measure?
Fields list, click the three-dot ellipsis on the selected field and select New measure.
Text will now appear in the formula bar underneath the ribbon.
Difference between quick measure and measure?
A quick measure does not require the formula bar and transforms an existing column whereas a measure uses the formula bar and creates a new column/field
What is the difference between a calculated column and a measure?
Calculated column
The fundamental difference between a calculated column and a measure is that a calculated column creates a value for each row in a table.
For example, if the table has 1,000 rows, it will have 1,000 values in the calculated column.
Calculated column values are stored in the Power BI .pbix file.
Each calculated column will increase the space that is used in that file and potentially increase the refresh time.
Calculated columns use disk space and RAM
Measure
Measures are calculated based on the filters that are used by the report user. These filters combine to create the filter context.
Measures are used for Aggregation.
This is because measures only use CPU
Changes with the filter context (e.g., tables, fields and value changes when using filters)
Every calculated measure must contain an aggregation function such as AVG or SUM
What is meant by context in Power BI?
The unique set of tables, fields and values which determine DAX measure results
How does context affect DAX measures?
With Power BI, even though the measure was only defined once, it can be used in these visuals in different ways.
It is the context of how the DAX measure is used that calculates these totals accurately.
Interactions between visuals will also change how the DAX measure is calculated.
What factors affect DAX calculations?
Many other factors affect how DAX formulas are evaluated. Slicers, page filters, and more can affect how a DAX formula is calculated and displayed.
What is the calculate function?
The CALCULATE function is your method of creating a DAX measure that will override certain portions of the context that are being used to express the correct result.
How should you name calculate functions?
When you use the CALCULATE function to override the context, it is helpful to name the measure in a way that describes exactly how you are overriding it.
What is USERELATIONSHIP?
This function is used to specify a relationship to be used in a specific calculation and is done without overriding any existing relationships.
Why is USERELATIONSHIP important?
It is a beneficial feature in that it allows developers to make additional calculations on inactive relationships by overriding the default active relationship between two tables in a DAX expression, as shown in the following example:
Sales by Ship Date = CALCULATE(Sum(Sales[TotalPrice]), USERELATIONSHIP(Sales[ShipDate],’Calendar’[Date]))
Now, you will be able to create the second visual.
What is a semi-additive measure?
Semi additive measures, i.e. measures that have to be aggregated differently over different dimensions, are commonly used in BI solutions. One example could be stock levels. Of course we don’t want to sum them up over time, but only over product, location etc. For the time, a different aggregation is used, for example average or last value.