Power BI-查询中的矩阵值计数

时间:2019-04-04 10:46:51

标签: powerbi dax

我正在尝试创建一个矩阵表,该表将计算一个月内到期的作业数量以及它们是否按时完成。

目前,我可以做到这一点。 enter image description here

我尝试汇总数据,创建度量等。但无济于事。

我需要提供以下内容:

Desired output

我的数据源是一系列交易行,其中包括

1)交易截止日期 2)交易完成日期

术语定义:

Due in month        Count of due date
Done on time        Completed within due month
Outside time        completed outside due month
"%Perf"             percentage completed on time.

任何帮助,将不胜感激。

enter image description here

1 个答案:

答案 0 :(得分:0)

添加以下措施:

测量“到期”:

Due = COUNTROWS ( Table1 )

衡量“按时完成”:

Done on time = 
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
    CALCULATE ( 
        [Due],
        FILTER ( 
            Table1,
            MONTH ( Table1[completed] ) = DueMonth
        )
    )

测量“外部时间”:

Outside time = 
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
    CALCULATE ( 
        [Due],
        FILTER ( 
            Table1,
            MONTH ( Table1[completed] ) <> DueMonth && 
            NOT ISBLANK ( Table1[completed] )
        )
    )

测量“不完整”:

Incomplete = 
VAR DueMonth = MONTH ( FIRSTDATE ( Table1[due_date] ) )
RETURN
    CALCULATE ( 
        [Due],
        FILTER ( 
            Table1,
            ISBLANK ( Table1[completed] )
        )
    )

测量“%Perf”

% Perf = 
    DIVIDE ( 
        [Done on time],
        [Due],
        BLANK()
    )    

按报告中的due_date.month分组。

有关示例PBIX文件,请参见https://pwrbi.com/so_55513978/

在评论中更新问题后进行编辑:

如果确实需要,可以采用多种方法来创建将这些指标与按完成日期算起的每月计数结合在一起的可视化文件。这是一种方法:

创建“报告列”表:

Report Columns = 
VAR ListMeasures = 
    DATATABLE ( 
        "Header", STRING,
        "Sort Order", INTEGER,
        "First Of Month", DATETIME,
        { 
            {"Due in month", 1, BLANK() }, 
            {"Done on time", 2, BLANK() }, 
            {"Outside time", 3, BLANK() }, 
            {"Incomplete", 4, BLANK() }, 
            {"% Perf", 5, BLANK() }
        }
    )
VAR ListMonths = 
    CALCULATETABLE(
        GROUPBY ( 
            ADDCOLUMNS ( 
                DISTINCT ( Table1[completed] ),
                "Header", FORMAT ( Table1[completed], "YYYY-MMM" ),
                "Sort Order", YEAR ( Table1[completed] ) * 100 + MONTH ( Table1[completed] ),
                "First Of Month", DATE ( YEAR ( Table1[completed] ), MONTH ( Table1[completed] ), 1 )
            ),
            [Header], [Sort Order], [First Of Month]
        ),
        Table1[completed] <> BLANK()
    )
RETURN 
    UNION ( 
        ListMeasures, 
        ListMonths 
    )

Header列设置为Sort Order列排序

创建度量“报告度量”:

Report Measure = 
IF ( 
    NOT HASONEFILTER ( 'Report Columns'[Header] ), 
    BLANK(),
    SWITCH ( 
        VALUES ( 'Report Columns'[Header] ),
        "Due in month", [Due],
        "Done on time", [Done on time],
        "Outside time", [Outside time],
        "Incomplete", [Incomplete],
        "% Perf", [% Perf],
        CALCULATE ( 
            [Due],
            FILTER ( 
                Table1,
                DATE ( YEAR ( Table1[completed] ), MONTH ( Table1[completed] ), 1 ) = VALUES ( 'Report Columns'[First Of Month] )
            )
        )
    )
)

添加矩阵可视化效果,行中Table1[due_date],列中Report Columns[Header],值中[Report Measure]。格式适合。

enter image description here

更新的示例PBIX文件:https://pwrbi.com/so_55513978-2/