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.
Hi everyone,
I have a table organized by Date + ID and I would like to get in a 3rd column a count of consecutive days based on these 2 variables:
Date | ID | ConsecutiveDaysNum |
1/1/2020 | 52 | 1 |
1/2/2020 | 52 | 2 |
1/3/2020 | 52 | 3 |
1/4/2020 | 52 | 4 |
1/8/2020 | 52 | 1 |
1/9/2020 | 52 | 2 |
1/1/2020 | 789 | 1 |
1/2/2020 | 789 | 2 |
1/3/2020 | 789 | 3 |
1/15/2020 | 789 | 1 |
Is there a DAX code for this? Any help will be really appreciated, thanks in advance!
Michelle
Solved! Go to Solution.
someone is bound to have a more elegant solution, but heres mine.
all calculated columns
isConsecutiveForward =
var NextEntryDate = CALCULATE(min(TableX[Date]),
filter( ALLEXCEPT(TableX,TableX[ID]),
TableX[Date] >EARLIER(TableX[Date])))
return if(NextEntryDate - TableX[Date]=1,TRUE(),FALSE())
isConsequtiveBackward =
var PreviousEntryDate = CALCULATE(max(TableX[Date]),
filter(ALLEXCEPT(TableX,TableX[ID]),
TableX[Date] <EARLIER(TableX[Date])))
return if( TableX[Date]-PreviousEntryDate=1,TRUE(),FALSE())
ConsDays=
var StartOfSeq=CALCULATE(MAX(TableX[Date]),
FILTER(ALLEXCEPT(TableX,TableX[ID]),
[isConsecutiveForward] &&
not([isConsecutiveBackward])&&
TableX[Date] < EARLIER(TableX[Date])))
var Seq=CALCULATE(COUNTROWS(TableX),
FILTER(ALLEXCEPT(TableX,TableX[ID]),
TableX[Date]<=EARLIER(TableX[Date])&&
TableX[Date] >=StartOfSeq))
return SWITCH(TRUE(),
NOT([isConsecutiveBackward]||[isConsecutiveForward]),1,
[isConsecutiveForward]&& not([isConsecutiveBackward]),1,
Seq)
It worked like a charm! Thanks rfigtree!
someone is bound to have a more elegant solution, but heres mine.
all calculated columns
isConsecutiveForward =
var NextEntryDate = CALCULATE(min(TableX[Date]),
filter( ALLEXCEPT(TableX,TableX[ID]),
TableX[Date] >EARLIER(TableX[Date])))
return if(NextEntryDate - TableX[Date]=1,TRUE(),FALSE())
isConsequtiveBackward =
var PreviousEntryDate = CALCULATE(max(TableX[Date]),
filter(ALLEXCEPT(TableX,TableX[ID]),
TableX[Date] <EARLIER(TableX[Date])))
return if( TableX[Date]-PreviousEntryDate=1,TRUE(),FALSE())
ConsDays=
var StartOfSeq=CALCULATE(MAX(TableX[Date]),
FILTER(ALLEXCEPT(TableX,TableX[ID]),
[isConsecutiveForward] &&
not([isConsecutiveBackward])&&
TableX[Date] < EARLIER(TableX[Date])))
var Seq=CALCULATE(COUNTROWS(TableX),
FILTER(ALLEXCEPT(TableX,TableX[ID]),
TableX[Date]<=EARLIER(TableX[Date])&&
TableX[Date] >=StartOfSeq))
return SWITCH(TRUE(),
NOT([isConsecutiveBackward]||[isConsecutiveForward]),1,
[isConsecutiveForward]&& not([isConsecutiveBackward]),1,
Seq)
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
42 | |
21 | |
21 | |
14 | |
13 |
User | Count |
---|---|
43 | |
39 | |
33 | |
18 | |
17 |