Categories: DAX
Tags:

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 have Profit and Revenue)
  • 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 Segment to 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 Segment on axis and Count of Sales or Sum of Profit as 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.