Forum Discussion

Nun's avatar
Nun
Resolver I
1 year ago

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

49 Replies

  • 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]

    • Anonymous's avatar
      Anonymous
      Not applicable

      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.

      • Nun's avatar
        Nun
        Resolver I

        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. 

  • Nun's avatar
    Nun
    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 

     

    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 
  • Anonymous's avatar
    Anonymous
    Not applicable

    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.

    • Nun's avatar
      Nun
      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

      • Anonymous's avatar
        Anonymous
        Not 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.

  • Nun's avatar
    Nun
    Resolver I

    Hi,

    unfortunately, I didn't solve the issue. We need to consider that EUR/PZ is a measure and not an existing column. We have the column EUR, PZ but not EUR/PZ. We have the same agent that sells to different delivery customer, the same product (or different) in the same quarter with different EUR and different quantity PZ. Creating the rank, ofcourse there several rows with the same rank. Based on your model, I created a column EUR/PZ, then a column Rank, using the column EUR/PZ, that a measure TopEUR, 10% and the TopAvg, but the result is so far to be correct.

    Please take a look of this example for a selected product in Q2 2024 PZ and EUR are measure. The total PZ is 1202 for Q2, the calculated 10% is 120,2, 9761,07 is PZ*EUR/PZ. As end result what we need to get is the top price 345,49. Applying your formulas, I get for some reason, 363. Thanks!

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Nun , 

       

      Thank you for your continued efforts in testing the approaches we’ve provided. As some time has passed and multiple solutions have been attempted without achieving the desired outcome, we kindly suggest raising a support ticket with the Microsoft product team. They will be able to investigate the matter further and provide more in-depth assistance to help resolve the issue.

       

      You can raise a support ticket using the following link: Submit a product support request

      Additionally, if you do find a resolution through the support channel, we would sincerely appreciate it if you could share your findings with the community. Your insights may help other members facing similar challenges.

       

      Thank you for your patience and understanding. Please continue to engage with the Fabric Community for any further questions or support needs.

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Nun ,

         

        We are following up once again regarding your query. Could you please confirm if the issue has been resolved through the support ticket?

        If the issue has been resolved, we kindly request you to share the resolution or key insights here to help others in the community. If we don’t hear back, we’ll go ahead and close this thread.

        Should you need further assistance in the future, we encourage you to reach out via the Microsoft Fabric Community Forum and create a new thread. We’ll be happy to help.

         

        Thank you for your understanding and participation.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Nun ,

     

    Can you please confirm whether you have resolved issue. If yes, you are welcome to share your workaround and mark it as a solution so that other users can benefit as well. This will be helpful for other community members who have similar problems to solve it faster. 

    If we don’t hear back, we’ll go ahead and close this thread.Should you need further assistance in the future, we encourage you to reach out via the Microsoft Fabric Community Forum and create a new thread. We’ll be happy to help.

     

    Thank you.

  • Nun's avatar
    Nun
    Resolver I

    No, I didn't resolve the issue, in a post you were mentioning to create a table rank ( I remember correctly), but I cannot find that post.Thanks

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Nun ,

       

      As I mentioned in my previous message, please click the link provided. It will take you to the post I referred to earlier about creating a table rank.

       

      Thank you.

  • Anonymous's avatar
    Anonymous
    Not applicable

    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's avatar
      Nun
      Resolver I

      Hi,
      no I didn't. In the solution you proposed, it's not calculate the "remaining part". I try to figure out how to add it.

  • Nun's avatar
    Nun
    Resolver I

    Hi.

    Thanks so much for all. I will continue to find a solution by myself. I have contacted a partner support but the costs are not compatible with the request.

    Thank you

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Nun ,

       

      We did our best to assist you, but unfortunately, we were not able to achieve the desired result. Since you chose not to engage with Partner Support, we hope you are able to resolve the issue from your end.

      If you have found a solution, we would appreciate it if you could share your experience with the community, as it may help others with similar issues.

      According to our support policy, we will now close this thread. If you need further assistance in the future, please open a new thread and we will be happy to help.

       

      Thank you for your understanding and collaboration.