If you’re juggling multiple relationships and writing dozens of redundant measures, it’s time to simplify your approach using Power BI UDFs. Combined with Calculation Groups, these features can drastically reduce your DAX code while maintaining full flexibility across your model. This blog post shows how to manage inactive relationships and repetitive logic efficiently using this powerful combination.
The Problem: Too Many Measures for Inactive Relationships
Say you have a fact table (like Encounters or Sales) and a dimension table (like Users or Dates). But here’s the catch: the dimension table is related to the fact table through multiple columns and Power BI only allows one active relationship.
Traditionally, you had a few clunky options:
- Create multiple explicit measures using
USERELATIONSHIP() - Duplicate your dimension table for each needed relationship
- Unpivot or reshape the data model
All of these lead to bloated models and redundant DAX.
Let’s fix that.
Step 1: Create a User Defined Function (UDF)
In DAX Query View, define a function that accepts:
- A measure (as an expression)
- A column (for the relationship)
Sample Code:
define function MeasuresByDate(measure: decimal, column: any) =
CALCULATE(measure, USERELATIONSHIP(column, 'Date'[Date]))
This makes the DAX dynamic. You can now pass any measure and the column for the relationship you want to activate.
Step 2: Add the Function to Your Model
Once added, your model has a reusable UDF that works across different relationships and measures. You can now:
- Call this function in any DAX expression
- Avoid repeating long
CALCULATE()+USERELATIONSHIP()logic
But we’re not stopping here.
Step 3: Use a Calculation Group Instead of More Measures
Rather than creating multiple measures using the UDF, you can simplify even further using Calculation Groups.
Let’s say you need to analyze data by:
- Order Date (active)
- Ship Date (inactive)
- Due Date (inactive)
Instead of creating 3 measures per metric (like Sales, Freight, Quantity), create just one base measure and a calculation item for each relationship.
Example:
Create a Calculation Group called Measures By Date with items:
- Ordered: Uses the selected measure directly (active relationship)
- Shipped: Calls
MeasuresByDate([Selected Measure], 'Date'[ShipDate]) - Due: Calls
MeasuresByDate([Selected Measure], 'Date'[DueDate])
Now, Power BI will dynamically calculate the selected base measure using the correct relationship without you writing redundant DAX for every combo.
Step 4: Add the Calculation Group to Your Report
In your matrix visual:
- Add the calculation group to rows or columns.
- Add your base measures to values.
Power BI will apply each calculation item to the base measures giving you different relationship perspectives in a single visual, all powered by reusable logic.
The Payoff: Less Code, More Flexibility
Let’s do the math:
- 4 base measures × 3 relationships = 12 measures using traditional DAX
- With UDFs + Calculation Groups? Just 4 base measures + 3 calculation items = 7 total DAX elements
If you scale this to 10 measures, you go from 30+ measures down to 13. That’s a massive gain in maintainability and performance.
Want to Learn More?
Explore Power BI Training with Data Bear
Includes deep dives on advanced DAX, model optimization, and real-world use cases with UDFs and Calculation Groups.
Final Thoughts
If you’re tired of duplicating DAX or managing an overwhelming number of measures, it’s time to try Power BI UDFs with Calculation Groups. This approach not only reduces code but improves flexibility, performance, and reusability across your reports.




