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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
NS05112021
Frequent Visitor

How do I sum two distinct counts based on two filtered columns in DAX?

I am new to Power BI. I am looking to count the distinct Employee ID's based on 2 columns in the report view. 

 

In this example below, I am trying to count all distinct Employee IDs if LOB = 'Guard' and Shift='NIGHT' PLUS count all distinct Employee IDs if LOB = 'Guard' and Additional Shift='NIGHT'

 

Here is example data: 

Employee IDDateLOBShiftAdditional Shift
16/4/2024GuardMID 
16/4/2024GuardMIDNIGHT
26/4/2024GuardNIGHT 
26/4/2024GuardNIGHT 
36/4/2024HKLDAY 
36/4/2024HKLDAY 
46/4/2024HKLMID 
46/4/2024HKLMID 
56/4/2024GuardMID 
56/4/2024GuardMID 
66/4/2024HKLNIGHT 
66/4/2024HKLNIGHT 
76/4/2024GuardNIGHTDAY
76/4/2024GuardNIGHT 
86/4/2024GuardDAY 
86/4/2024GuardDAY 
96/4/2024GuardDAY 
96/4/2024GuardDAY 

 

Here is the expected outcome I am looking for: 

Guards Night Shift  = 3

1 ACCEPTED SOLUTION

Hi @NS05112021 ,

You can update the formula of measure [Measure] as below, please find the details in the attachment.

Measure = 
VAR _date =
    SELECTEDVALUE ( 'Table'[Date] )
VAR _shift =
    SELECTEDVALUE ( 'Table'[Shift] )
VAR _lob =
    SELECTEDVALUE ( 'Table'[LOB] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[Employee ID] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[LOB] = _lob
                && (
                    'Table'[Date]
                        = IF (
                            'Table'[Shift] = "NIGHT"
                                || 'Table'[Additional Shift] IN { "DAY", "NIGHT" },
                            _date - 1,
                            _date
                        )
                        && ( 'Table'[Shift] = _shift
                        || 'Table'[Additional Shift] = _shift )
                )
        )
    )

vyiruanmsft_0-1719456825375.png

Best Regards

Community Support Team _ Rena
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

8 REPLIES 8
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1719284334788.png

 

 

expected result measure: =
VAR _list =
    SUMMARIZE (
        FILTER (
            Data,
            Data[LOB] = "Guard"
                && OR ( Data[Shift] = "NIGHT", Data[Additional Shift] = "NIGHT" )
        ),
        Data[Employee ID]
    )
RETURN
    COUNTROWS ( _list )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Thank you. what if I wanted to creaet a matrix or a table to summarize with the expected outcome like this. 

 

 GUARDSHKL
NIGHT31
DAY31
MID21

@Jihwan_Kim Thanks for your contribution on this thread.

Hi @NS05112021 ,

You can follow the steps below to get it, please find the details in the attachment.

1. Create two measures as below

Measure = 
VAR _shift =
    SELECTEDVALUE ( 'Table'[Shift] )
VAR _lob =
    SELECTEDVALUE ( 'Table'[LOB] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[Employee ID] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[LOB] = _lob
                && ( 'Table'[Shift] = _shift
                || 'Table'[Additional Shift] = _shift )
        )
    )
Count of employees = SUMX(VALUES('Table'[LOB]),[Measure])

2. Create a matrix visual

vyiruanmsft_0-1719394819948.png

Best Regards

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

Thank you. Now I need to add dates into this somehow. If _shift = Night then take selected date from my slicer and subtract 1 day. because I want to pull in the night before to be included in todays report. so any night shifts from 6/3 would count when 6/4 was selected. 

Also-
If 'Table'[Shift]= day then take selected date from my slicer. But if 'Table'[Additional Shift] = day then take the selected day from the slicer and subtract one day. 

If _shift =mid, use the selected day from the slicer for all. 

Hi @NS05112021 ,

You can update the formula of measure [Measure] as below, please find the details in the attachment.

Measure = 
VAR _date =
    SELECTEDVALUE ( 'Table'[Date] )
VAR _shift =
    SELECTEDVALUE ( 'Table'[Shift] )
VAR _lob =
    SELECTEDVALUE ( 'Table'[LOB] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[Employee ID] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[LOB] = _lob
                && (
                    'Table'[Date]
                        = IF (
                            'Table'[Shift] = "NIGHT"
                                || 'Table'[Additional Shift] IN { "DAY", "NIGHT" },
                            _date - 1,
                            _date
                        )
                        && ( 'Table'[Shift] = _shift
                        || 'Table'[Additional Shift] = _shift )
                )
        )
    )

vyiruanmsft_0-1719456825375.png

Best Regards

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

Thank you so much. I think I only have one more question. I want to create an identical matrix but instead of distinct count of employee ID, I want to sum to columns. Columns arenamed Table[Punch hours] and Table[Additional Hours]. 

I think I need the exact same formula only changing this part

DISTINCTCOUNT(Merge1[Employee Name.1.2.1])

@v-yiruan-msft 
Actually I think I need this logic on the new matrix. 

If 'Table'[Shift]= Night then take selected date from my slicer and subtract 1 day and  SUM(Table[Punch hours].

If 'Table'[Additional Shift] = Night then take selected date from my slicer and subtract 1 day and  SUM(Table[Additional hours].


If 'Table'[Shift]= day then take selected date from my slicer and SUM(Table[Punch hours] .

 

If 'Table'[Additional Shift] = day then take the selected day from the slicer and subtract one day and SUM(Table[Additional hours]. 

If _shift =mid, use the selected day from the slicer for all and SUM(Table[Punch hours] +  SUM(Table[Additional hours].

 

Still looking to have shift and lob in the view like this but replacing headcounts with hours. 

NS05112021_0-1719583301547.png

 

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.