cancel
Showing results for 
Search instead for 
Did you mean: 
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.

View solution in original post

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!!!
Check out my External Tool for Power BI Desktop!
Microsoft Hates Greg's Quick Measures
Check out my latest book!

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
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Power BI October Update 2021.jpg

Power BI Release

Click here to read more about the October 2021 Release!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Teds Dev Camp Oct. 2021 768x460.jpg

Power BI Dev Camp - October 28th, 2021

Mark your calendars and join us for our next Power BI Dev Camp!