Forum Discussion
[Seek help] How to calculate the value difference on two dates?
Hi Expert,
I have a table like below which shows the actice workers as of two dates.
I would like to create a measure to calculate the difference of headcount between these two dates.
| Effective Date | Employee |
| 1/1/2019 | A |
| 1/1/2019 | B |
| 1/1/2019 | C |
| 1/1/2019 | D |
| 1/1/2019 | E |
| 3/18/2019 | A |
| 3/18/2019 | B |
| 3/18/2019 | C |
| 3/18/2019 | D |
| 3/18/2019 | E |
| 3/18/2019 | F |
For example,
On 1/1/2019, the headcount is 5.
On 3/18/2019, the headcount is 6.
Therefore, the [change of headcount] is 6 -5 =1.
Is it possible to achieve that by using DAX?
Many thanks!
Best regards,
Tom
Hi TomLU123
Try this measure in a card visual. You can change the dates of interest in the code. This could also be done through slicers.
Measure = VAR _Date1 = DATE ( 2019, 01, 01 ) VAR _Date2 = DATE ( 2019, 03, 18 ) VAR _HeadCountDate1 = CALCULATE ( DISTINCTCOUNT ( Table1[Employee] ), Table1[Effective Date] = _Date1 ) VAR _HeadCountDate2 = CALCULATE ( DISTINCTCOUNT ( Table1[Employee] ), Table1[Effective Date] = _Date2 ) RETURN _HeadCountDate2 - _HeadCountDate1Basically what you're saying, if I'm not mistaken, is that you want the earliest and latest dates selected in the slicers, so just taking the MIN and MAX should suffice. Note it is also necessary to update the filter argument in the CALCULATEs; the logic is still the same but MIN and MAX are not allowed in the simplified syntax so we have t use FILTER. Please consider kudoing the posts if they are of help. Table1[Effective Date] is what you'd need on the slicer.
Cheers
Measure_v2 = VAR _Date1 = MIN ( Table1[Effective Date] ) VAR _Date2 = MAX ( Table1[Effective Date] ) VAR _HeadCountDate1 = CALCULATE ( DISTINCTCOUNT ( Table1[Employee] ), FILTER ( ALL ( Table1[Effective Date] ), Table1[Effective Date] = _Date1 ) ) VAR _HeadCountDate2 = CALCULATE ( DISTINCTCOUNT ( Table1[Employee] ), FILTER ( ALL ( Table1[Effective Date] ), Table1[Effective Date] = _Date2 ) ) RETURN _HeadCountDate2 - _HeadCountDate1
6 Replies
- AlB
Community Champion
Hi TomLU123
Try this measure in a card visual. You can change the dates of interest in the code. This could also be done through slicers.
Measure = VAR _Date1 = DATE ( 2019, 01, 01 ) VAR _Date2 = DATE ( 2019, 03, 18 ) VAR _HeadCountDate1 = CALCULATE ( DISTINCTCOUNT ( Table1[Employee] ), Table1[Effective Date] = _Date1 ) VAR _HeadCountDate2 = CALCULATE ( DISTINCTCOUNT ( Table1[Employee] ), Table1[Effective Date] = _Date2 ) RETURN _HeadCountDate2 - _HeadCountDate1- TomLU123
Helper III
AlB Thanks for your help!
A further question about "This could also be done through slicers."
If now there are more than 2 dates and the user can use a slicer to choose the time range they want to see.
How should I modify the Date Varibles to let the report show the change of headcount between the latest effectice date the user chose and previous 1 effectve date?
Example as below:
Effective Date Employee 1/1/2019 A 1/1/2019 B 1/1/2019 C 1/1/2019 D 1/1/2019 E 3/18/2019 A 3/18/2019 B 3/18/2019 C 3/18/2019 D 3/18/2019 E 3/18/2019 F 3/25/2019 A 3/25/2019 B 3/25/2019 C 3/25/2019 D 3/25/2019 E 3/25/2019 F 3/25/2019 G 3/25/2019 H For example,
On 1/1/2019, the headcount is 5.
On 3/18/2019, the headcount is 6.
On 3/25/2019, the headcount is 8
If the user choose 1/1/2019 to 3/18/2019, the [change of headcount] is 6-5=1
If the user choose 3/18/2019 to 3/25/2019, the [change o f headcount] is 8-6=2
If the user choose 1/1/2019 to 3/25/2019, the [change of headcount] is 8-6=2
Is it possible to achieve that by using DAX?
Many thanks!
Best regards,Tom
- AlB
Community Champion
Basically what you're saying, if I'm not mistaken, is that you want the earliest and latest dates selected in the slicers, so just taking the MIN and MAX should suffice. Note it is also necessary to update the filter argument in the CALCULATEs; the logic is still the same but MIN and MAX are not allowed in the simplified syntax so we have t use FILTER. Please consider kudoing the posts if they are of help. Table1[Effective Date] is what you'd need on the slicer.
Cheers
Measure_v2 = VAR _Date1 = MIN ( Table1[Effective Date] ) VAR _Date2 = MAX ( Table1[Effective Date] ) VAR _HeadCountDate1 = CALCULATE ( DISTINCTCOUNT ( Table1[Employee] ), FILTER ( ALL ( Table1[Effective Date] ), Table1[Effective Date] = _Date1 ) ) VAR _HeadCountDate2 = CALCULATE ( DISTINCTCOUNT ( Table1[Employee] ), FILTER ( ALL ( Table1[Effective Date] ), Table1[Effective Date] = _Date2 ) ) RETURN _HeadCountDate2 - _HeadCountDate1