Forum Discussion
Display when the difference between two dates reaches a threshold for lines having a common value
Hi everyone đ
I have a database of companies and their number of employees.
I get updates about these companies that appear as a new line in the database.
Here is roughly what it looks like :
The date format is DD/MM/YY.
The list of companies is very long and I donât control when I get updates.
What I want to achieve is : Filtering only the companies whose number of employees rose above a given threshold (for instance 10%) between two dates.
In a barbaric code it would look like :
DISPLAY IF ( [Employees] of the same [Company] with [Update] after 01/10/22 ) / ( [Employees] of the same [Company] with [Update] before 01/02/22 ) > 1,10
Can you guys help me format a DAX command to do this ? Or at least advise functions you think would be the best suited.
Many thanks đ
- Anonymous3 years ago
After some tests, I decided to stick with the command below to detect the changes. It returns "sans donnée" to signal the companies for which there is no data after or before the dates of comparison.
Many thanks again to v-henryk-mstf ; you really did 99% of the heavy lifting and it helped a lot !
I wish a great day to everybody,
Augmentation_employee_count_entre_2_dates =
VAR seuil = 5 //augmentation en pourcentage, 20 signifie +20%
VAR date_after =
DATE ( 2022, 10, 01 ) //date Ă partir de laquelle on regarde le changement
VAR date_before =
DATE ( 2022, 09, 01 ) //date de référence
VAR after_ =
CALCULATE (
MAX ( 'Table'[employees_count] ),
FILTER (
ALL ( 'Table' ),
'Table'[company_name] = EARLIER ( 'Table'[company_name] )
&& 'Table'[updated_at] > date_after
)
)
VAR before_ =
CALCULATE (
MAX ( 'Table'[employees_count] ),
FILTER (
ALL ( 'Table' ),
'Table'[company_name] = EARLIER ( 'Table'[company_name] )
&& 'Table'[updated_at] < date_before
)
)
RETURN
IF (
after_ = 0,
"sans donnée",
IF (
before_ = 0,
"sans donnée",
IF ( after_ / before_ > ( 1 + ( seuil / 100 ) ), "oui", "non" )
)
)
4 Replies
- v-henryk-mstfCommunity Support
Hi Anonymous ,
You can try formula like below:
Result = VAR after_ = CALCULATE ( SUM ( 'Table'[employee] ), FILTER ( ALL ( 'Table' ), 'Table'[company] = EARLIER ( 'Table'[company] ) && 'Table'[update] > DATE ( 2022, 10, 01 ) ) ) VAR before_ = CALCULATE ( SUM ( 'Table'[employee] ), FILTER ( ALL ( 'Table' ), 'Table'[company] = EARLIER ( 'Table'[company] ) && 'Table'[update] < DATE ( 2022, 02, 01 ) ) ) RETURN IF ( after_ / before_ > 10, "YES", "No" )If the problem is still not resolved, please provide detailed error information and test data. Looking forward to your reply.
Best Regards,
Henry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. - AnonymousNot applicable
Many thanks, for your precious help v-henryk-mstf . It is greatly appreciated đ
I shared below, a small sample of the data. I added on the sample, the expected result.
I could create a new column âResultâ. It runs, no syntax error detected, but the result is quite different from expected :
- I have lines that are âYESâ, however there is only one line for the company so there canât be a comparison. For instance â2° Investing Initiativeâ.
- The few expected âYESâ are not identified. For instance â2Empowerâ.
- On the contrary, there are companies identified âYESâ while they shouldnât. For instance â73 Stringsâ.
Here is your script, adapted to my real database labels :
Result =
VAR after_ =
CALCULATE (
SUM ( 'Table'[employees_count]),
FILTER (
ALL ( 'Table' ),
'Table'[company_name] = EARLIER ( 'Table'[company_name] )
&& 'Table'[updated_at] > DATE ( 2022, 10, 01 )
)
)
VAR before_ =
CALCULATE (
SUM ( 'Table'[employees_count]),
FILTER (
ALL ( 'Table' ),
'Table'[company_name] = EARLIER ( 'Table'[company_name] )
&& 'Table'[updated_at] < DATE ( 2022, 05, 30 )
)
)
RETURN
IF ( after_ / before_ > 10, "YES", "No" )
Do you see where the error could originate ?
I noticed that the result doesn't change when the threshold in line 21 change.
Many thanks for your help,
------------------------------------------------------------------------------------------------------------------
Sample :
company_name employees_count updated_at expected result 2Empower 57 2022-08-21 07:56:16 YES 2Empower 85 2022-09-22 15:46:42 YES â€ïžPositive thought âïž Daily Good News 750 2022-06-30 09:03:33 YES â€ïžPositive thought âïž Daily Good News 845 2022-08-23 08:35:18 YES â€ïžPositive thought âïž Daily Good News 845 2022-08-23 11:17:16 YES 2° Investing Initiative 40 2022-10-04 10:59:20 No 73 Strings 45 2022-08-21 08:02:40 No 73 Strings 46 2022-08-28 08:01:52 No 73 Strings 46 2022-09-22 15:54:29 No 73 Strings 47 2022-10-04 10:45:28 No A l'Ă©coute d'opportunitĂ©s 21 2022-10-13 16:15:51 No . 136 2022-10-04 07:39:50 No . 135 2022-10-06 07:39:11 No Result:
company_name employees_count updated_at Result 2Empower 57 2022-08-21 07:56:16 No 2Empower 85 2022-09-22 15:46:42 No â€ïžPositive thought âïž Daily Good News 750 2022-06-30 09:03:33 No â€ïžPositive thought âïž Daily Good News 845 2022-08-23 08:35:18 No â€ïžPositive thought âïž Daily Good News 845 2022-08-23 11:17:16 No 2° Investing Initiative 40 2022-10-04 10:59:20 YES 73 Strings 45 2022-08-21 08:02:40 YES 73 Strings 46 2022-08-28 08:01:52 YES 73 Strings 46 2022-09-22 15:54:29 YES 73 Strings 47 2022-10-04 10:45:28 YES A l'Ă©coute d'opportunitĂ©s 21 2022-10-13 16:15:51 YES . 136 2022-10-04 07:39:50 YES . 135 2022-10-06 07:39:11 YES - AnonymousNot applicable
To complete my answer v-henryk-mstf , I brainstomed :
By using MAX and MIN instead fo SUM functions, like in the script below, I can compare the lowest and highest point easily.
However I lose the time dimension. The companies loosing employees are identified like the companies gaining employees. Do you see how I could filter them out ?
Result =
VAR after_ =
CALCULATE (
MAX ( 'Table'[employees_count]),
FILTER (
ALL ( 'Table' ),
'Table'[company_name] = EARLIER ( 'Table'[company_name] )
)
)
VAR before_ =
CALCULATE (
MIN ( 'Table'[employees_count]),
FILTER (
ALL ( 'Table' ),
'Table'[company_name] = EARLIER ( 'Table'[company_name] )
)
)
RETURN
IF (after_/before_ > 1.10 , "YES" , "No")
- AnonymousNot applicable
After some tests, I decided to stick with the command below to detect the changes. It returns "sans donnée" to signal the companies for which there is no data after or before the dates of comparison.
Many thanks again to v-henryk-mstf ; you really did 99% of the heavy lifting and it helped a lot !
I wish a great day to everybody,
Augmentation_employee_count_entre_2_dates =
VAR seuil = 5 //augmentation en pourcentage, 20 signifie +20%
VAR date_after =
DATE ( 2022, 10, 01 ) //date Ă partir de laquelle on regarde le changement
VAR date_before =
DATE ( 2022, 09, 01 ) //date de référence
VAR after_ =
CALCULATE (
MAX ( 'Table'[employees_count] ),
FILTER (
ALL ( 'Table' ),
'Table'[company_name] = EARLIER ( 'Table'[company_name] )
&& 'Table'[updated_at] > date_after
)
)
VAR before_ =
CALCULATE (
MAX ( 'Table'[employees_count] ),
FILTER (
ALL ( 'Table' ),
'Table'[company_name] = EARLIER ( 'Table'[company_name] )
&& 'Table'[updated_at] < date_before
)
)
RETURN
IF (
after_ = 0,
"sans donnée",
IF (
before_ = 0,
"sans donnée",
IF ( after_ / before_ > ( 1 + ( seuil / 100 ) ), "oui", "non" )
)
)