Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Prepping for a Fabric certification exam? Join us for a live prep session with exam experts to learn how to pass the exam. Register now.

Reply
Anonymous
Not applicable

Matrix and percentage of total for two categories

How can I create two tables like this (data below) for each categorie and department:

%departure is grand total/total population
 

Table 1

pat123_1-1670943520166.png

Table 2:

pat123_2-1670943575794.png

 

 

I have two HR data sets below; one is data of all employees by department and categories (total population) and the second is the data set of employees that left and their leave reason (departure data).

 

data:

IDcategorieDepartment
1011BD1
1002BD1
1004CD2
1011CD2
1012AD2
1013AD1
1014CD2
1009BD2
1016BD2
1017CD2
1018AD3
1019CD2
1020BD2
1021BD3
1022BD3
1023AD1

 

IDcategorieDepartmentLeave reason
1001AD1Retirement
1002BD1Lay-off
1003CD3Retirement
1004CD2Resignation
1005AD2Resignation
1006AD1Retirement
1007CD2Lay-off
1008BD3Retirement
1009BD2Resignation
1010CD1Discharge
1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

Please try to use the function SELECTCOLUMNS().

It returns a table with selected columns from the table and new columns specified by the DAX expressions.

 

For more details, please refer to:

SELECTCOLUMNS function (DAX) - DAX | Microsoft Learn

 

Here is the DAX:

Table 3 = 
VAR _a =
    ADDCOLUMNS (
        CROSSJOIN (
            SELECTCOLUMNS (
                'Table 2',
                "ID", [ID],
                "categorie", [categorie],
                "Department", [Department]
            ),
            { "Grand Total" }
        ),
        "Sort", 5
    )
VAR _b =
    ADDCOLUMNS ( CROSSJOIN ( 'Table 1', { "Total population" } ), "Sort", 6 )
VAR _c =
    UNION (
        _a,
        _b,
        ADDCOLUMNS (
            SELECTCOLUMNS('Table 2',"ID",[ID],"categorie",[categorie],"Department",[Department],"Leave reason",[Leave reason]),
            "Sort",
                SWITCH (
                    [Leave reason],
                    "Discharge", 1,
                    "Lay-off", 2,
                    "Resignation", 3,
                    "Retirement", 4
                )
        )
    )
RETURN
    _c

Output:

vjianbolimsft_0-1671605485453.png

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-jianboli-msft
Community Support
Community Support

Hi @Anonymous ,

 

Please try:

First create a new column:

Table 3 =
VAR _a =
    ADDCOLUMNS (
        CROSSJOIN (
            SELECTCOLUMNS (
                'Table 2',
                "ID", [ID],
                "categorie", [categorie],
                "Department", [Department]
            ),
            { "Grand Total" }
        ),
        "Sort", 5
    )
VAR _b =
    ADDCOLUMNS ( CROSSJOIN ( 'Table 1', { "Total population" } ), "Sort", 6 )
VAR _c =
    UNION (
        _a,
        _b,
        ADDCOLUMNS (
            'Table 2',
            "Sort",
                SWITCH (
                    [Leave reason],
                    "Discharge", 1,
                    "Lay-off", 2,
                    "Resignation", 3,
                    "Retirement", 4
                )
        )
    )
RETURN
    _c

Then sort the value by column [Sort]:

vjianbolimsft_0-1670998038431.png

Then create a measure and apply it to the matrix visual:

Measure =
VAR _a =
    CALCULATE (
        COUNT ( 'Table 3'[ID] ),
        FILTER ( 'Table 3', [Value] = "Grand Total" )
    )
VAR _b =
    CALCULATE (
        COUNT ( 'Table 3'[ID] ),
        FILTER ( 'Table 3', [Value] = "Total population" )
    )
VAR _c =
    FORMAT ( DIVIDE ( _a, _b ), "Percent" )
RETURN
    IF ( ISINSCOPE ( 'Table 3'[Value] ), COUNT ( 'Table 3'[ID] ), _c )

vjianbolimsft_1-1670998115098.png

Change the name of the visual's subtotal:

vjianbolimsft_2-1670998198550.png

Final output:

vjianbolimsft_3-1670998263191.png

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

 

Anonymous
Not applicable

Thank you so much! When I try to create the table with the first measure I get this error message however: Each table argument of 'UNION' must have the same number of columns.

In reality my Table 1 and 2 have a lot more columns. I changed table 1 to select the same columns as 2, but I still get the message. I found that it is because of table 2, it has more columns. How can I add selectcolumns statement in the below to select the same columns?

ADDCOLUMNS (
            'Table 2',
            "Sort",
                SWITCH (
                    [Leave reason],
                    "Discharge", 1,
                    "Lay-off", 2,
                    "Resignation", 3,
                    "Retirement", 4

Thanks. 

Hi @Anonymous ,

 

Please try to use the function SELECTCOLUMNS().

It returns a table with selected columns from the table and new columns specified by the DAX expressions.

 

For more details, please refer to:

SELECTCOLUMNS function (DAX) - DAX | Microsoft Learn

 

Here is the DAX:

Table 3 = 
VAR _a =
    ADDCOLUMNS (
        CROSSJOIN (
            SELECTCOLUMNS (
                'Table 2',
                "ID", [ID],
                "categorie", [categorie],
                "Department", [Department]
            ),
            { "Grand Total" }
        ),
        "Sort", 5
    )
VAR _b =
    ADDCOLUMNS ( CROSSJOIN ( 'Table 1', { "Total population" } ), "Sort", 6 )
VAR _c =
    UNION (
        _a,
        _b,
        ADDCOLUMNS (
            SELECTCOLUMNS('Table 2',"ID",[ID],"categorie",[categorie],"Department",[Department],"Leave reason",[Leave reason]),
            "Sort",
                SWITCH (
                    [Leave reason],
                    "Discharge", 1,
                    "Lay-off", 2,
                    "Resignation", 3,
                    "Retirement", 4
                )
        )
    )
RETURN
    _c

Output:

vjianbolimsft_0-1671605485453.png

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

Check out the April 2025 Power BI update to learn about new features.

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

Find out what's new and trending in the Fabric community.