The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Original Measures,
_CombinedWeightedPercentage =
IF(
HASONEVALUE(Volumes[Region]),
1,
-- Case when a single region is selected, calculate weighted percentage within the region
DIVIDE(
SUM(Volumes[Volume]),
CALCULATE(
SUM(Volumes[Volume]),
ALLEXCEPT(Volumes, Volumes[Region])
)
)
and,
_TotalWeightedCOP =
IF(
HASONEVALUE('CAD$,T,Ha'[Region]),
-- Case when a single region is selected: calculate weighted cost per sourcing area
SUMX(
SUMMARIZE(
'CAD$,T,Ha',
'CAD$,T,Ha'[Region],
'CAD$,T,Ha'[Sourcing Area],
'CAD$,T,Ha'[Growing Season],
"AvgInputCost", AVERAGE('CAD$,T,Ha'[Total Input Cost]),
"WeightedPercentage", [_CombinedWeightedPercentage]
),
[AvgInputCost] * [WeightedPercentage]
),
-- Case when all regions are selected: calculate weighted cost per region
SUMX(
SUMMARIZE(
'CAD$,T,Ha',
'CAD$,T,Ha'[Region],
"RegionAvgInputCost",
IF(
-- Check if region has only one variety
COUNTROWS(VALUES('CAD$,T,Ha'[Variety])) = 1,
-- Case 1: One variety in the region
AVERAGEX(
VALUES('CAD$,T,Ha'[Sourcing Area]),
AVERAGE('CAD$,T,Ha'[Total Input Cost])
),
-- Case 2: Multiple varieties in the region
SUMX(
VALUES('CAD$,T,Ha'[Sourcing Area]),
AVERAGEX(
VALUES('CAD$,T,Ha'[Variety]),
'CAD$,T,Ha'[_CurrencyCOP]
)
)
),
"RegionWeight", LOOKUPVALUE(Weights[Weight Region], Weights[Regions], 'CAD$,T,Ha'[Region])
),
[RegionAvgInputCost] * [RegionWeight]
)
)
I’m working with a large dataset that tracks the Cost of Production (COP) over the last 10 years, broken down by region, sourcing area, and variety.
I want to visualize the COP as a weighted average, where:
The weights come from production volumes, which differ by sourcing area.
Within a sourcing area, there can be multiple varieties, each with its own volume, so the weighted average must also account for the volume of each variety.
I created two DAX measures that calculate the global weighted average COP, using predefined weights from a [Weights] table. This works correctly for the overall (global) calculation.
However, the issue arises when I select only a few sourcing areas (either:
within the same region, or
across multiple regions).
In these cases, Power BI continues to apply the global weights from the [Weights] table instead of dynamically recalculating the weights based on the actual selected sourcing areas and their volumes.
As a result, the weighted COP is incorrect — it uses static global weights rather than adjusting the weights to the filtered selection. What I need is for Power BI to:
Dynamically recalculate weights based on selected sourcing areas and varieties.
Then multiply those new weights by the respective COPs and summarize that result.
To solve this I designed two new measures,
_SelectedWeight =
VAR TotalVolumeSelectedSourcingAreas =
CALCULATE(
SUM(Volumes[Volume]),
ALLSELECTED(Volumes[Sourcing Area])
)
VAR TotalVolumeSelectedRegions =
CALCULATE(
SUM(Volumes[Volume]),
ALLSELECTED(Volumes[Region])
)
VAR IsOneRegionSelected = HASONEVALUE(Volumes[Region])
VAR IsOneSourcingAreaSelected = HASONEVALUE(Volumes[Sourcing Area])
VAR NumberOfSelectedRegions =
CALCULATE(
COUNTROWS(VALUES(Volumes[Region])),
ALLSELECTED(Volumes[Region])
)
VAR TotalNumberOfRegions =
CALCULATE(
COUNTROWS(ALL(Volumes[Region]))
)
VAR AreAllRegionsSelected = NumberOfSelectedRegions = TotalNumberOfRegions
RETURN
SWITCH(
TRUE(),
-- Case 1: Only one sourcing area selected
IsOneSourcingAreaSelected, 1,
-- Case 2: One region selected without specifying sourcing areas
IsOneRegionSelected && NOT IsOneSourcingAreaSelected,
DIVIDE(
SUM(Volumes[Volume]),
TotalVolumeSelectedRegions
),
-- Case 3: NOT all regions selected, dynamic calculation based on selected sourcing areas
NOT AreAllRegionsSelected,
DIVIDE(
SUM(Volumes[Volume]),
TotalVolumeSelectedSourcingAreas
),
-- Case 4: All regions selected, use fixed region weights
AreAllRegionsSelected,
LOOKUPVALUE(
Weights[Weight Region],
Weights[Regions],
SELECTEDVALUE('CAD$,T,Ha'[Region])
)
)
and,
_COPWeighted =
SUMX(
ADDCOLUMNS(
VALUES('CAD$,T,Ha'[Sourcing Area]),
"WeightedSourcingAreaCOP",
VAR SourcingAvg = AVERAGEX(
VALUES('CAD$,T,Ha'[Variety]),
'CAD$,T,Ha'[_CurrencyCOP]
)
RETURN
SourcingAvg * [_SelectedWeight] -- Dynamically adjusted now
),
[WeightedSourcingAreaCOP]
)
However, the values produced now are too large. I am not sure where the issue lies.
Has anyone handled a similar issue or found a robust way to dynamically adjust weights in Power BI measures depending on the filters (region, sourcing area, variety) applied?
This is my first dashboard, and by far the hardest issue I have had to dealt with.
Thanks
Solved! Go to Solution.
Hi apolgiies for my late response, a lot of other work came up and I had to put this on hold and when I came back I forgot about my post.
I solved by adding the volumes as a new column to the existing data table. From there the process became much easier and I could simply average the sum of the cost for a specific sourcing area, summarise the volumes of all the other selected sourcing areas and then calculate the weights.
Multiply the weights with the specific costs, and that is how I got the weighted average. See the code below.
_TotalWeightedCOP =
VAR SourcingAreaTable =
SUMMARIZE(
'CAD$,T,Ha',
'CAD$,T,Ha'[Sourcing Area],
"AvgCOP", AVERAGEX(
FILTER(
'CAD$,T,Ha',
'CAD$,T,Ha'[Sourcing Area] = EARLIER('CAD$,T,Ha'[Sourcing Area])
),
[_CurrencyCOP] --- specific measure for the cost
),
"Volume", MAX('CAD$,T,Ha'[Volumes.Volume]) -- Use only once per sourcing area
)
VAR TotalVolume =
SUMX(SourcingAreaTable, [Volume])
RETURN
SUMX(
SourcingAreaTable,
DIVIDE([Volume], TotalVolume) * [AvgCOP])
Hi @PBIstruggling,
Thank you for reaching out and for providing detailed context regarding the issue with inflated values in your weighted average Cost of Production (COP) calculation.
Based on your explanation, we understand that the volume data is currently associated at the Sourcing Area level, while the weighted COP is being calculated at a more granular level—Variety. This mismatch in granularity can lead to incorrect weight application, especially if the same volume is being implicitly reused for each variety under a sourcing area. This results in inflated final COP values, as the same weight is being effectively multiplied multiple times.
To address this, we recommend a dynamic approach where volumes from each sourcing area are proportionally distributed to the varieties under it. This allows for weight calculations at the variety level, which better aligns with your COP granularity. You may implement this by:
Calculating the number of varieties per sourcing area.
Distributing the sourcing area volume equally across its varieties
Computing each variety’s share of the total selected volume to use as its weight.
Multiplying the individual COP of each variety by its computed weight, and finally summing these values.
This revised logic will ensure that weights are dynamically calculated based on the actual selection of regions, sourcing areas, and varieties, and will produce more accurate COP values under various filter conditions.
Kindly try implementing this approach in your model and let us know if you observe improved accuracy in the results. We would be happy to assist further if needed.
Thank you.
Hi @PBIstruggling,
Since we haven't heard back from you yet, I'd like to confirm if you've successfully resolved this issue or if you need further help?
If you've already resolved the issue, you can mark the helpful reply as a "solution" so others know that the question has been answered and help other people in the community. Thank you again for your cooperation!
If you still have any questions or need more support, please feel free to let us know. We are more than happy to continue to help you.
Hi @PBIstruggling,
we haven't heard back from you regarding our last response and wanted to check if your issue has been resolved.
If our response addressed by the community member for your query, please mark it as Accept Answer and give us Kudos. Should you have any further questions, feel free to reach out.
Thank you for being a part of the Microsoft Fabric Community Forum!
Hi @PBIstruggling,
May I ask if you have gotten this issue resolved?
If it is solved, please mark the helpful reply or share your solution and accept it as solution, it will be helpful for other members of the community who have similar problems as yours to solve it faster.
Thank you.
Hi apolgiies for my late response, a lot of other work came up and I had to put this on hold and when I came back I forgot about my post.
I solved by adding the volumes as a new column to the existing data table. From there the process became much easier and I could simply average the sum of the cost for a specific sourcing area, summarise the volumes of all the other selected sourcing areas and then calculate the weights.
Multiply the weights with the specific costs, and that is how I got the weighted average. See the code below.
_TotalWeightedCOP =
VAR SourcingAreaTable =
SUMMARIZE(
'CAD$,T,Ha',
'CAD$,T,Ha'[Sourcing Area],
"AvgCOP", AVERAGEX(
FILTER(
'CAD$,T,Ha',
'CAD$,T,Ha'[Sourcing Area] = EARLIER('CAD$,T,Ha'[Sourcing Area])
),
[_CurrencyCOP] --- specific measure for the cost
),
"Volume", MAX('CAD$,T,Ha'[Volumes.Volume]) -- Use only once per sourcing area
)
VAR TotalVolume =
SUMX(SourcingAreaTable, [Volume])
RETURN
SUMX(
SourcingAreaTable,
DIVIDE([Volume], TotalVolume) * [AvgCOP])
Hi @PBIstruggling,
I'm glad you found a solution and resloved the query. Thank you very much for sharing here.
Kindly mark your reply as the accepted solution so that others in the community can find it quickly.
Thankyou for connecting with Microsoft Community Forum.
Try to calculate weights per Variety (the lowest grain), based on Volume, and then multiply them by their respective COP, and finally aggregate the results.
_VarietyWeight =
VAR TotalVolume =
CALCULATE(
SUM(Volumes[Volume]),
ALLSELECTED(Volumes)
)
RETURN
DIVIDE(
SUM(Volumes[Volume]),
TotalVolume
)
calculate the weighted COP, multiply each variety’s COP by its share of the total volume:
_COPWeightedDynamic =
SUMX(
ADDCOLUMNS(
VALUES('CAD$,T,Ha'[Variety]),
"WeightedCOP",
'CAD$,T,Ha'[_CurrencyCOP] * [_VarietyWeight]
),
[WeightedCOP]
)
This assumes that _CurrencyCOP is the average COP per variety (you might want to use AVERAGEX(...) if that’s not already handled in your model).
Currently, there are no weights attached to varieties, we only have the COPs. The volumes are static, and attached primarly to the sourcing area.
The table looks as such,
Region - Volume - Sourcing Area
User | Count |
---|---|
11 | |
9 | |
6 | |
6 | |
5 |
User | Count |
---|---|
23 | |
14 | |
14 | |
9 | |
7 |