如何在Power-BI DAX中计算多个类别的平均值?

问题描述 投票:0回答:2

我有一个包含以下列的表格:

行业表

Industry_ID          Score    

1                          2

1                          3

2                          2

2                          4

3                          0

4                          2

我需要计算每个行业的平均值,然后计算这些平均值的平均值。

就像

的平均分数

1=(2+3)/2 =>2.5

2=(2+4)/2 =>3

3=0/1 => 0

4=2/1 => 2

然后计算这些平均值的平均值,即 (2.5+3+0+2)/4 => 1.85

这些表是直接查询的,所以请考虑这一点。任何帮助表示赞赏。谢谢你

powerbi dax powerbi-desktop
2个回答
0
投票
  1. 要创建不同值的平均值,请创建一个计算列:

     Average = 
     var no_ID = 'Table'[Industry_ID]
    
     Return
     AVERAGEX( 
            FILTER(ALL('Table'), 'Table'[Industry_ID] = no_ID),
           'Table'[Score]
            )
    

这将为您提供一个具有不同

Industry_ID
平均值的列。

  1. 要创建平均值的平均值,请创建一个度量:

    Measure = AVERAGEX(SUMMARIZE('Table', 'Table'[Industry_ID], 'Table'[Average]), 'Table'[Average])
    

最终输出-


0
投票

这里有两种方法可以实现这一目标:

只需在 RETURN 部分中在“Individual”和“Overall Average”变量之间切换,同时将此代码存储在单独的测量中

CALCULATE ( COUNTROWS ( Industry ) )
,以便可以在不同的地方重复使用它,而不会使代码变得冗长

Industry Average =
VAR AllIndustryAverages =
    AVERAGEX (
        ALL ( Industry[IndustryID] ),
        DIVIDE ( [Total Score], CALCULATE ( COUNTROWS ( Industry ) ) )
    )
VAR IndividualAverages =
    AVERAGEX (
        VALUES ( Industry[IndustryID] ),
        DIVIDE ( [Total Score], CALCULATE ( COUNTROWS ( Industry ) ) )
    )
RETURN
    IndividualAverages
Industry Average 2 =
VAR VisibleIndustries =
    VALUES ( Industry[IndustryID] )
VAR AllIndustryAverages =
    ADDCOLUMNS (
        ALL ( Industry[IndustryID] ),
        "Average",
            VAR CurrentIndustryTotalScore = [Total Score]
            VAR IndustryCount =
                CALCULATE ( COUNTROWS ( Industry ) )
            RETURN
                DIVIDE ( CurrentIndustryTotalScore, IndustryCount )
    )
VAR IndividualAverages =
    AVERAGEX (
        FILTER ( AllIndustryAverages, Industry[IndustryID] IN VisibleIndustries ),
        [Average]
    )
VAR OverallAverage =
    AVERAGEX ( AllIndustryAverages, [Average] )
RETURN
    IndividualAverages

© www.soinside.com 2019 - 2024. All rights reserved.