Forum Discussion
Group and Classify Measures in a Calculated Table
Hi,
I have a PBIX file with lots of measures created across a number of different tables, from different Divisions of our organisation.
I want to be able to show all these measures in a matrix with a marker for the Division and Business Unit for each measure. This information doesn't exist within the data, I'll need to add it in.
In the past I've done something similar using the New Parameter - Fields option under the Modelling tab. I would select the fields I want, give them a name and create the parameter. Then I would add in additional fields to give the measures a classification, as below:
ALL MEASURES = {
("Total Bequest Donation Amount (FYTD)", NAMEOF([Total Bequest Donation Amount (FYTD)]), 0, "Fundraising", "Gifts in Wills"),
("Total Bequest Donors (FYTD)", NAMEOF([Total Bequest Donors (FYTD)]), 1, "Fundraising", "Gifts in Wills"),
("Total New Bequestors (FYTD)", NAMEOF([Total New Bequestors (FYTD)]), 2, "Fundraising", "Gifts in Wills"),
("Average Bequest Donor Donation (FYTD)", NAMEOF([Average Bequest Donor Donation (FYTD)]), 3, "Fundraising", "Gifts in Wills"),
("% of Bequest Donors New Bequestors (FYTD)", NAMEOF([% of Bequest Donors New Bequestors (FYTD)]), 4, "Fundraising", "Gifts in Wills"),
("GIW Pledge Amount Vs Donation Amount (FYTD)", NAMEOF([GIW Pledge Amount Vs Donation Amount (FYTD)]), 5, "Fundraising", "Gifts in Wills"),
("% of GIW Pledge Amount Donated (FYTD)", NAMEOF([% of GIW Pledge Amount Donated (FYTD)]), 6, "Fundraising", "Gifts in Wills"),
("Total Regular Giving Donation Amount (FYTD)", NAMEOF([Total Regular Giving Donation Amount (FYTD)]), 7, "Fundraising", "Regular Giving")
}
This gives me the following calculated table:
When I go to add this to a matrix, I'm seeing each Measure repeated for each Business Unit, even though it doesn't have a classification for both, as below:
"Total Regular Giving Donation Amount (FYTD)" should be the only measure listed in the Regular Giving Business Unit.
On the matrix, I have Division and Business Unit in Rows, I have my date fields in Columns and the Measure Field in the measure bucket, with the matrix set up to show measures in rows.
Any idea how I can amend my DAX so measures only show under the relevant Business Unit?
Thanks,
Mark
Hi Anonymous
The Business Unit column can be used in a slicer, but not with the Parameter columns. However, you can use your parameter table as a placeholder for measure names in another measure. Ensure that in the dropdown next to the Parameter column name in the visual Show selected field is enabled so the fields are shown and not the values.
You will also need to apply the format strings dynamically
Please see the attached pbix.
9 Replies
- OwenAuger
Super User
Hi Anonymous ,
Unfortunately field parameters with additional grouping columns don't work this way when the grouping columns and field parameter column are included in the same visual.
When "Measure Field" is placed in the measure bucket, Power BI determines the set of fields (in this case measures) only once, at the visual level. The set of measures is not determined separately within different groupings of Division or Business Unit within the visual.
I would suggest either of these other methods, using a parameter table with a similar structure to your field parameter table. With these methods, a single measure is placed in the visual, using the parameter table which can be "filtered" depending on which values result in a non-blank measure value.
- Creating a "measure selection" measure that uses SWITCH to select the appropriate measure.
You can use a dynamic format string expression if needed.
See here for example:
https://data-goblins.com/power-bi/measure-selection - Creating a calculation group for measure selection, where each calculation item corresponds to a measure.
You can specify a format string expression per calculation item.
See here for example:
https://tenfingers.medium.com/combining-calculation-groups-in-power-bi-to-create-powerful-reports-cc06f85e8de6
https://tenfingers.medium.com/allowing-users-to-choose-any-two-different-metrics-to-compare-in-a-single-chart-with-calculation-297bfc1f3e98
https://data-goblins.com/power-bi/measure-selection (a method using calc group + TREATAS)
Please post back if needed.
Regards
- AnonymousNot applicable
Hi OwenAuger ,
I'm not looking for the user to be able to select the measure they want. I want to classify the measures I'll be showing them into groups. So the first option wouldn't work for this, as it's about allowing the user to choose. Correct?
For the second option, am I right in thinking that also would give the user a slicer of options to switch between?
I'm looking for something similar to what danextian has suggested. Do you have any alternative way to produce this output?
Thanks,
Mark
- OwenAuger
Super User
Hi Anonymous
Yes, understood 🙂 You are wanting to group the measures within a visual rather than allow the user to select measures by filtering.
The methods I suggested are intended to allow grouping within a visual, but also happen to be applicable to measure selection in general. Grouping within a visual is really a form of selection where each "row" of the visual is a selection corresponding to the filters applying to that row (due to the grouping columns).
I have attached a PBIX showing both methods.
1. A calculation group called Measure Selection.
- Each calculation item corresponds to a measure, identified by the calc item column Measure Name.
- Includes grouping columns Report Group and Metrics Group (and associated Ordinal columns). These can only be created as calculated columns in a calculation group. In this case I retrieved the values from the table mentioned below (Measure Selection Table).
- A format string is defined for each calculation item.
2. An imported table called Measures Selection Table.
- Each row corresponds to a measure, identified by the column Measure Name.
- Exactly the same structure as the Measure Selection calculation group.
- The measure Selected Measure uses SWITCH to select the appropriate measure based on the value of Measure Name (similar to danextian's example).
- This measure has a dynamic format string to select the appropriate number format per measure.
Measure Selection measure definition:
Selected Measure = SWITCH ( SELECTEDVALUE ( 'Measure Selection Table'[Measure Name] ), "Quantity", [Total Quantity], "Sales Amount", [Sales Amount], "Cost", [Total Cost], "Margin", [Margin], "Average Price per Unit", [Average Price per Unit], "Average Cost per Unit", [Average Cost per Unit], "Average Margin per Unit", [Average Margin per Unit], "Cost %", [Cost %], "Margin %", [Margin %] -- Otherwise BLANK )and the dynamic format string expression:
VAR ZeroDecimals = "#,0" VAR TwoDecimals = "#,0.00" VAR PctTwoDecimals = "0.00%" VAR Result = SWITCH ( SELECTEDVALUE ( 'Measure Selection Table'[Measure Name] ), "Quantity", ZeroDecimals, "Sales Amount", ZeroDecimals, "Cost", ZeroDecimals, "Margin", ZeroDecimals, "Average Price per Unit", TwoDecimals, "Average Cost per Unit", TwoDecimals, "Average Margin per Unit", TwoDecimals, "Cost %", PctTwoDecimals, "Margin %", PctTwoDecimals -- Otherwise BLANK ) RETURN ResultTo apply these in the report:
1. For the calculation group, place an arbitrary measure in Values (it is over-ridden by calc items), and grouping columns + Measure Name on Rows, plus required columns on Columns.
2. For the table with SWITCH measure, similar setup using the columns of Measure Selection Table, with Selected Measure in Values.
The reason that a field parameter containing measures with a hierarchy of grouping columns is unsuitable for grouping in a visual is that the list of measures displayed in the visual is defined once at the visual level, and Power BI won't selectively include the measures within a particular grouping of the visual. The end result is the same as if the full list of measures had been included in Values.
If you have a field parameter table defined, you can certainly write a SWITCH measure referencing that table as danextian showed, but this doesn't make use of any special features of the field parameter table, and is really the same as my method #2.
That's my take on it anyway. Hopefully that helps a bit! 🙂
Regards
- Creating a "measure selection" measure that uses SWITCH to select the appropriate measure.
- danextian
Super User
Hi Anonymous
The Business Unit column can be used in a slicer, but not with the Parameter columns. However, you can use your parameter table as a placeholder for measure names in another measure. Ensure that in the dropdown next to the Parameter column name in the visual Show selected field is enabled so the fields are shown and not the values.
You will also need to apply the format strings dynamically
Please see the attached pbix.
- danextian
Super User
You might wonder why we use the Parameter order column instead of the parameter name. Using the parameter name can lead to errors, as shown below
- AnonymousNot applicable
Hi danextian ,
I've downloaded your PBIX but I'm not quite able to get the same result that you do. Can you tell me where I'm going wrong?
On my existing parameter table, I've added a calculated colunmn for the BU. I've then added the Switching Measures measure, matching up with Order IDs from the parameter table:
But when I add the information to the matrix table, it's giving me this:
Any ideas what I'm doing wrong? Could you perhaps go through each steps from the beginning?
Thanks,
Mark
- danextian
Super User
Hi Anonymous
To get the desired result, you need to expand the hierarchy further. Regular Giving shows a value because it has only one measure name, so SELECTEDVALUE returns that single measure's order. However, Gift in Wills has seven measure order values, causing SELECTEDVALUE to return blank. The SWITCH measure relies on the value returned by SELECTEDVALUE of measures order, so if the current row results in a blank, the measure will also return blank.