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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Lucy64
Helper III
Helper III

Count by Category

Having resolved one issue on my project, I now have come accross this one that puzzles me.

 

I have a line/column chart which is showing total employes for each year, what I want is for the pie charts to show the total current employes for the last date range selected, split by gender and another by age group.  What puzzles me is why I am getting negative numbers for some groups, ie by Gender the Total 25 is correct, Male 21 is correct, but why do I get a negative number for female.  Also by Age Group, all groups have a negative number but the overall total is correct.  What is happening?

Screenshot.PNG

1 ACCEPTED SOLUTION
Sean
Community Champion
Community Champion

@Lucy64

Remember with this setup your DATE table has only 1 Active Relationship with the PERSONAL table based on the HIRE_DATE!
Therefore when you filter/slice the data ONLY what matches your selection in the HIRE_DATE column will be considered!
So anytime you use the Year or Date Slicer or Timeline to limit the dates

it will filter your table ONLY considering what matches your selection in the HIRE_DATE and not the TERM_DATE!

 

However note that your Measures will produce the Correct Result because you use the USERELATIONSHIP function.

 

I created these Measures and got the same exact results as before:

 

Number Hired = COUNTA(PERSONAL[HIRE_DATE])

Number Fired =
CALCULATE (
    COUNTA ( PERSONAL[TERM_DATE] ),
    USERELATIONSHIP ( DATES[Date], PERSONAL[TERM_DATE] )
)

Balance = [Number Hired] - [Number Fired]

Number of Current Employess =
CALCULATE (
    [Balance],
    FILTER ( ALL ( DATES ), DATES[Date] <= MAX ( DATES[Date] ) )
)

Clear all slicers and timelines and look at your Data:

 

USERELATIONSHIP.png

 

Then Select YEAR => 2016

Feb 2016 disappears in the second table (sorted by TERM_DATE) because it is filtered based on HIRE_DATE!

So when you filter remember you are filtering what matches in the HIRE_DATE not TERM_DATE!

However as you can see your Measures calculate the correct results.
So in the Matrix and the Chart the results are correct because we address this by using USERELATIONSHIP!

 

USERELATIONSHIP 2.png

 

Hope this helps! Smiley Happy

View solution in original post

7 REPLIES 7
Sean
Community Champion
Community Champion

@Lucy64 You know I really didn't want to mention this in the previous post but now I have to...

 

Why do you have TERMDATE table? Your relationships should be setup like in the original file you posted!

 

Sample4.png

 

Then change your leaving Measure like this...

 

Leaving = 
    CALCULATE (
        COUNT ( PERSONAL[TERM_DATE] ),
        USERELATIONSHIP ( 'DATES'[Date], PERSONAL[TERM_DATE] ),
        ALL ( PERSONAL[TERM_DATE] )
    )

I think that's all I changed. Let me know if this works!

 

Sample5.png

 

Good Luck! Smiley Happy

 

EDIT: You can read more about this here

http://exceleratorbi.com.au/multiple-relationships-between-tables-in-dax/

And I believe I also implemented something similar here...

http://community.powerbi.com/t5/Desktop/Cumulative-backlog-to-date/m-p/35845#M12981

Hi Sean, I added the second table for the Termination data as I was getting incorrect figures when I was taking the data from one table.  I will look at what you sent and see if this method works. Thanks.

Sean
Community Champion
Community Champion

Did you setup the second inactive relationship as it was in the sample file?

 

Drag TERM_DATE from Personal Table to DATE from DATES table - and you'll see the inactive relationship will be created.

 

Then change the Leaving Measure as above.

 

Let me know if you have any questions.

 

Also this link explains USERELATIONSHIP very well

http://exceleratorbi.com.au/multiple-relationships-between-tables-in-dax/

 

Sorry, not sure what is happening but I could not get this to work linking both Hire & Termination dates from one table to the same date table.  The results are not correct, it shows ´people who left against the month they were hired, which is not what I want.Leaving.PNG

Sean
Community Champion
Community Champion

@Lucy64

Remember with this setup your DATE table has only 1 Active Relationship with the PERSONAL table based on the HIRE_DATE!
Therefore when you filter/slice the data ONLY what matches your selection in the HIRE_DATE column will be considered!
So anytime you use the Year or Date Slicer or Timeline to limit the dates

it will filter your table ONLY considering what matches your selection in the HIRE_DATE and not the TERM_DATE!

 

However note that your Measures will produce the Correct Result because you use the USERELATIONSHIP function.

 

I created these Measures and got the same exact results as before:

 

Number Hired = COUNTA(PERSONAL[HIRE_DATE])

Number Fired =
CALCULATE (
    COUNTA ( PERSONAL[TERM_DATE] ),
    USERELATIONSHIP ( DATES[Date], PERSONAL[TERM_DATE] )
)

Balance = [Number Hired] - [Number Fired]

Number of Current Employess =
CALCULATE (
    [Balance],
    FILTER ( ALL ( DATES ), DATES[Date] <= MAX ( DATES[Date] ) )
)

Clear all slicers and timelines and look at your Data:

 

USERELATIONSHIP.png

 

Then Select YEAR => 2016

Feb 2016 disappears in the second table (sorted by TERM_DATE) because it is filtered based on HIRE_DATE!

So when you filter remember you are filtering what matches in the HIRE_DATE not TERM_DATE!

However as you can see your Measures calculate the correct results.
So in the Matrix and the Chart the results are correct because we address this by using USERELATIONSHIP!

 

USERELATIONSHIP 2.png

 

Hope this helps! Smiley Happy

Hi Sean,  Thanks for the explanation, I know now what I was doing wrong, I was using the TERM_DATE field in the chart instead of the measure which was created.  Thanks for your help, I learnt alot.

CheenuSing
Community Champion
Community Champion

@Lucy64

 

Please share data model, expressions used and sample data to find a solution

 

Cheers

 

CheenuSing

Did I answer your question? Mark my post as a solution and also give KUDOS !

Proud to be a Datanaut!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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