To create a dynamic aggregation measure in Power BI that switches between Sum, Average, Count, etc., based on user selection in a slicer, you can use DAX functions like SWITCH and SELECTEDVALUE. The measure should be responsive and update based on the slicer choice.
Best Approach Using DAX
-
Create an Aggregation Type Table
-
Manually create a table (e.g., AggregationType) with values: "Sum", "Average", "Count", etc.
-
Add this table as a disconnected table (no relationships).
-
Create a slicer using this table to let users choose an aggregation type.
-
Define the Dynamic Measure
DynamicAggregation =
VAR SelectedAggregation = SELECTEDVALUE(AggregationType[Type], "Sum")
RETURN
SWITCH(
SelectedAggregation,
"Sum", SUM(Sales[Amount]),
"Average", AVERAGE(Sales[Amount]),
"Count", COUNT(Sales[Amount]),
SUM(Sales[Amount]) -- Default to Sum if no selection
)
Ensure Correct Adaptation
-
Use SELECTEDVALUE to get the user's slicer choice.
-
SWITCH dynamically selects the right aggregation.
-
Set a default aggregation to avoid blank results.