Power BI
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:
Answer:
Examples:
• Calculated Column:
Measure:
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])
Running Total =
CALCULATE(
SUM(Sales[Amount]),
Explanation:
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.
Answer:
CALCULATE modifies the filter context for a calculation.
Example:
Explanation:
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.
Explanation:
Certainly! Here's the detailed explanation for the requested DAX functions,
including examples and use cases:
Explanation:
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.
CALCULATE(SUM(Sales[Amount]), USERELATIONSHIP(Sales[ShipDate],
Dates[Date]))
Explanation:
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:
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:
Answer:
Examples:
• SUM:
SUMX:
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:
This formula ranks products based on their total sales in descending order.
Answer:
Example:
Answer:
Answer:
Example:
Optimized Measure =
Answer:
• 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:
SUM(Sales[Amount]),
ALLSELECTED(Sales)
• 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.