Forum Discussion
Count active employers
Hi I am trying to count active employers aggregated per month, I have a start date and a end date and a date 9999-12-31 if its still active or without end date. The start date can be greater than the actul day.
My code count employers that have a start date after this day
If you're trying to ignore people with a start date in the future you could add another clause to the FILTER
Aktiva personer = CALCULATE ( DISTINCTCOUNT ( 'analytics employment_periods'[sk_employment] ), FILTER ( 'analytics employment_periods', 'analytics employment_periods'[Period_start_datum] >= MIN ( 'Kalender'[Date] ) && 'analytics employment_periods'[Period_start_datum] <= TODAY () && ( 'analytics employment_periods'[Period_slut_datum] <= MAX ( 'Kalender'[Date] ) || 'analytics employment_periods'[Period_slut_datum] = DATE ( 9999, 12, 31 ) ) ) )
9 Replies
- MasonMASuper User
Hi, your logic is employees are active if their start date is before or equal to the end of the period you’re looking at,
and their end date is either missing / 9999-12-31, or after the start of the period. so try this instead,
CALCULATE(DISTINCTCOUNT('analytics employment_periods'[sk_employment]),FILTER('analytics employment_periods','analytics employment_periods'[Period_start_datum] <= MAX('Kalender'[Date]) &&('analytics employment_periods'[Period_slut_datum] >= MIN('Kalender'[Date]) ||'analytics employment_periods'[Period_slut_datum] = DATE(9999, 12, 31))))- FBergamaschiSuper User
I just suggest an optmization of MasonMA great code
If the table you have is huge, the below can improve performance
CALCULATE(DISTINCTCOUNT('analytics employment_periods'[sk_employment]),FILTER(SUMMARIZE('analytics employment_periods',employment_periods'[Period_start_datum],employment_periods'[Period_slut_datum]),'analytics employment_periods'[Period_start_datum] <= MAX('Kalender'[Date]) &&('analytics employment_periods'[Period_slut_datum] >= MIN('Kalender'[Date]) ||'analytics employment_periods'[Period_slut_datum] = DATE(9999, 12, 31))))If this helped, please consider giving kudos and mark as a solution
@me in replies or I'll lose your threadWant to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page
Consider voting this Power BI idea
Francesco Bergamaschi
MBA, M.Eng, M.Econ, Professor of BI
- ElinGFrequent Visitor
Thanx but it stil the same problem here is a example of people that conut that shouldn't
They have a start date for example 2026-01 and so on
- v-saisrao-msftCommunity Support
Hi ElinG,
I reproduced the issue using the sample data and obtained the following output. I have attached the PBIX file for your reference.
Aktiva personer = CALCULATE( DISTINCTCOUNT('employment_periods'[sk_employment]), FILTER( 'employment_periods', 'employment_periods'[Period_start_datum] <= MAX('Kalender'[Date]) && ( 'employment_periods'[Period_slut_datum] >= MIN('Kalender'[Date]) || 'employment_periods'[Period_slut_datum] = DATE(9999,12,31) ) ) )Hope this helps
Thank you.
- johnt75Super User
If you're trying to ignore people with a start date in the future you could add another clause to the FILTER
Aktiva personer = CALCULATE ( DISTINCTCOUNT ( 'analytics employment_periods'[sk_employment] ), FILTER ( 'analytics employment_periods', 'analytics employment_periods'[Period_start_datum] >= MIN ( 'Kalender'[Date] ) && 'analytics employment_periods'[Period_start_datum] <= TODAY () && ( 'analytics employment_periods'[Period_slut_datum] <= MAX ( 'Kalender'[Date] ) || 'analytics employment_periods'[Period_slut_datum] = DATE ( 9999, 12, 31 ) ) ) )- ElinGFrequent Visitor
Tnx for a really good solution
- ElinGFrequent Visitor
Hi agian!
My calculation of this is very slow I have only 2 years right now and more will it be is there any solutions thayt will move up the speed for the calculations, becouase right now I can only have years in the filter if I ad month there will be an error
Anställd =CALCULATE(DISTINCTCOUNT('Anställda'[Individ_kundnummer]),FILTER('Anställingsperiod','Anställingsperiod'[Period_start_datum] <= MAX('Kalender (2024–idag)'[Date]) &&('Anställingsperiod'[Period_slut_datum] >= MIN('Kalender (2024–idag)'[Date]) ||'Anställingsperiod'[Period_slut_datum] = DATE(9999, 12, 31))))- v-saisrao-msftCommunity Support
Hi ElinG,
To enhance performance when counting active employees, use Tertas() instead of the row-by-row Filter() in Calculate(). Tertas() efficiently applies filters from the 'Kalender' table to 'Anställingsperiod' without needing a physical relationship, allowing for month-level analysis. This approach aligns with Microsoft’s recommendation to use Tertas instead of Filter() for improved efficiency.
Avoid using FILTER as a filter argument in DAX - DAX | Microsoft Learn
Filter functions (DAX) - DAX | Microsoft Learn
TREATAS function - DAX | Microsoft Learn
Thank you.
- v-saisrao-msftCommunity Support
Hi ElinG,
Have you had a chance to review the solution we shared earlier? If the issue persists, feel free to reply so we can help further.
Thank you.