Hey folks, these are the most asked interview questions and answers for all experienced levels.
Q: What is an Aggregation function in DAX?
Ans: Aggregation functions in DAX compute a single scalar value, such as count, sum, average, minimum, or maximum, for a column or table based on a specified expression. The resulting value is calculated by summarizing or grouping the data based on the defined expression.
Q: How do you create a custom aggregation function in DAX?
Ans: To create a custom aggregation function in DAX, you can use the DEFINE MEASURE function in Power BI or Excel Power Pivot. Here are the steps to create a custom aggregation function:
Open Power BI or Excel Power Pivot and create a new measure by clicking on the “New Measure” button in the “Modeling” tab.
In the formula bar, type in the following code to define a new measure using the DEFINE MEASURE function:
MyCustomFunction =
DEFINE MEASURE(
TableName,
"MyCustomFunction",
)
Replace TableName with the name of the table you want to use, and <DAX expression> with the DAX formula for your custom aggregation function.
In the <DAX expression> section of the code, you can use any DAX functions to define your custom aggregation function. For example, to create a custom function that calculates the weighted average of a column, you can use the following code:
MyWeightedAvg =
DEFINE MEASURE(
TableName,
"MyWeightedAvg",
SUMX(
TableName,
TableName[Value] * TableName[Weight]
) / SUM(TableName[Weight])
)
This code calculates the sum of the products of the Value and Weight columns, and divides it by the sum of the Weight column to get the weighted average.
After you have defined your custom aggregation function, you can use it in your Power BI or Excel Power Pivot report by dragging and dropping it into the Values area of the visualizations.
Q: What is the difference between a calculated table and a calculated column in DAX?
Ans:
In DAX, a calculated table and a calculated column are two different types of calculations that you can create in Power BI or Excel Power Pivot. The main difference between them is the scope of the calculation and how the results are displayed.
A calculated column is a column that you add to an existing table in your data model. The values in the calculated column are calculated based on a DAX formula that you specify. The formula can reference other columns in the same table, as well as columns from other tables in the data model. The calculated column is evaluated row by row, and the calculated values are stored in the column. The results of the calculated column are displayed alongside the other columns in the table, and can be used in other calculations, measures, and visualizations.
A calculated table, on the other hand, is a new table that you create by defining a DAX formula. The formula specifies the structure of the table, including the columns, column names, and data types. The table is created dynamically based on the DAX formula, and the results are based on the context of the calculation. Unlike a calculated column, a calculated table does not store any data, but generates the data as a result of the calculation. The results of the calculated table can be used in other calculations, measures, and visualizations, just like any other table in the data model.
In summary, a calculated column is a new column that you add to an existing table, while a calculated table is a new table that you create based on a DAX formula. The main difference is that a calculated column stores the calculated values in a new column, while a calculated table generates the data as a result of the calculation.
Q: How do you implement time intelligence calculations in DAX?
Ans:
Time intelligence calculations are used in DAX to analyze and compare data over time periods, such as days, weeks, months, quarters, and years. Here are the steps to implement time intelligence calculations in DAX:
Define a date table: A date table is a table that contains a list of dates, and is used as a reference for time intelligence calculations. The date table should have one row for each date in the period that you are analyzing, and should include columns for the date, year, month, day of the week, and other relevant information. In Power BI or Excel Power Pivot, you can create a date table using the “New Table” option in the “Modeling” tab, and then using a DAX formula to generate a list of dates.
Define a measure: A measure is a calculation that you want to perform on the data, such as a sum, average, or count. To perform time intelligence calculations, you need to define a measure that uses the date table as a reference. For example, to calculate the total sales for each month, you can use the following DAX formula:
Total Sales = SUM(Sales[Amount])
Use time intelligence functions: Time intelligence functions are special DAX functions that allow you to perform calculations over time periods, such as months, quarters, and years. These functions require a date column as a parameter, which is typically a column from your date table. For example, to calculate the total sales for the current month, you can use the following DAX formula:
Total Sales This Month =
CALCULATE(
[Total Sales],
DATESYTD(DateTable[Date])
)
This formula uses the DATESYTD function to calculate the period from the beginning of the year to the current date, and then applies the Total Sales measure to that period.
Other commonly used time intelligence functions in DAX include:
- DATEADD: Adds a specified number of units to a date, such as days, months, or years.
- TOTALYTD: Calculates a total for the year-to-date period.
- SAMEPERIODLASTYEAR: Returns a set of dates for the same period in the previous year.
- CUMULATIVE TOTAL: Calculates a running total of a measure over time, such as a running total of sales for each month.
In summary, implementing time intelligence calculations in DAX involves defining a date table, defining a measure, and using time intelligence functions to perform calculations over time periods.
Q: Can you explain the difference between a many-to-one and a one-to-many relationship in DAX?
Ans:
In DAX, a relationship is an association between two tables based on a common column. There are two types of relationships: many-to-one and one-to-many.
A many-to-one relationship occurs when multiple rows from one table match a single row from another table. In this type of relationship, the table on the “many” side of the relationship contains duplicate values in the column that is used to establish the relationship. For example, if you have a table of orders and a table of customers, each order is associated with a single customer, but multiple orders can be associated with the same customer. The relationship between the tables is established based on the common column, which is typically a foreign key column in the “many” table that references the primary key column in the “one” table.
A one-to-many relationship, on the other hand, occurs when a single row from one table matches multiple rows from another table. In this type of relationship, the table on the “many” side of the relationship contains unique values in the column that is used to establish the relationship. In the context of a one-to-many relationship between a table of customers and a table of orders, each customer can have multiple orders, while each order is associated with a single customer. The relationship between the tables is established based on the common column, which is typically a foreign key column in the “one” table that references the primary key column in the “many” table.
The difference between many-to-one and one-to-many relationships in DAX has implications for the way you can perform calculations and create visualizations. For example, if you have a many-to-one relationship between tables, you can use aggregation functions, such as SUM and AVERAGE, to summarize data from the “many” table and display it in a table or chart based on the “one” table. In contrast, if you have a one-to-many relationship between tables, you may need to use functions that iterate over the rows in the “many” table, such as CALCULATE and FILTER, to perform calculations based on the related data.
In summary, a many-to-one relationship occurs when multiple rows from one table match a single row from another table, while a one-to-many relationship occurs when a single row from one table matches multiple rows from another table. The type of relationship you have between tables has implications for the way you can perform calculations and create visualizations in DAX.
Q: How do you create a dynamic slicer in DAX?
Ans:
A dynamic slicer in DAX allows users to interactively filter data based on the selection made in the slicer. The slicer values can be based on a column in a table, or a DAX expression that generates a list of values. Here are the steps to create a dynamic slicer in DAX:
- Create a measure or a calculated column: A dynamic slicer in DAX requires a measure or a calculated column that is used as the basis for the slicer values. The measure or calculated column should return a list of values that can be used as the slicer items.
- Create a slicer visual: In Power BI or Excel Power Pivot, create a slicer visual and select the column or measure that you want to use as the basis for the slicer values.
- Define a DAX expression for the slicer values: In the “Values” field of the slicer visual, enter a DAX expression that generates the list of values you want to use in the slicer. For example, you can use the DISTINCT function to generate a list of unique values from a column in a table:
Distinct Values = DISTINCT('Table'[Column])
Alternatively, you can use a more complex DAX expression that takes into account user-selected parameters or filters to generate the list of values. For example, you can use the CALCULATETABLE function to generate a filtered list of values based on a user-selected date range:
Filtered Values =
CALCULATETABLE(
DISTINCT('Table'[Column]),
'Table'[Date] >= MIN('Date'[Date]) && 'Table'[Date] <= MAX('Date'[Date])
)
- Use the slicer values in DAX calculations: Once the dynamic slicer is created, you can use the selected slicer values in DAX calculations. For example, you can use the SELECTEDVALUE function to retrieve the value selected in the slicer, and use it in a measure or a calculated column:
Selected Value Measure =
CALCULATE(
[Total Sales],
'Table'[Column] = SELECTEDVALUE('Slicer'[Distinct Values])
)
This formula calculates the total sales for the selected value in the dynamic slicer.
In summary, creating a dynamic slicer in DAX involves creating a measure or a calculated column, creating a slicer visual, defining a DAX expression for the slicer values, and using the slicer values in DAX calculations. The dynamic slicer allows users to interactively filter data based on the selection made in the slicer.