Forum Discussion
Calculate average price based on certain condition
Hi Nun ,
Sorry for the inconvenience and delay response.
Assume that your table name is SalesData:
TotalPZ = CALCULATE(SUM(SalesData[PZ]))
Try to Create a Ranked Table (sorted by EUR/PZ ascending)
RankedTable =
ADDCOLUMNS (
FILTER (
ADDCOLUMNS (
SalesData,
"EURPerPZ", SalesData[EUR] / SalesData[PZ]
),
NOT ISBLANK(SalesData[PZ]) && SalesData[PZ] > 0
),
"Rank", RANKX (ALL (SalesData), SalesData[EUR] / SalesData[PZ], , ASC, DENSE)
)
You can also pre-create EURPerPZ as a calculated column.)
Next, please use the below DAX to calculate Cumulative PZ and Filter for Top 10%
Top10PctTable =
VAR TotalPZ = CALCULATE(SUM(SalesData[PZ]))
VAR PZTarget = TotalPZ * 0.10
VAR SortedTable =
ADDCOLUMNS (
FILTER (
SalesData,
NOT ISBLANK(SalesData[PZ]) && SalesData[PZ] > 0
),
"EURPerPZ", SalesData[EUR] / SalesData[PZ]
)
VAR SortedByPrice =
ADDCOLUMNS (
ADDCOLUMNS (
SortedTable,
"CumulativePZ",
SUMX (
FILTER (
SortedTable,
(SalesData[EUR] / SalesData[PZ]) <= EARLIER(SalesData[EUR] / SalesData[PZ])
),
SalesData[PZ]
)
),
"IncludeRow",
IF (
SUMX (
FILTER (
SortedTable,
(SalesData[EUR] / SalesData[PZ]) <= EARLIER(SalesData[EUR] / SalesData[PZ])
),
SalesData[PZ]
) <= PZTarget,
1,
0
)
)
RETURN
FILTER (SortedByPrice, [IncludeRow] = 1)
As a final step, create a below measure to calculate Weighted Avg Price for Top 10% PZ:
Top10Pct_EURPerPZ =
VAR TotalPZ = CALCULATE(SUM(SalesData[PZ]))
VAR PZTarget = TotalPZ * 0.10
VAR SortedTable =
ADDCOLUMNS (
FILTER (
SalesData,
NOT ISBLANK(SalesData[PZ]) && SalesData[PZ] > 0
),
"EURPerPZ", SalesData[EUR] / SalesData[PZ]
)
VAR Ordered =
ADDCOLUMNS (
SortedTable,
"SortOrder", RANKX(SortedTable, [EURPerPZ], , ASC)
)
VAR Running =
ADDCOLUMNS (
Ordered,
"RunningPZ",
SUMX (
FILTER (
Ordered,
[SortOrder] <= EARLIER([SortOrder])
),
SalesData[PZ]
)
)
VAR Trimmed =
FILTER (
Running,
[RunningPZ] <= PZTarget
)
VAR TotalEUR =
SUMX (
Trimmed,
SalesData[EUR]
)
VAR TotalTrimmedPZ =
SUMX (
Trimmed,
SalesData[PZ]
)
RETURN
DIVIDE(TotalEUR, TotalTrimmedPZ)
I hope this will resolve your issue, if you need any further assistance, feel free to reach out.
If this post helps, then please give us Kudos and consider Accept it as a solution to help the other members find it more quickly.
Thankyou.
- Nun1 year ago
Resolver I
HI Anonymous , thanks so much for supporting, I really appreciate it!
I created the Ranked Table, then I tryed to create the table (Top10Pcttbale) to calculate Cumulative PZ and Filter for Top 10%, but I can't because I get this error:"The first argument of EARLIER/EARLIEST is not a valid column reference in the earlier row context.". Then I created the measure to calculate Weighted Avg Price for Top 10% PZ. But I do not understand the relationship between the three functions. My final aim is to determine the average price of the prices that have a quantity of 10% of the total, for a given period, for a selected product. In the attached tables, the top price 273.28 is the average price of product X for the first quarter, for the total of products that are equal to 10% of the overall total- Anonymous1 year agoNot applicable
Hi Nun ,
To calculate the weighted average EUR/pz for the top 10% of total PZ (prioritizing by highest price). I implemented a DAX-based approach and verified the result.
For your reference, I’ve attached the .pbix file so you can review the implementation and DAX measures used.
Please feel free to open the file and explore the visuals and calculations.
Thank you.
- Anonymous1 year agoNot applicable
Hi Nun ,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
Thank you.
- Nun1 year ago
Resolver I
Hi, yes you are right, my apologies. But that
Top10PctTable
is not taking in consideration the "remaining PZ", right?
- Anonymous1 year agoNot applicable
Hi Nun ,
Yes, it won’t include the remaining PZ because we’re explicitly specifying the top 10 PZ, so those others won’t be considered.
Thank you.