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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
kbol
Frequent Visitor

Count on Dim Table Based on Fact Table Filtering

Hello,
I've got a model like this:

model.PNG

 

My goal is to count the number of rows in the Projects table that have at least 1 corresponding row in the StaffProj for a given selection of Staff. My attempt was this measure:

Projs With Selected Staff = COUNTROWS(FILTER(
    Projects,
    COUNTROWS(StaffProj)
))

 

This measure instead simply yields a count of Projects, regardless of what Staff selection makes. To give an example, first with no filtering (displays 3 correctly):

good.PNG

When filtering on Staff=B, though, it still displays 3 even though it should be 2:

bad.PNG

 

I recognize in this model that I could get this information purely from the StaffProj table, but the actual situation/model is more complex. Why doesn't this approach work and is there an alternate way of getting the correct counts "from" the Projects table?

1 ACCEPTED SOLUTION
v-eachen-msft
Community Support
Community Support

Hi @kbol ,

 

You could try the following measure.

Projs With Selected Staff =
CALCULATE (
    DISTINCTCOUNT ( 'StaffProj'[Project] ),
    ALLSELECTED ( 'Staff'[Name] )
)

Here is the test result.

3-2.PNG3-1.PNG 

 

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

View solution in original post

4 REPLIES 4
v-eachen-msft
Community Support
Community Support

Hi @kbol ,

 

You could try the following measure.

Projs With Selected Staff =
CALCULATE (
    DISTINCTCOUNT ( 'StaffProj'[Project] ),
    ALLSELECTED ( 'Staff'[Name] )
)

Here is the test result.

3-2.PNG3-1.PNG 

 

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

Hello @v-eachen-msft ,

 

Thank you, that idea works. I am quite surprised that the FILTER expression on Project in my attempt apparently changes the context such that it discards the filtering from Staff on StaffProj, but using that ALLSELECTED idea addresses that issue with this measure:

Projs With Selected Staff = CALCULATE(
    COUNTROWS(Project),
    FILTER(
        Project,
        CALCULATE(COUNTROWS(StaffProj), ALLSELECTED(Staff))
    )
)

I've accepted your post as the solution since it is more logical in this scenario. I don't fully understand why we need the ALLSELECTED here, but it does work.

Greg_Deckler
Super User
Super User

Perhaps a column in Project table like:

 

Count = COUNTROWS(RELATEDTABLE('StaffProj')

 

Then you could create a measure:

 

Project Count = COUNTROWS(FILTER('Projects'),[Count] > 0)


@ 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!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

Hi,

I'm afraid that Project column solution doesn't quite work because the filtering on Staff can change and the column is computed at refresh time, including all Staff. Thus with the above data is evaluates to 6 for every project, each having two associated staff. 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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