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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register 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
Anonymous
Not applicable

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

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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

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
Anonymous
Not applicable

@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

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. 

@Anonymous 

Anonymous
Not applicable

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

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])

@Anonymous 
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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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