All,
An excellent DAX Challenge by my friend, Chandeep Chhabra.
Is this what I should have been doing at 3 AM? Perhaps not – but when a clever DAX challenge rings the bell, I answer the door.
I went to post my solution on his blog, and saw that it already been solved by another Enterprise DNAer. I’ll give you one guess who…
My solution:
Summary
I’m not a big fan of calculated columns, but this one works more cleanly as a calculated column than a measure, so I did that here. In the real world I would just use Power Query.
No. of Stints =
VAR CurrRole = Data[Role]
VAR CurrDate = Data[Date]
VAR CurrEmp = Data[EmpCode]
VAR PrevDate =
CALCULATE (
MAX ( Data[Date] ),
FILTER (
ALL ( Data ),
Data[EmpCode] = CurrEmp
&& Data[Date] < CurrDate
)
)
VAR PrevRole =
CALCULATE (
MAX ( Data[Role] ),
FILTER (
ALL ( Data ),
Data[Date] = PrevDate
&& Data[EmpCode] = CurrEmp
)
)
VAR Stints =
SWITCH (
TRUE (),
PrevDate
= BLANK (), 1,
Data[Role] <> PrevRole, 1,
0
)
RETURN
Stints
Enjoy!
- Brian
P.S. he specifies clearly that you are to use DAX, probably because Power Query would make quick work of this using the “Feldmann Shift”.