Percentage of parts in a matrix issue using DAX within Power BI

时间:2019-04-08 13:45:45

标签: powerbi dax

Basically I'm trying to find the defect percentage per "Defect Code."

The machine number is in one table ([Knitting]) and the defect code/defect quantity are in another table ([Knitting Defects]). They are related by piece number and are being affected by a calendar table. Processed Qty is from the Knitting Defects table as depicted below.

Problem is when applying the percentage calculation below, it works perfectly for the machine, but the percentage of the parts (defect codes) come out as 100%.

enter image description here

I have three DAX measures that I'm using currently.

 Defect Quantity = 
    CALCULATE(
        SUMX( 'Knit Defects','Knit Defects'[Processed Qty]),
        left('Knit Defects'[Defect Code])="5"
    )


Defect and 1st Quality = 
   calculate(
       sum('Knit Defects'[Processed Qty]),
           'Knit Defects'[Defect Code] = ""
       ) + [Defect Quantity]


Defect Percentage = 
  iferror([Defect Quantity] / [Defect and 1st Quality] ,0)

1 个答案:

答案 0 :(得分:0)

问题出在您的[缺陷和第一质量]措施上。如果将其放到矩阵中,您会发现每个缺陷描述的值都与[缺陷数量]相同:

enter image description here

只有在[缺陷和第一质量]的第一部分得到衡量时,才有可能

Defect and 1st Quality = 
   calculate( sum('Knit Defects'[Processed Qty]),
           'Knit Defects'[Defect Code] = "")

总是返回0或空白。

为什么会这样?因为在您的视觉过滤器中,您仅选择了以“ 5”开头的缺陷代码。因此,在此方法中,您实质上是在要求Power BI为从“ 5” AND 等于“”开始的缺陷代码计算已处理数量的总和。由于此类组合不存在,因此该公式返回空白。

要解决此问题,您需要先“消除”视觉级过滤器的影响,然后应用新的过滤器:

Defect and 1st Quality =
CALCULATE (
    SUM ( 'Knit Defects'[Processed Qty] ),
    ALL ( 'Knit Defects'[Defect Description] ),
    'Knit Defects'[Defect Code] = ""
) + [Defect Quantity]

结果:

enter image description here

一些其他建议:

您可以按以下方式重新编写“缺陷百分比”度量:

Defect Percentage = DIVIDE([Defect Quantity], [Defect and 1st Quality])

它执行相同的操作,但是效率更高,并且代码更简洁。

此外,我建议您重新访问数据模型设计。需要将其转换为正确的开始模式-就目前而言,它存在许多尺寸建模问题。如果您不解决它们,DAX将继续为您挑战。