The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredCompete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.
Hi Power BI community
I have a question. Is it possible to check if an event happens two days in a row.
I want to make a meassure that counts how many times an error happens two days in a row.
I want to look at a table with
registration [Date]: Diffrent dates
registration [user]: an id that identify the user
registration [sick]: 1 or 0 depending on whether or not the user has been sick that day.
Each time a user is sick two or more days in a row I want the measure to be one higher. Is this possible?
All help is greatly appreciated.
Solved! Go to Solution.
Hello @ThomasWeppler
Could you please try this
Sick Days in a Row (Distinct) =
VAR CurrentDate = SELECTEDVALUE('registration'[Date])
VAR CurrentUser = SELECTEDVALUE('registration'[user])
VAR PreviousDay =
CALCULATE(
MAX('registration'[Date]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] < CurrentDate
)
)
VAR SickToday =
CALCULATE(
MAX('registration'[sick]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] = CurrentDate
)
)
VAR SickYesterday =
CALCULATE(
MAX('registration'[sick]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] = PreviousDay
)
)
VAR IsDuplicate =
CALCULATE(
COUNTROWS('registration'),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] = CurrentDate
)
) > 1
RETURN
IF(
SickToday = 1 && SickYesterday = 1 && NOT IsDuplicate,
1,
IF(
SickToday = 1 && SickYesterday = 1 && IsDuplicate,
1 / IsDuplicate,
0
)
)
Thanks,
Pankaj Namekar | LinkedIn
If this solution helps, please accept it and give a kudos (Like), it would be greatly appreciated.
Hello @ThomasWeppler
Sick Days in a Row =
VAR CurrentDate = SELECTEDVALUE('registration'[Date])
VAR CurrentUser = SELECTEDVALUE('registration'[user])
VAR PreviousDay =
CALCULATE(
MAX('registration'[Date]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] < CurrentDate
)
)
VAR SickToday =
CALCULATE(
MAX('registration'[sick]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] = CurrentDate
)
)
VAR SickYesterday =
CALCULATE(
MAX('registration'[sick]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] = PreviousDay
)
)
RETURN
IF(
SickToday = 1 && SickYesterday = 1,
1,
0
)
Thanks,
Pankaj Namekar | LinkedIn
If this solution helps, please accept it and give a kudos (Like), it would be greatly appreciated.
Thanks @pankajnamekar25
Your answer got me very close to a solution. However I still have one problem. A person can registre sick multiple times in a day and than they are counted twice when I add it all togther with sumx. I cannot just divided by two becuase sometimes they only regsitre once a day. Is there a way where I can make sure that I only count each day once?
I can be with another measure if that is easier than change the former dax code.
I already use this code to add the days togther.
Hello @ThomasWeppler
Could you please try this
Sick Days in a Row (Distinct) =
VAR CurrentDate = SELECTEDVALUE('registration'[Date])
VAR CurrentUser = SELECTEDVALUE('registration'[user])
VAR PreviousDay =
CALCULATE(
MAX('registration'[Date]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] < CurrentDate
)
)
VAR SickToday =
CALCULATE(
MAX('registration'[sick]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] = CurrentDate
)
)
VAR SickYesterday =
CALCULATE(
MAX('registration'[sick]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] = PreviousDay
)
)
VAR IsDuplicate =
CALCULATE(
COUNTROWS('registration'),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] = CurrentDate
)
) > 1
RETURN
IF(
SickToday = 1 && SickYesterday = 1 && NOT IsDuplicate,
1,
IF(
SickToday = 1 && SickYesterday = 1 && IsDuplicate,
1 / IsDuplicate,
0
)
)
Thanks,
Pankaj Namekar | LinkedIn
If this solution helps, please accept it and give a kudos (Like), it would be greatly appreciated.
@pankajnamekar25
I had to change the code a tiny amount to.
Sick Days in a Row (Distinct) =
VAR CurrentDate = SELECTEDVALUE('registration'[Date])
VAR CurrentUser = SELECTEDVALUE('registration'[user])
VAR PreviousDay =
CALCULATE(
MAX('registration'[Date]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] < CurrentDate
)
)
VAR SickToday =
CALCULATE(
MAX('registration'[sick]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] = CurrentDate
)
)
VAR SickYesterday =
CALCULATE(
MAX('registration'[sick]),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] = PreviousDay
)
)
VAR IsDuplicate =
CALCULATE(
COUNTROWS('registration'),
FILTER(
ALL('registration'),
'registration'[user] = CurrentUser &&
'registration'[Date] = CurrentDate
)
) > 1
RETURN
IF(
SickToday = 1 && SickYesterday = 1 && NOT IsDuplicate,
1,
IF(
SickToday = 1 && SickYesterday = 1 && IsDuplicate,
1 / (IsDuplicate+1),
0
)
)
But it was pretty close so I still accepted your solution.
Thanks a ton for the help. 🙂
Thanks for the quick answer again@pankajnamekar25
Unfortunately it didn't work.
the sumx has been changed to