To create dynamic Profit Margin Segmentation in Power BI using interquartile range (IQR) logic that updates with filters, follow this step-by-step guide using DAX:
๐งฎ Objective:
Segment profit margins into:
- High Profit โ greater than Q3
- Medium Profit โ between Q1 and Q3
- Low Profit โ less than Q1
Where Q1 (25th percentile) and Q3 (75th percentile) are calculated dynamically, and the measure should respond to filters (e.g., by region, product, etc.).
โ Assumptions:
You have a Sales table with at least these columns:
[ProfitMargin](or calculate it if you only haveProfitandRevenue)- You want to segment each row into High / Medium / Low
๐ก Step 1: Create Profit Margin Column (if not available)
Profit Margin = DIVIDE(Sales[Profit], Sales[Revenue])
๐ก Step 2: Create Q1 and Q3 as Measures
Q1 Profit Margin =
PERCENTILEX.INC(
ALLSELECTED(Sales),
Sales[Profit Margin],
0.25
)
Q3 Profit Margin =
PERCENTILEX.INC(
ALLSELECTED(Sales),
Sales[Profit Margin],
0.75
)
๐ก Step 3: Create Dynamic Segmentation Measure
Profit Margin Segment =
VAR Q1 = [Q1 Profit Margin]
VAR Q3 = [Q3 Profit Margin]
VAR CurrentProfitMargin = SELECTEDVALUE(Sales[Profit Margin])
RETURN
SWITCH(
TRUE(),
CurrentProfitMargin < Q1, "Low Profit",
CurrentProfitMargin >= Q1 && CurrentProfitMargin <= Q3, "Medium Profit",
CurrentProfitMargin > Q3, "High Profit",
"Undefined"
)
๐ Step 4: Use It in Your Visuals
- Add this
Profit Margin Segmentto a Table or Matrix visual - Use slicers to filter by region, product, date, etc.
- It will dynamically update based on the filtered data
๐ Optional Tips
- Use a Stacked Column Chart with
Profit Margin Segmenton axis andCount of SalesorSum of Profitas value. - You can format the segments with colors using Conditional Formatting.
- If using a calculated column instead of a measure for segmenting, it wonโt respond to slicers/filters.
