0% found this document useful (0 votes)
19 views8 pages

Power BI

Uploaded by

Bhavaneswari B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

Power BI

Uploaded by

Bhavaneswari B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Power BI DAX

1. What is DAX, and why is it important in Power BI?

Answer:
DAX (Data Analysis Expressions) is a formula language for building custom
calculations in Power BI. It enables advanced analytics by creating measures,
calculated columns, and calculated tables.

Example:

Calculate total revenue:

Total Revenue = SUM(Sales[Revenue])

2. What is the difference between calculated columns and measures? Provide


examples.

Answer:

• Calculated Column: Row-level operations, stored in the model.


• Measure: Calculated dynamically during queries, context-sensitive.

Examples:

• Calculated Column:

Profit = Sales[Revenue] - Sales[Cost]

Measure:

Total Profit = SUM(Sales[Profit])

3. How do you calculate a running total in DAX?

Answer:
A running total (cumulative sum) can be calculated using either TOTALYTD for
date-based calculations or a custom approach for flexibility.

Examples:

1. Using TOTALYTD:
Power BI DAX
Running Total (YTD) = TOTALYTD(SUM(Sales[Amount]), Dates[Date])

2.Custom Running Total:

Running Total =

CALCULATE(

SUM(Sales[Amount]),

FILTER(ALL(Dates[Date]), Dates[Date] <= MAX(Dates[Date]))

4. How do you calculate Year-over-Year (YoY) growth in DAX?

Explanation:

Year-over-Year (YoY) growth measures the percentage change in a metric


compared to the same period in the previous year. You typically use the
SAMEPERIODLASTYEAR function to shift the date context by one year and then
calculate the difference between the current period and the previous year.

Example:

You can calculate YoY growth in sales using the following DAX expression:

YoY Growth =

DIVIDE(

SUM(Sales[Amount]) - CALCULATE(SUM(Sales[Amount]),
SAMEPERIODLASTYEAR(Dates[Date])),

CALCULATE(SUM(Sales[Amount]), SAMEPERIODLASTYEAR(Dates[Date]))

Explanation:
Power BI DAX
• SUM(Sales[Amount]): Calculates the total sales in the current period.
• CALCULATE(SUM(Sales[Amount]), SAMEPERIODLASTYEAR(Dates[Date])):
Calculates the total sales in the same period last year by shifting the date
context using SAMEPERIODLASTYEAR.
• DIVIDE: Divides the difference in sales between this year and last year by
the sales of the previous year to calculate the percentage growth.

This formula gives you the YoY growth percentage for sales.

5. How does the CALCULATE function work in DAX?

Answer:
CALCULATE modifies the filter context for a calculation.

Example:

Sales for East = CALCULATE(SUM(Sales[Amount]), Region[Name] = "East")

6. What is RELATEDTABLE in DAX?

Explanation:

The RELATEDTABLE function is used to retrieve a related table's rows in the


current context. It is useful when you need to perform calculations that
depend on related data in another table. This function works in a many-to-one
relationship, where the current table is the "one" side, and the related table is
the "many" side.

Example:

Suppose you have two tables: Orders and OrderDetails. You want to calculate
the total quantity of products sold for each order in the Orders table.

Total Quantity = SUMX(RELATEDTABLE(OrderDetails),


OrderDetails[Quantity])

Explanation:

• RELATEDTABLE(OrderDetails): This fetches all rows from the


OrderDetails table that are related to the current row in the Orders
table.
Power BI DAX
• SUMX: Iterates over these rows and sums the Quantity field in the
OrderDetails table.

This function is helpful when you need to aggregate or perform calculations on


data from a related table.

Certainly! Here's the detailed explanation for the requested DAX functions,
including examples and use cases:

7. What is the USERELATIONSHIP function, and when is it used?

Explanation:

USERELATIONSHIP is used to activate an inactive relationship in a data model.


In Power BI, you can define multiple relationships between tables, but only one
can be active at a time. USERELATIONSHIP allows you to temporarily activate
an inactive relationship for a specific calculation, which is useful when you
need to use an alternate relationship (for example, a different date column) in
your measure.

Example:

Suppose you have a Sales table with two date columns: OrderDate and
ShipDate. You have two relationships between the Sales table and the Dates
table—one for OrderDate and one for ShipDate, but only one can be active.
You can use USERELATIONSHIP to activate the ShipDate relationship for a
specific calculation.

Total Sales by Ship Date =

CALCULATE(SUM(Sales[Amount]), USERELATIONSHIP(Sales[ShipDate],
Dates[Date]))

Explanation:

• USERELATIONSHIP(Sales[ShipDate], Dates[Date]): Temporarily activates


the relationship between Sales[ShipDate] and Dates[Date] for this
calculation.
• CALCULATE(SUM(Sales[Amount])): Sums the sales amount, but now the
calculation uses the ShipDate relationship instead of the default
OrderDate.
Power BI DAX
This function is useful when working with models that have multiple
relationships between tables and you need to specify which one to use in a
particular measure.

8. What is SUMMARIZETABLE, and how does it differ from SUMMARIZE?

SUMMARIZETABLE returns a physical table that can be directly used in


reports, visualizations, or further calculations. It's useful when you want to
create a summarized table to be referenced later.

SUMMARIZE returns a table expression that is temporary and often used


within other DAX functions (like CALCULATE or FILTER). It's not typically used
as a standalone table but rather as part of a larger formula.

Example Output:

For both functions, if you summarize Sales by Product and calculate total sales
(SUM(Sales[Amount])), both will create a summary table like:

Product Total Sales

A 5000

B 3000

But:

• SUMMARIZETABLE will give you a physical table that you can use in
visualizations.
• SUMMARIZE will give you a table expression that’s used within another
DAX expression, not directly usable in visuals.

Thank you for pointing that out! Here’s the complete and revised list,
including Year-over-Year (YoY) Growth and Running Total calculations along
with their explanations, examples, and corresponding interview questions.
Power BI DAX
9. What is CONCATENATEX in DAX, and how is it used?

Answer:
CONCATENATEX concatenates values from a column or expression into a single
string with a delimiter.

Example:

Product Names = CONCATENATEX(Sales, Product[Name], ", ")

10. What is the difference between SUM and SUMX?

Answer:

• SUM: Directly sums a column.


• SUMX: Evaluates an expression row-by-row and sums the results.

Examples:

• SUM:

Total Sales = SUM(Sales[Amount])

SUMX:

Total Sales = SUMX(Sales, Sales[Quantity] * Sales[Price])

11. How do you rank items in DAX?

Explanation:

To rank items in DAX, the RANKX function is used. This function ranks a set of
values (usually measures or columns) based on a specific sorting order (e.g.,
descending or ascending). The function is particularly useful for creating
rankings such as "Top N" or "Bottom N" reports.

Example:

Suppose you want to rank products based on their sales amounts:

Product Rank = RANKX(ALL(Product[Name]), SUM(Sales[Amount]), , DESC,


DENSE)
Power BI DAX
Explanation:

• RANKX: The function used to calculate the rank.


• ALL(Product[Name]): Removes any filters on the Product[Name] column
to rank across all products.
• SUM(Sales[Amount]): The expression used for ranking (total sales for
each product).
• DESC: The sorting order (descending, so higher sales get a lower rank
number).
• DENSE: The ranking type (dense ranking, meaning if two products tie,
the next rank will continue without skipping numbers).

This formula ranks products based on their total sales in descending order.

12. How do VALUES and DISTINCT differ in DAX?

Answer:

• VALUES: Returns unique values, including blanks.


• DISTINCT: Returns unique values, excluding blanks.

Example:

Unique Products = COUNTROWS(VALUES(Product[Name]))

Distinct Products = COUNTROWS(DISTINCT(Product[Name]))

13. How would you calculate the percentage of total in DAX?

Answer:

% of Total Sales = DIVIDE(SUM(Sales[Amount]),


CALCULATE(SUM(Sales[Amount]), ALL(Sales)))

14. How do you optimize DAX calculations?

Answer:

• Use variables (VAR) for repeated calculations.


Power BI DAX
• Avoid heavy operations inside iterators.

Example:

Optimized Measure =

VAR TotalRevenue = SUM(Sales[Revenue])

RETURN TotalRevenue / SUM(Sales[Quantity])

15. How would you filter data dynamically in DAX?

Answer:

Using ALLSELECTED to Retain Slicer-Based Filters

• ALLSELECTED is used when you want to retain the filter context set by
slicers or other visual elements while ignoring other context filters (like
row filters or column filters).
• It ensures that the values selected by slicers are preserved while
removing other filters that may have been applied to the data.

Example:

Filtered Sales = CALCULATE(

SUM(Sales[Amount]),

ALLSELECTED(Sales)

How ALLSELECTED Works:

• With Slicers: If a slicer filters the data to a subset (e.g., only products
from certain regions), ALLSELECTED will respect that slicer selection
while ignoring other filters (like table row filters).
• Without Slicers: If no slicers are applied, ALLSELECTED behaves similarly
to ALL in removing all filters.

You might also like