Count the repetition of values in last 5 days in DAX - powerbi

I have a Anti Virus scan dataset which gets generated on daily basis and goes many days back in the past, in this i have to calculate number of times a machine is appearing a Non-Compliant in last 5 days, Currently I am using below DAX formula to create calculated column
Repetition = CALCULATE(COUNT('SCCM Antivirus'[MachineName]),ALLEXCEPT('SCCM Antivirus','SCCM
Antivirus'[MachineName]),'SCCM Antivirus'[ComplianceStatus]= "Non-
Compliant",'SCCM Antivirus'[HRs]<= 120)
But, The problem is that every machine has multiple appids hence instead of calculating 1 count per day per machine, which should be maximum 5 for 5 last days, i get count(appid),
e.g. in the attached table, if i want to see how many times Machine 'A' has repeated in last two days 11/03 and 10/03 then it should give me a count of 2, and that is what my requirement is also, but instead i get count of 5, as Machine A has 2 Appid on 11/03 and 3 Appid on 10/03, and this is my problem, i Want distinct count of machine group by date for last 5 days only. Could some please help me.
enter image description here

Try DISTINCTCOUNT('SCCM Antivirus_RBF'[Date]) in stead of COUNT('SCCM Antivirus_RBF'[MachineName])
Something like this:
Repetition =
CALCULATE (
DISTINCTCOUNT ( 'SCCM Antivirus_RBF'[Date] ),
ALLEXCEPT ( 'SCCM Antivirus_RBF', 'SCCM Antivirus_RBF'[MachineName] ),
'SCCM Antivirus_RBF'[Status] = "Non-Compliant"
)

Related

What's DAX To extract employees ID's whose Shifts fall with in certain time slot

I have table contains data col's of EmpID, Shift Start Time, Shift End Time & Resonable Notice Time in case of closing business before business hours. I'm trying to filter Employees whose ShiftStartTime is less than 2 hours of Resonable Notice Time of Closing so they get compensation as per the policy. My syntax does not fit to filter those employees. Please advise.
I also tried calculating time difference by using DATEDIFF as
Step 1
Time Diff = DATEDIFF('Table1'[ShiftStartTime],'Table1'[Resonable Notice Time],MINUTE)
Step 2
Trying to filter only those EmpID's who has less than 120 mins and >= 0 as they many not have had chance to clock in. but not getting right syntax
EmpID's for Compensation = LOOKUPVALUE('Table1'[iEmpID],('Table1'[Time Diff]<120 &&'Table1'[Time Diff]>=0),BLANK()) Need help solving this.
I assume that 'Time_Diff' is a calculated column?
Time_Diff =
DATEDIFF ( 'Table1'[ShiftStartTime], 'Table1'[Resonable Notice Time], MINUTE )
Then I think you should use a table function (such as FILTER or summarize) instead of lookup value which returns a scalar value.
Please test this:
EmpID's for Compensation =
FILTER (
ALL ( 'Table1'[iEmpID], 'Table1'[Time Diff] ),
( 'Table1'[Time Diff] < 120
&& 'Table1'[Time Diff] >= 0 )
)

Calculate daily average including zero in Power BI

I have a dataset of patients visiting several categories(SPECIALISM) of a hospital. A visit lasts a couple of hours each day. Each row in my dataset represents an hour that they are present for a certain hospital specialism.
Input
I want to calculate for each hour of the day, the number of patients that are present on average, per specialism, I used the following code (measure):
daggem = AVERAGEX(values('Date'[Date]),[distinctpat])
with distinctpat being a distinct count of patient IDs
This gives me almost the desired result, but the tales of the graph are too heavy (it shows an average of 1 patient during the night, but this 1 patient was there only on 1 specific day, normally it is zero. But the average, as I calculated, it does not include all other nights when there were zero patients. So I would like to obtain an average that is much lower (and more correct)
Output
You probably need an extra table with all the days and times. Based on the reports, you will be able to find the hours without visits
Here is an example of how to create a Date table with hours; Add relationship and use in calculation.
DatesWithHours =
var Dates = CALENDAR("2021-01-01 00:00:00", "2021-01-03 00:00:00")
var DatesHour =
GENERATE(Dates, (ADDCOLUMNS(GENERATESERIES(1,12,1),"Hours", [Date] + TIME([Value],0,0))))
return
DatesHour
That's the problem with AVERAGEX, it ignores blanks. Try a simple division instead.
daggem =
DIVIDE (
COUNTROWS ( TargetTable ),
COUNTROWS ( Dates )
)

Is there a way to show the sum of a value within a certain time range (ex: 2 days)?

I am measuring the duration in minutes of various tools. The tool can run multiple times in the same day. I would like to flag a tool as "High Use" if the tool runs for more than 20 minutes in a 2 day period. This will be shown in matrix format.
For example, I want to flag Tool A as "High Use" but not flag Tool B
I'm not sure how to indicate a 2 day time period. Is this possible to do in DAX?
Date
Tool Name
Minutes
12/2
Tool A
10
12/2
Tool B
5
12/2
Tool C
7
12/3
Tool A
12
12/3
Tool B
6
Assuming the table name is ToolUsage and the Date column to be a DATETIME, it's possible to write a measure to compute the last two days usage for a single tool
Tool2LastDayUsage =
IF (
HASONEVALUE ( ToolUsage[Tool Name] ),
VAR MaxDate = MAX ( ToolUsage[Date] )
VAR LastTwoDays =
CALCULATETABLE (
VALUES(ToolUsage[Date]),
ToolUsage[Date] > MaxDate - 2
&& ToolUsage[Date] <= MaxDate
)
RETURN
CALCULATE (
SUM ( ToolUsage[Minutes] ),
LastTwoDays,
ALLEXCEPT (
ToolUsage,
ToolUsage[Tool Name]
)
)
)
First we check to have a single tool selected using HASONEVALUE, then we compute the last date in the current selection and we use it to prepare the LastTwoDays table containing the 2 days period. At last we compute the last two days usage by applying the LastTwoDays filter table, together with ALLEXCEPT to remove any existing filter over the ToolUsage table but the filter over the Tool Name
Then we can use this measure to build another measure to check the last two days period usage and flag high used tools with "High Use"
HighUse = IF( [Tool2LastDayUsage] > 20, "Hig Use" )
These measures can be used in a matrix or a table visuals. If the ToolUsage[Date] is included, they use it to compute the last two days period.
Of course, different behavior may be implemented; for instance to flag the tool regardless of the date on the current visual row, using the overall max date instead.
To show a 2 period inside a visual, it may be possible to create a table with a description, like 12/2-12/3 and a date, 12/3 in this case, to be set in relationship with the ToolUsage table.

Count Distinct ID if it has more than 7 periods

I am trying to counts students absent if they have skipped more than 8 bell periods on the same day. I was wondering if somebody could help me out here. I tried a distinct count with a counts of bell periods greater than 6 (that is the ballpark) but it is not working. I am providing a sample table below here.
You need to count the items, but using ALLEXECPT, to count the items by only using the filter context of StudentID and Date
So for example based on your data, assuming that student 101 missed 9 bells then:
Measure =
VAR _countofmisses = CALCULATE(COUNTAX(Table1, 1), ALLEXCEPT(Table1, Table1[Student ],Table1[Date]))
RETURN
IF(_countofmisses >=7, "Missed", "OK")
Which would give the following:
You can change the COUNTAX to a SUMX and still get the same result. All it is doing is counting/summing 1 for each row of the filter condition.
If I've read it wrong and student 101 has attended all 9 bells, just change the IF >= clause to the logic you need.

power bi RANKX returning 1 for all rows

I am trying to give rank to each row in daily stock price details table to figure out previous day closing price:
The code I use is:
rank =
RANKX(
FILTER(
ALL(NSE_DAILY_REPORT),
NSE_DAILY_REPORT[SYMBOL]="ADLABS"
),
MAX(NSE_DAILY_REPORT[TDATE]),,
ASC
)
The problem is that it returns a rank of 1 for all rows.
Try changing MAX(NSE_DAILY_REPORT[TDATE]) to NSE_DAILY_REPORT[SCLOSE]
The second argument expects an expression to compare to evaluations of that same expression in the filtered subset. Using MAX will yield that every record gets ranked in a set of just one record, hence the 1 for all rows.
So if I understand correctly your goal is to get the closing price of the day before?
In that case RANKX() is not necessary in contrast to the post you shared as an example. There they create ordinality by creating a ranking first and then perform a pretty inefficient calculation to get the previous in rank. There already is ordinality as you have a date column. Power BI already knows how to interpret that scale, so getting a value for a previous day does not need an additional ranking.
There's tons of posts around stack overflow dealing with this problem. Have a look around to learn more. For your particular problem, the solution will be a calculated column with code looking something like:
PreviousDay =
CALCULATE (
SUM ( NSE_DAILY_REPORT[SCLOSE] ),
FILTER (
ALLEXCEPT ( NSE_DAILY_REPORT, NSE_DAILY_REPORT[SYMBOL] ),
NSE_DAILY_REPORT[TDATE] = EARLIER(NSE_DAILY_REPORT[TDATE]) - 1
)
)
It will do the trick, but it still has some inefficiencies you can improve by looking through other examples like this