How to calculate the signal period counter - powerbi

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 )

Related

Variances by row values depending on primary key

acc.num
Balance
Period
1234
200
1
2345
300
1
1234
300
2
2345
200
2
Is there a way to do row level variances of the balance for each acc. num by period with dax?
I've tried separating the single table in too multiple by filtering periods but can't figure out how to do it by row
Desired output would be the difference in period by account for example 1234 period 2 Balance 300 minus period 1 balance 200 = 100(result im looking for)
This should do the trick. Note that the first balance will be equal to the amount of the first period.
MyVariance =
VAR currentBalance =
SUM ( MyTable[Balance] )
VAR priorBalance =
CALCULATE (
SUM ( MyTable[Balance] ),
REMOVEFILTERS ( MyTable[Period] ),
MyTable[Period]
= MAX ( MyTable[Period] ) - 1
)
RETURN
currentBalance - priorBalance

Power BI Dax to Calculate the average in 3 periods

I have two below tables in Dim_Date the Period doesn't correspond to exact months and first I calculated a measure to Divide the count of records per region by NumofWeeks.
The Num of the week is always the same in the same period.
Table1
Region
Subregion
DataID
Amount
North Central
Missouri
12042022
123000
North Central
Minnesota
12052022
170000
North Central
North Dakota
10042022
234000
Northeast
New York
08042022
500000
Northeast
New Jersey
12052022
578000
Southwest
Nevada
12032022
679000
Southwest
Arizona
10032022
654000
DimDate
DataID
Period
NumofWeeks
12052022
2022_05
5
10042022
2022_04
4
12042022
2022_04
4
12052022
2022_05
5
08042022
2022_04
4
12032022
2022_03
4
10032022
2022_03
4
Divide per region week =
VAR TotalCountPerRegion = COUNT(Table1[Region])
VAR tblNumOfWeeksInPeriod=
SUMMARIZE(
DimDate
,DimDate[Period]
,DimDate[NumofWeeks]
)
VAR SuMOfWeeksInPeriod = SUMX(tblNumOfWeeksInPeriod,DimDate[NumofWeeks])
RETURN
MROUND(
DIVIDE(
TotalCountPerRegion
,SuMOfWeeksInPeriod
)
,1
)
With the formula, I got the following table
Period
Divide per region week
2021_04
17
2021_05
15
2021_06
9
2021_07
16
2021_08
20
2021_09
21
2021_10
17
2021_11
19
2021_12
20
2022_01
27
I want a new formula to calculate the average of the period with 2 following periods
Example
Average 2021_04 = 2021_04 + 2021_05 + 2021_06 = (17+15+9)/3
Average 2021_05 = 2021_05 + 2021_06 + 2021_07 = (15+9+16)/3
and so on.
thank you in advance.
I accepted this solution thank you very much ...
[M 3-Period Rolling Avg] =
// Set the number of periods to calculate
// the average over.
var PeriodCount = 3
// First, get the last visible period.
var LastPeriodSeqno = MAX( Dim_Date[PeriodSeqno] )
// Then, get the periods over which to calc.
var PeriodsToAverageOver =
CALCULATETABLE(
DISTINCT( Dim_Date[PeriodSeqno] ),
// Here's the place where you use the fact that
// all the periods are consecutively numbered.
// In fact, the counting does not have to start
// at 1 but it has to increment by 1.
Dim_Date[PeriodSeqno] <= LastPeriodSeqno,
Dim_Date[PeriodSeqno] > LastPeriodSeqno - PeriodCount,
REMOVEFILTERS( Dim_Date )
)
// We need to make sure that there are indeed
// PeriodCount periods in the set. Otherwise,
// the average will not be correct. This could happen
// if we were too close to the beginning of the calendar.
var ShouldCalculate =
COUNTROWS( PeriodsToAverageOver ) = PeriodCount
var Result =
if( ShouldCalculate,
CALCULATE(
AVERAGEX(
PeriodsToAverageOver,
[M]
),
REMOVEFILTERS( Dim_Date )
)
)
return
Result
My calendar is different for example the periods are like a financial calendar the period
2021_04 - Starts on 05/04/2021 and finishes on 02/05/2021
2021_05 - Starts on 03/05/2021 and finishes on 30/05/2021
2021_06 - Starts on 31/05/2021 and finishes on 04/07/2021
I want to calculate
Divide per region week =
VAR TotalCountPerRegion = COUNT(Table1[Region])
VAR tblNumOfWeeksInPeriod=
SUMMARIZE(
DimDate
,DimDate[Period]
,DimDate[NumofWeeks]
)
VAR SuMOfWeeksInPeriod = SUMX(tblNumOfWeeksInPeriod,DimDate[NumofWeeks])
RETURN
MROUND(
DIVIDE(
TotalCountPerRegion
,SuMOfWeeksInPeriod
)
,1
)
Rolling 3 periods if we start in 2021_04 I have
2021_06 = Average(2021_06+2021_05+2021_04)
2021_07 = Average(2021_07+2021_06+2021_05)
2021_08 = Average(2021_08+2021_07+2021_06)
and so on
I need to incorporate the formula above because the average is the sum of the formula for 3 periods.
Thanks in advance

PowerBI calculate overflow backlog in measure

My data is as follows:
Factory ID
Week
CAPACITY
Request
1
21
1000
500
1
22
1000
1200
1
23
1500
1600
1
24
1500
1100
2
21
1000
500
2
22
2000
1900
2
23
2000
1000
2
24
2000
2500
3
21
1000
200
3
22
1000
900
3
23
1000
1300
3
24
1000
800
I want to calculate backlog in a measure or any other way to have backlog be dynamic based on the factories I select. Backlog is calculated as follows:
Backlog = Capacity - (Request + Previous week backlog); where we have backlog when requests + pre. week backlog exceeds capacity or else it is 0. I cannot move capacity from future weeks, so the backlog would always accumulate going forward
Eg. If I select Factory 1, my backlog should look as follows:
Factory Selected: 1
Week
Backlog
21
0
22
-200
23
-300
24
-100
Factory Selected: 1,2
Week
Backlog
21
0
22
-100
23
0
24
-400
Factory Selected: 1,3
Week
Backlog
21
0
22
-100
23
-500
24
0
I have been trying to find a solution since the last 2 days. Let me know if you need any additional details. Any help will be greatly appreciated.
In DAX there are no loops and no recursion, this means that we must write some very ugly loop unrolling dax code. This is a minimal implementation of a working measure, but to make it working in the general case the BacklogWeekNN variables must be added until reaching the maximum possible week depth in the model.
Backlog =
VAR MinWeek = CALCULATE(MIN( T[Week] ), REMOVEFILTERS( T ) )
VAR MaxWeek = MAX( T[Week] )
VAR TAggregated =
ADDCOLUMNS(
CALCULATETABLE( VALUES( T[Week] ), T[Week] <= MaxWeek, REMOVEFILTERS( T ) ),
"#Capacity", CALCULATE( SUM( T[CAPACITY] ), ALLEXCEPT( T, T[Week], T[Factory ID] ) ),
"#Request", CALCULATE( SUM( T[Request] ), ALLEXCEPT( T, T[Week], T[Factory ID] ) )
)
VAR BacklogWeek00 = SUMX( FILTER( TAggregated, T[Week] = MinWeek ), [#Capacity] - [#Request] ) + 0
VAR BacklogWeek01 = SUMX( FILTER( TAggregated, T[Week] = MinWeek + 1), [#Capacity] - [#Request] ) + IF(BacklogWeek00 > 0, 0, BacklogWeek00)
VAR BacklogWeek02 = SUMX( FILTER( TAggregated, T[Week] = MinWeek + 2), [#Capacity] - [#Request] ) + IF(BacklogWeek01 > 0, 0, BacklogWeek01)
VAR BacklogWeek03 = SUMX( FILTER( TAggregated, T[Week] = MinWeek + 3), [#Capacity] - [#Request] ) + IF(BacklogWeek02 > 0, 0, BacklogWeek02)
VAR Result = IF(BacklogWeek03 > 0, 0, BacklogWeek03)
RETURN Result
This way we can obtain the desired resulting matrix using a slicer to select the factories (I'm afraid there are a few errors in the expected result samples int the question)
Edit: I used 'FILTER( TAggregated, T[Week] = MinWeek )' instead of the equivalent CALCULATE/CALCULATTABLE DAX code to avoid context transition happening and because the weeks table has very few rows to be iterated (I can imagine a maximum of a few tens or at maximum hundreds of rows if keeping a few years history)

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.

Count No of occurrences in measure

I have created a measurement (Sales/budget Percentage) that returns the performed output regarding sales/budget. If the percentage output of this measurement is greater or equal to 71% then return 1.
I then need to count the No of occurrences from this measurement that is 1.
I have tried multiple syntax such as Countx, Controws(filter etc. but al of these needs a table and column which I don't provide because it is a measurement.
this is my simple measurement to retrieve a 1 if value is true,
Percentage = sales/budget
Green = IF([Percentage]>=0,701;1;0)
My table is as follows for example
Sales Budget (Measurement) Percentage (Measurement) Green
100 80 125% 1
50 100 50% 0
100 100 100% 1
My Measurement which then would count No of occurrences of 1 or 0 in table above would return as below but this I can't seem to figure out.
No 1 = 2
No 0 = 1
How do I do this since my tests with counting above values = IF Above 70% is 1 doesn't seem to work?
Create a calculated column using the following DAX:
[Above 70% Flag] :=
VAR Result =
CALCULATE ( DIVIDE ( 'Table'[Sales], 'Table'[Budget] ) )
RETURN
SWITCH ( TRUE (), Result >= .71, 1, 0 )
Then, create your measure referencing your new column:
[Count Above 70%] :=
CALCULATE ( COUNT ( [Above 70% Flag] ), [Above 70% Flag] > 0 )
[Count Below 70%] :=
CALCULATE ( COUNT ( [Above 70% Flag] ), [Above 70% Flag] = 0 )