简体   繁体   English

比较两个表并显示缺失的数据 - Power BI

[英]Compare two tables and display missing data - Power BI

I have hundreds of lines with different courses that my employees took during the year.我有数百条线路,我的员工在这一年里参加了不同的课程。 And I also have another table with the mandatory courses they have to take... I want to find a way to compare those two tables and see what courses are missing.而且我还有另一个表格,上面列出了他们必须参加的必修课程......我想找到一种方法来比较这两个表格,看看缺少哪些课程。 I would also appreciate if someone help me giving me ideas to perform a "completion bar" (example: employee 1: 78% courses taken)如果有人帮助我给我提供执行“完成栏”的想法,我也将不胜感激(例如:员工 1:参加了 78% 的课程)

To illustrate a little bit the data that I have:为了说明我拥有的数据:

Table 1(TRAINING 2021)表 1(2021 年培训)

Bruno |布鲁诺 | Course A课程A
Bruno |布鲁诺 | Course B课程B

Table 2(MANDATORY TRAINING 2021)表 2(2021 年强制性培训)

Course A课程A
Course B课程B
Course C课程C

Desired output (missing courses):期望输出(缺少课程):

Bruno |布鲁诺 | Course C课程C

Thanks a lot.非常感谢。

See the comments in the code, and explore the Applied Steps window, to understand the algorithm查看代码中的注释,并浏览应用步骤窗口,以了解算法

Training2021培训2021
在此处输入图片说明

MandatoryTraining2021强制性培训2021
在此处输入图片说明

M Code M码

let
    Training2021= Table.FromRecords(
        {[Name="Bruno", Course="Course A"],
        [Name="Bruno", Course="Course B"],
        [Name="George",Course="Course A"],
        [Name="George",Course="Course C"],
        [Name="Sandra",Course="Course C"]},
        type table [Name=Text.Type, Course=Text.Type]),
    MandatoryTraining2021= Table.FromRecords(
        {[Course="Course A"],
        [Course="Course B"],
        [Course="Course C"]},
        type table[Course=Text.Type]),

//group training table by Name
    group = Table.Group(Training2021,"Name",
                {"Courses", each [Course]}
            ),

//Missing courses using List.RemoveMatchingItems
    #"Added Custom" = Table.AddColumn(group, "Missed", each 
            Text.Combine(
                List.RemoveMatchingItems(
                    MandatoryTraining2021[Course],[Courses]),"; "), type text),

//Percent Completed
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "% Taken", each 
        List.Count([Courses])/Table.RowCount(MandatoryTraining2021),Percentage.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Courses"})


in
    #"Removed Columns"

Results结果
在此处输入图片说明

If your Table 1 is like this如果你的Table 1是这样的

| name   | course  |
|--------|---------|
| Bruno  | CourseA |
| Bruno  | CourseB |
| George | CourseA |
| George | CourseB |
| George | CourseC |
| Sam    | CourseB |

and Table 2 is like this Table 2是这样的

| mandatoryCourse |
|-----------------|
| CourseA         |
| CourseB         |
| CourseC         |

You need following two measures to achieve what you desire您需要以下两个措施来实现您的愿望

_missingCourses =
VAR _0 =
    CROSSJOIN ( 'Table 1', 'Table 2' )
VAR _1 =
    EXCEPT (
        SUMMARIZE ( _0, 'Table 1'[name], 'Table 2'[mandatoryCourse] ),
        SUMMARIZE ( _0, 'Table 1'[name], 'Table 1'[course] )
    )
RETURN
    CONCATENATEX ( _1, [mandatoryCourse], ",", [mandatoryCourse], ASC )



% Completion =
VAR _0 =
    CROSSJOIN ( 'Table 1', 'Table 2' )
VAR _1 =
    INTERSECT (
        SUMMARIZE ( _0, 'Table 1'[name], 'Table 2'[mandatoryCourse] ),
        SUMMARIZE ( _0, 'Table 1'[name], 'Table 1'[course] )
    )
VAR _3 =
    COUNT ( 'Table 2'[mandatoryCourse] )
VAR _4 =
    COUNTX ( _1, [mandatoryCourse] )
VAR _5 =
    DIVIDE ( _4, _3 )
RETURN
    _5

解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM