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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
HassanAshas
Helper V
Helper V

Fixing the DAX that calculates Count of Filtered Resources based upon a Measure

Hi, I have a dataset of employees working on month-to-month basis, 

The dataset looks something like this, 

 

DateEmp CodeNameGradeFieldAllocation
Aug-221Umair1ML0
1-Sep1Umair1ML0
Oct-221Umair1ML0.75
Nov-221Umair1ML1
Dec-221Umair1ML1
Aug-222Avery2Data Analysis1
1-Sep2Avery2Data Analysis0
Oct-222Avery2Data Analysis0
Nov-222Avery2Data Analysis0
Dec-222Avery2Data Analysis0
Aug-223Aryub2Web Dev0.9
1-Sep3Aryub2Web Dev1
Oct-223Aryub2Web Dev0.9
Nov-223Aryub2Web Dev0.75
Dec-223Aryub2Web Dev1
Aug-224Failak3ML0
1-Sep4Failak3ML0
Oct-224Failak3ML0.75
Nov-224Failak3ML1
Dec-224Failak3ML0.5

 

I have a Slicer for Date Range from which the user can select any date range and data will be filtered accordingly. 

 

I have a Matrix Visual that shows allocation of employees month-wise. There is an additional Filter is applied on this Matrix visual, that it only shows those employees which have been allocated (have allocation > 0) on all months being filtered. For example, If I am filtering from August to December, then only the records which have allocation greater than 0 FOR ALL THE MONTHS (August to December) will be shown. I have also attached two examples below to demonstrate this, 

HassanAshas_0-1674323890614.png

(As only the employee with Emp ID = 3 was allocated on all the months from August to Ddecember, so his record is shown only) 

 

HassanAshas_1-1674323926951.png

 

(3 Employees were allocated in both the months of Nov and December, so their records are shown) 

 

This is the measure that I am using currently in this Matrix Visual to filter out the employees which don't have allocation > 0 for all the months being filtered, 

 

 

Employee Allocated All Months Boolean = 
Var _count = 
        CALCULATE(
            DISTINCTCOUNT('Calendar'[Month]), 
            FILTER(
                ALLSELECTED(Competency),
                Competency[Emp Code] = MAX(Competency[Emp Code])
            )
        )
    Var _total = 
        CALCULATE(
            DISTINCTCOUNT('Calendar'[Month]), 
            ALLSELECTED(Competency)
        )
RETURN 
    IF(_count = _total, 1)

 

 

My problem comes in the other visuals. I have a visual that shows Total Unique Resources in the Data (As can be seen in the images above). I want this Visual to also act similarly (that is, if the employee is not allocated in all the months being filtered, then he shouldn't be considered in this count. So, if I am filtering from August to December, so this visual should show 1 employee only. And if I am filtering from November to December, so it should show 3 Employees) 

 

Same for the field-wise breakdown of the employees count, only those employees should be counted which are allocated in that specific date range. 

 

MY ATTEMPT: 

I tried to achieve this by changing my Total Resources Measure with the following DAX, however it doesn't seem to be giving correct results,

 

 

Total Unique Resources - Test = 
CALCULATE(
    DISTINCTCOUNT(Competency[Emp Code]), 
    FILTER(
        Competency, 
        [Employee Allocated All Months Boolean] = 1
    )
)

 

 

Results that I got with this DAX are as follows (it gives correct result for the Total Unique Resources/Employees) but doesn't give correct results in the Grade-wise Breakdown of the Resources (where technically it should show only one employee)

 

HassanAshas_1-1674376065852.png

 

Can anyone help me out in this? 

 

You can also download the complete Power BI File: https://drive.google.com/file/d/1pHapxFQ9MafSwt3yrqN3x5oKocv4pFcv/view?usp=sharing

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@HassanAshas First off, applause for an absolute textbook way to post a question on the forums. Bravo! PBIX is attached below signature:

Total Unique Resources - Test 2 = 
    VAR __AllocatedEmp = 
        SELECTCOLUMNS(
            FILTER(
                ADDCOLUMNS(
                    DISTINCT('Competency'[Emp Code]),
                    "__AllAllocated",[Employee Allocated All Months Boolean]
                ),
                [__AllAllocated] = 1
            ),
            "__EmpCode",[Emp Code]
        )
    VAR __Table = INTERSECT(DISTINCT('Competency'[Emp Code]), __AllocatedEmp)
    VAR __Result = COUNTROWS(__Table)
RETURN
    __Result

Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

1 REPLY 1
Greg_Deckler
Super User
Super User

@HassanAshas First off, applause for an absolute textbook way to post a question on the forums. Bravo! PBIX is attached below signature:

Total Unique Resources - Test 2 = 
    VAR __AllocatedEmp = 
        SELECTCOLUMNS(
            FILTER(
                ADDCOLUMNS(
                    DISTINCT('Competency'[Emp Code]),
                    "__AllAllocated",[Employee Allocated All Months Boolean]
                ),
                [__AllAllocated] = 1
            ),
            "__EmpCode",[Emp Code]
        )
    VAR __Table = INTERSECT(DISTINCT('Competency'[Emp Code]), __AllocatedEmp)
    VAR __Result = COUNTROWS(__Table)
RETURN
    __Result

Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.