Count/Disctinct in function of datediff - powerbi

I need to count the number of residents who have just arrived
in a residence and have been check-up within 31 days.
I have a table with 4 columns which contains the id_resident, the code_event (arrival or weighing)
each line corresponds to an event
ID_RESIDENT
CODE_EVENT
DATE_EVENT
A
ARRIVAL
2022-01-01
A
ASSESSMENT
2022-01-02
B
ASSESSMENT
2022-01-02
C
ARRIVAL
2022-01-01
[D
ASSESSMENT
2022-05-05
I waited 1 count resident in this case .
assess_arrival = CALCULATE(DISTINCTCOUNT('TABLE'[ID_RESIDENT]),
FILTER(MEDICSGR_NORM, ( DATEDIFF( CALCULATE((min(TABLE[DATE_EVENT])),FILTER(
TABLE, TABLE[CODE_EVENT_GR] = "ASSESSMENT"
)), CALCULATE((min(TABLE[DATE_EVENT])),FILTER(
TABLE, TABLE[CODE_EVENT_GR] = "ARRIVAL"
)),DAY) <= 31)))
but the result is complety wrong
Thanks so much for your help

Related

How to calculate the signal period counter

I am reading a signal value every few seconds.Plotting the signal results in a clipped fullwave rectified sine wave.I am trying to write a measure, such that everytime the signal completes a period, I should update the counter by 1.How can I do it?
This is how the table looks. This table shows the completion of 1 period.
DeviceID
TesterID
Value
DateTime
1
22
0
01.10.2022 00:00:00
1
22
500
01.10.2022 00:01:00
1
22
500
01.10.2022 00:01:15
1
22
1000
01.10.2022 00:02:00
1
22
1000
01.10.2022 00:02:15
1
22
1000
01.10.2022 00:02:30
1
22
500
01.10.2022 00:02:40
1
22
500
01.10.2022 00:02:42
1
22
0
01.10.2022 00:02:45
This is how the plot of the signal looks like.For the above image, measure should calculate the counter as 3.
I wrote a measure in DAX, to increase the count by 1, if the signal value goes above 500.But this did not work since I am sampling the data quite fast which results in reading the same data again(See the column Value for row number 4,5 and 6).
Counter = CALCULATE (COUNTROWS (tableName), FILTER (tableName, tableName[Value]>500))
Another way would be to determine the slope of the signal i.e to find that the Value column has crossed 500 in upward slope and returned to 500 again in the downward slope.Can I calculate slope like this?
I would identify period start with a column as follows:
Period Start =
VAR cursor = tbl[Value]
VAR val = CALCULATE(SUM(tbl[Value]),OFFSET(1,SUMMARIZE(tbl, tbl[DateTime], tbl[Value]),ORDERBY(tbl[DateTime])), ALLEXCEPT(tbl, tbl[DateTime]))
RETURN
if(cursor ==0 && val>0, 1)
I would then have a running total showing which period each row falls into.
Period =
VAR cursor = tbl[DateTime]
RETURN
CALCULATE(SUM(tbl[Period Start]), tbl[DateTime] <= cursor, REMOVEFILTERS() )
If have a huge table then I'd add 2 calculated column. The reason is that some calculations would be done before data manipulating.
col 1 :
id =
RANKX(
CALCULATETABLE(
tbl
,ALLEXCEPT(tbl,tbl[DeviceID],tbl[TesterID])
)
,[DateTime]
,[DateTime]
,ASC
,Skip
)
col2 :
col =
LOOKUPVALUE(
tbl[Value]
,tbl[id]
,[id]-1
,tbl[TesterID]
,[TesterID]
,tbl[DeviceID]
,[DeviceID]
)
+[Value]
The second column will have 2 values of 1500 for each full wave within DeviceID-TesterID.
So, you can try then following trick:
Sumx(
ADDCOLUMNS(
SUMMARIZE(
tbl
,tbl[TesterID]
,tbl[DeviceID]
)
,"q"
,COUNTROWS(
Calculatetable(
tbl
,tbl[col]=1500
)
)
)
,INT([q]/2)
)
As a result it should be a number of full waves... I believe )

Calculate max value from Mon-Sun for every week

I have a table where I have to find the maximum weekly quantity for the data based on the date column in the past few years. In this example, say 06/07 is Monday and 06/12 is Sunday. Then in that week, the quantity 12 is the MAX value.
Quantity
Date
Time
1
2022-06-07
04:39:57.090
2
2022-06-07
04:39:58.850
3
2022-06-08
04:37:35.900
4
2022-06-08
04:37:37.247
5
2022-06-09
04:37:56.833
6
2022-06-09
04:37:58.190
7
2022-06-10
04:37:34.757
8
2022-06-10
04:37:36.103
9
2022-06-11
04:38:18.733
10
2022-06-11
04:38:20.100
11
2022-06-12
04:38:30.377
12
2022-06-12
04:38:31.833
13
2022-06-13
04:38:30.377
14
2022-06-14
04:38:31.833
I am trying to write a DAX script for this, but failing to do it because I am filtering on MAX date instead of value. Any help on how to modify this?
CALCULATE(
MAX( 'stock_xml'[Quantity] ),
FILTER( 'stock_xml',
'stock_xml'[Date] = MAX( 'stock_xml'[Date] )
)
)
RETURN
Stock
Can you please try this below Measure script-
weekly_max =
CALCULATE(
MAX('your_table_name'[Quantity]),
FILTER(
ALL('your_table_name'),
WEEKNUM('your_table_name'[Date],1) = WEEKNUM(MAX('your_table_name'[Date]),1)
)
)
Sample output-

Power BI - Get date when sum is 0

I have two tables - Customer and Transaction. The transaction holds invoices and payments. I need to get the date when the transaction sum is 0 or grater.
So the expected result should be John 02-20-2021. How can I achieve this?
customerID
Name
1
John
2
Ben
customerID
value
date
1
-150
02-13-2021
1
100
02-14-2021
1
200
02-20-2021
1
10
02-23-2021
You can try this out, by adding some new measures.
First measure to calculate the account balance over dates, returning a value only for positive balance:
Account Balance :=
VAR _date = MAX ( Transaction[date] )
VAR _balance =
CALCULATE (
SUM ( Transaction[value] ) ,
Transaction[date] <= _date
)
RETURN
IF ( _balance >= 0 , _balance )
The second filters the original table based on this measure and returns the earliest date:
Break Even Date :=
MINX (
FILTER (
VALUES ( Transaction[date] ),
[Account Balance]
),
[date]
)
Result:
Do note that I have not tested this beyond your (simple) example.

Calculated Column with Current Row Values and Previous Dates in Power BI (DAX)

I'm trying to get a calculated column (not a measure) that gets the sum of a column based on the values in the current row of that table for dates that are 1 month lagged to the date on the current row. My table has dates that are the 1st day of every month only .. no other days in the month. I'm asking the question about DAX; however, I have no problem implementing in M Language in Power Query (actually would probably prefer) if there is a solution that way as well.
I have been able to get a measure to work using something like this..
CALCULATE(SUM(AMT), DATEADD(DATECOLUMN, -1, MONTH))
But I'd like to be a new column instead.
Assuming the table looks something like this..
A B C D AMT
6 BAC456 5/1/2019 TEST 25
2 EPS123 4/1/2019 TEST 45
2 EPS123 3/1/2019 TEST 65
6 BAC456 4/1/2019 TEST 43
6 BAC456 4/1/2019 TEST 88
7 GRE123 4/1/2019 TEST 90
9 BAC456 4/1/2019 TEST 43
I'd like to have another column in this table where the first row would be:
A B C D AMT NEWCOL
6 BAC456 5/1/2019 TEST 25 131
Second row would be:
A B C D AMT NEWCOL
2 EPS123 4/1/2019 TEST 45 65
etc..
In cases where the month column is the first month in the entire table NEWCOL would be 0
To get 131, I'm assuming that you are requiring a match on both columns A and B.
NewCol =
CALCULATE (
SUM ( Table1[AMT] ),
ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
PREVIOUSMONTH ( Table1[C] )
)
This sums the column AMT keeping the row context of columns A and B and specifies the previous month as a filter on C. Note that this returns a blank for rows that don't have a previous month. If you'd prefer 0 then add + 0 after the last closing parenthesis.
If PREVIOUSMONTH doesn't work, then try this:
NewCol =
CALCULATE (
SUM ( Table1[AMT] ),
ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
Table1[C] = EOMONTH ( EARLIER( Table1[C] ), -2 ) + 1
)
For date = 5/1/2019, EOMONTH ( date, -2 ) returns 3/31/2019. Add one day to get 4/1/2019.
To achieve this easily in a calculated column, you need something like this before writing the final DAX.
Month Num = MONTH(MyTable[C])
Month Diff = DATEDIFF(MyTable[C],MAX(MyTable[C]),MONTH)
And now you have these Month differences and Month numbers defined, you can write a DAX like this -
Amount - New Column =
Var selectedValue_B = MyTable[B]
Var SelectedValue_MonthDiff = MyTable[Month Diff]
Var out1 = CALCULATE(SUM(MyTable[AMT]), FILTER(ALL(MyTable), MyTable[Month Diff] = SelectedValue_MonthDiff+1 && MyTable[B] = selectedValue_B)) + 0
return out1
This makes my table to look something like,
I have used Var(Variables) in my formula to help you understand what is happening inside the formula.
Kindly accept the answer if it solves your problem.

Power Bi: Get Column Name with Maximum and Minimum other column value

I have a sheet containing employees leave data-
Staff Leave Taken Month
A 19 April
A 3 May
A 3 June
B 2 April
B 1 May
B 0 June
C 0 April
C 0 May
C 1 June
I want to calculate the Employee whose has taken maximum no. of leaves and name of employee whose has taken minimum no. of leaves.
Here Employee with max. leaves is A and with min. is C.
I am having trouble in getting the maximum no. of leaves.
X = MAX( SUMX ( SUMMARIZE ( Table1, Table1[STAFF], Table1[Leaves] ), [Leaves] ))
But it is showing some error.
I tried to group by staff name then also it is not working out.
You can first create a summary table with the following DAX:
Summary = SUMMARIZE(Table1, Table1[Staff], "Leaves", SUM(Table1[Leave Taken]))
Then you can use the following DAX measure to get the Max / Min name:
Max Name =
CALCULATE(
FIRSTNONBLANK('Summary'[Staff], 1),
FILTER(
Summary,
Summary[Leaves] = MAX(Summary[Leaves])
)
)
-
Min Name =
CALCULATE(
FIRSTNONBLANK('Summary'[Staff], 1),
FILTER(
Summary,
Summary[Leaves] = MIN(Summary[Leaves])
)
)