Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Nun
Resolver I
Resolver I

Calculate average price based on certain condition

I have this table, 

AgentPZEUR/pz
A100280
 50275
C220260
D130258
E300255
F

500
253
G80252
H20240
I300239
J700238
K750235
L50225
M60220
N40215
O200210

PZ are calculated based on a formula sum (PC), EUR/pz is a formula too sum(eur)/sum(PC)
now I need to calculate the average price of highest prices falling within 10% of total PC. An example: in the table the total PC is 3500, the 10% is 350 so A,B,C are the top prices which have the pz amount within 10%, so 

we EUR 93750 (below table) PC 350 TOP EUR *PC 268 (93750/350),

PC x EUR/PC
28000
13750
52000
48 REPLIES 48

HI @v-tsaipranay , 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

Hi, yes you are right, my apologies. But that 

Top10PctTable

 is not taking in consideration the "remaining PZ", right?

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.

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.

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.

Hi, 

thanks so much for all the support, I tested the file attached, but the result is not what it should be. Please find enclosed how should be calculated the avg based on the top price with a PZ amount excatly macthing with the 10% of total PZ.

Considering that the 10% is 350, I will take all the agents with till the total is equal to the 10% (A-B-C). now the total doesn't match because it is 370, so from the last will be used 200 (370-350 is 20, so from C=220, to match to 350 we reduct it by 20). Then we need to moltiply the PZ for the EUR/pz, but for agent C will be 200*8,08. The total will be diveded by the 10%(350). The result is 8.767. In the power BI file the Top10Pct_Avg_Price is 9.29. Hope I was clear. Thanks!

Nun_0-1748506358048.png

 

Hi @Nun ,

 

I’ve reviewed and updated the file. It appears the difference in results was due to a mismatch between your dataset and the one I initially referenced.

The calculations and measures you used were correct—no issues there. The logic was sound; only the data source differed.

I am also including the screenshot for your reference:

vtsaipranay_0-1748565097391.png

 

Please feel free to open the file and explore the visuals and calculations.

 

Thank you.

 

 

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.

Hi, thanks for supporting.

I am testing it, because our scenario is a little different. First of all Eur/PZ is a measure, we have Eur for each row. It means we have the same agent that can buy the same product for several times in the same quarter, with different prize (Eur) and different quantity (PZ). 

Hi @Nun ,

 

May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

 

Thank you.

Nun
Resolver I
Resolver I

Hello,
thanks a lot! Here a table consider that PZ is sum of Pz (measure) and EUR/PZ is a measure too.I should get 

Nun_0-1747297392349.png

 

CustomerDelivery customerPZEUR/pz
AAA4,631296,6076441
ABB4,104291,4205653
ACC23,39286,9392903
ACC4,961282,9953638
A5,623282,4399787
A 10,254278,0856251
A 18,298277,7456553
A 15,877277,1902752
A 5,623276,6548106
A 15,388276,4121393
A 6,899276,2095956
A 10,915274,5112231
A 14,16273,9413842
A 11,867271,0878908
A 9,922268,8218101
A 17,522268,3449378
B 26,46266,9780801
C 40,684266,795792
A 5,292266,7611489
A 9,592264,8415346
A 18,524264,3462535
A 9,592264,2003753
A 21,5263,992093
D 172,329263,9917251
A 21,791262,3950255
A 9,262259,0671561
A 5,623257,5866975
A 74,689257,2599713
A 9,922255,1330377
A 42,338255
A 491,009254,8750023
…. 16,207252,7210465
  19,912252,5271193
  228,743249,5453413
  50,548248,844069
  90,31245,6905105
  134,544245,2783476
  9,016243,5337178
  259,57242,9669453
  16,207241,8905411
  18,705240,7399091
  127,008238,1796422
  37,008237,6513186
  58,579237,2280169
  43,332236,7659005
  54,874233,3671684
  5,954232,8249916
  41,636232,3311557
  40,683232,3184131
  4,631230,969553
  23,816230,5710447
  43,324229,251685
  37,231227,5069163
  55,441226,3701953
  4,874224,3906442
  29,108222,9359626
  93,878222,7130957
  9,592220,6380317
  5,954215,6415855
  85,931214,1419278
  54,104173,5557445
Total 2838,761 
bhanu_gautam
Super User
Super User

@Nun 

Create a measure to calculate the total PC:

TotalPC = SUM('Table'[PZ])

 

Create a measure to calculate 10% of the total PC:

ThresholdPC = [TotalPC] * 0.1

 

Create a calculated column to rank the rows by EUR/pz:

Rank = RANKX('Table', 'Table'[EUR/pz], , DESC, DENSE)

 

Create a measure to calculate the cumulative sum of PZ:

DAX
CumulativePZ =
CALCULATE(
SUM('Table'[PZ]),
FILTER(
ALL('Table'),
'Table'[Rank] <= MAX('Table'[Rank])
)
)

 

Create a measure to filter the rows within the 10% threshold:

DAX
TopPricesPZ =
CALCULATE(
SUM('Table'[PZ]),
FILTER(
'Table',
[CumulativePZ] <= [ThresholdPC]
)
)

 

Create a measure to calculate the sum of EUR for the selected rows:

DAX
TopPricesEUR =
CALCULATE(
SUM('Table'[EUR/pz] * 'Table'[PZ]),
FILTER(
'Table',
[CumulativePZ] <= [ThresholdPC]
)
)

 

Finally, create a measure to calculate the average price:

DAX
AveragePrice = [TopPricesEUR] / [TopPricesPZ]




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Hi @Nun ,

Thank you for reaching out to the Microsoft Fabric Community Forum.

 

As you've described, you're aiming to compute the average unit price (EUR/pz) of the highest-priced entries, constrained to the top 10% of total PZ volume. The idea is to rank entries by EUR/pz in descending order and include rows until their cumulative PZ reaches the 10% threshold then calculate a weighted average on that subset.

The approach provided by @bhanu_gautam  captures this logic accurately using a combination of ranking, cumulative sums, and conditional filters in DAX.

As a small note, please ensure that missing or blank values (such as null PZ or EUR/pz) are excluded in your model to avoid calculation errors.

 

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.

Hello, I had to create a table because I got the error of a circula dependency was detected. After that when I use the average formula, I get an error fetching data: there's not enough memory to complete this operation. 

Hi @Nun ,

 

You want to find the average unit price (EUR/pz) for the top 10% of your total volume based on price. The method you used is correct but caused errors because you created calculated columns that depend on each other, which Power BI doesn’t allow.

The memory error happens because processing large data inefficiently takes too much space. To fix this, use DAX measures instead of calculated columns, simplify your data, and write formulas that don’t refer to themselves.

 

To avoid these issues and improve performance, we suggest the following:

  • Implement logic using Measures instead of Calculated Columns/Tables, measures are evaluated at query time and are more memory-efficient, avoiding circular references.
  • Ensure rows with missing values for PZ or EUR/pz are excluded to prevent unexpected behavior:
FILTER('YourTable', NOT(ISBLANK('YourTable'[PZ])) && NOT(ISBLANK('YourTable'[EUR/pz])))
  • Use Variables for Intermediate Steps, this helps streamline execution and improve clarity:
AveragePrice :=
VAR TotalPZ = SUM('YourTable'[PZ])
VAR Threshold = TotalPZ * 0.1
VAR RankedTable =
    ADDCOLUMNS(
        FILTER(ALL('YourTable'), NOT(ISBLANK('YourTable'[PZ])) && NOT(ISBLANK('YourTable'[EUR/pz]))),
        "Rank", RANKX(ALL('YourTable'), 'YourTable'[EUR/pz], , DESC, DENSE)
    )
VAR CumulativeTable =
    ADDCOLUMNS(
        RankedTable,
        "CumulativePZ",
        CALCULATE(
            SUM('YourTable'[PZ]),
            FILTER(RankedTable, [Rank] <= EARLIER([Rank]))
        )
    )
VAR FilteredRows =
    FILTER(CumulativeTable, [CumulativePZ] <= Threshold)
VAR TopPZ = SUMX(FilteredRows, 'YourTable'[PZ])
VAR TopEUR = SUMX(FilteredRows, 'YourTable'[PZ] * 'YourTable'[EUR/pz])
RETURN
    DIVIDE(TopEUR, TopPZ)

 

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.

Hi! 
EUR/pz is a formula, it is

sum(table[EUR])/sum(tabel[pz])
Thanks!
 

 

for some reason these formulas the result is blank TopPricesPZ and 

TopPricesEUR the result is blank, 
Nun_3-1746533850318.png

 

 

 

 

Hi @Nun ,

 

The reason you're seeing blanks for TopPricesPZ and TopPricesEUR is because your EUR/pz value is an aggregate measure and doesn't exist at row level. This prevents DAX functions like SUMX or RANKX from working properly.

To resolve this, please create a calculated column:

EURperPZ = DIVIDE('Table'[EUR], 'Table'[PZ])

Then update the logic to use this column instead of a measure. This ensures each row has a value, enabling ranking and filtering to work correctly.

 

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.

 

I get an average but it is not correct. I try to explain myself better. I have this table

BuyerPZEUR/PZ
A2341.7592592592593
B10326.6785466785467
C15310.236739175187
D11306.7788638878643
E8295.0180972078594
F10282.64568401371145

If I take for example sale F which is 10 pieces for the selected period. But these 10 pieces are the sum of the sale of the same product several times, and that 10 is obtained with the formula sum(amount of pieces) for this product

product1-3,4410
product12,27342
product14,46322

the average that I m getting is 546,48, ToppriceEUR -617,52 ToppricesPZ -1,13(which is -3,4+2,27). In In practice, it should use not the single row of the product, but PZ and EUR/PZ, so the TOP average should take the PZ 2 and 10 whitin 10%(so if the tot PZ is 55 and 10% is 5,5, it should take PZ 2 and PZ 3,5 to reach the 5,5) and the prices 341 and 326.
I am using:

TopPricesEUR =
CALCULATE(
SUMX(table[AMOUNT_PZ]*table[ EUR/PZColumn]),
FILTER(
'table,
[CumulativePZ] <= [TenPercentVolume]
)
)
TopPricesPZ =
CALCULATE(
SUMX('Table,table[AMOUNT_PZ]),
FILTER(
'table',
[CumulativePZ] <= [TenPercentVolume]
)
)
Thanks so much!

Hi,

That poasted data does not appear properly.  If possible, could you share the download link of an MS Excel file.  Put your data there and show the MS Excel formulas there used to arrive at the result.  I will convert them into measures.  If you cannot write formulas there, then via comments/textboxes, tell us what you want to do.

Hello. Can you please answer the request? If further information is required, please ask.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.