Forum Discussion

wasirafi's avatar
wasirafi
Helper I
2 years ago
Solved

DAX expression - How to calculate Weightage average based on a condition

Hello Community,

 

I have been scratching my head for many hours to get a DAX for the following Data table

 

SKUDateTotal TimeReasonAvailabilityPerformance
Apple12-12-2334Break50%65%
Apple12-12-2322Reduced speed45%83%
Banana12-12-2342Reduced speed36%87%
Carrot12-12-2354Break37%46%
Apple13-12-2320Reduced speed65%44%
Banana13-12-2322Reduced speed12%39%
Banana13-12-2355Break35%44%
Carrot13-12-2311Break43%77%
Carrot13-12-2345Break29%61%
Apple14-12-2311Break34%37%
Apple14-12-2325Break80%63%
Banana14-12-2345Break33%35%
Carrot14-12-2339Break26%64%
Apple15-12-2320Break75%72%
Banana15-12-2333Break23%56%
Banana15-12-2335Reduced speed34%78%
Carrot15-12-2323Break23%38%
Carrot15-12-2370Break26%66%

 

I am trying to get weighted average (Availability) and Weighted Average (Performance) based on SKU or Date ( what ever i set as a filter). The result should only include "Break" as a Reason.

 

Ultimately i am trying to achieve the below Values (i dont need a table)

Weightage is Total time for the filter/ total time. For example 34/90=38%,11/90=12%

1) Filter at SKU Level

 Reason Filter : Break only     
    Availability Performance
Apple   61.9% 62.6%

2) Filter at Date

 Reason Filter : Break only     
Date   Availability Performance
12-12-23   42.0% 53.3%
13-12-23   33.1% 54.5%

https://1drv.ms/x/s!AjIGh95acxebg6F7lBBXTOoz7qe0rw?e=AIlOND 

9 Replies

  • 123abc's avatar
    123abc
    Community Champion

    To calculate the weighted average of Availability and Performance based on a condition (in this case, "Break") using DAX in Power BI, you can follow these steps:

    1. Create a Measure for Total Time for 'Break' Reason:

    TotalTime_Break =
    SUMX(
    FILTER(
    YourTableName,
    YourTableName[Reason] = "Break"
    ),
    YourTableName[Total Time]
    )

     

    Calculate the Weighted Average for 'Break' Reason:

    For the Availability and Performance weighted average, use the following measures:

     

    WeightedAvg_Availability = 
    DIVIDE(
        CALCULATE(
            SUMX(
                FILTER(
                    YourTableName, 
                    YourTableName[Reason] = "Break"
                ), 
                YourTableName[Total Time] * YourTableName[Availability]
            ),
            ALL(YourTableName)
        ),
        [TotalTime_Break]
    )
     
    WeightedAvg_Performance = 
    DIVIDE(
        CALCULATE(
            SUMX(
                FILTER(
                    YourTableName, 
                    YourTableName[Reason] = "Break"
                ), 
                YourTableName[Total Time] * YourTableName[Performance]
            ),
            ALL(YourTableName)
        ),
        [TotalTime_Break]
    )

     

     

    1. Using the Measures in Visuals:

      • If you want to see the weighted averages for each SKU with the 'Break' reason:

        • Drag and drop the SKU into a table or matrix visualization.
        • Drag the WeightedAvg_Availability and WeightedAvg_Performance measures into the values section of the visualization.
      • If you want to see the weighted averages for each Date with the 'Break' reason:

        • Similarly, drag and drop the Date into a table or matrix visualization.
        • Again, drag the WeightedAvg_Availability and WeightedAvg_Performance measures into the values section.

    Remember to replace YourTableName with the actual name of your table in Power BI.

    These DAX measures should give you the desired weighted averages based on the 'Break' reason either at the SKU level or Date level as per your filters in Power BI.

    • wasirafi's avatar
      wasirafi
      Helper I

      123abc  Thank you for sharing this, Unfortunately i am not getting the desired result Please See Pic from Power BI Versus what was expected( manual calculation in the excel sheet shared earlier). The sum of the weighted average cannot be more than 100% .