Count No of occurrences in measure - powerbi

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 )

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 )

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 - 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.

Subtotal <> sum of the rows

I'm a relative neophyte with DAX so bear with me. In the simplest terms, I want to double the measure amount for all regions that are not Europe, then sum the result. Here is some example DAX:
DEFINE
measure Fact[test] = CALCULATE (IF(SELECTEDVALUE('Dim'[Region]) = "Europe", Fact[Revenue],Fact[Revenue] * 2))
EVALUATE(
SUMMARIZECOLUMNS(
ROLLUPADDISSUBTOTAL('Dim'[Region], "RegionSubtotal"),
"Original Measure", Fact[Revenue],
"New Measure", Fact[test]
)
)
Region
RegionSubtotal
Original Measure
New Measure
Asia
False
200
400
Americas
False
500
1000
Europe
False
300
300
True
1000
2000
I'm trying to get (400+1000+300) = 1700 for the second column instead of 2000. Any ideas?
For the subtotal row, the selected value is not "Europe" so it's doubling the value.
To fix this, you want to iterate over the regions in your measure. Something like this:
test =
SUMX (
VALUES ( 'Dim'[Region] ),
IF (
'Dim'[Region] = "Europe",
[Revenue],
[Revenue] * 2
)
)
An other alternative would be to create a calculated column wherein , in case if region<>Europe, then amount* 2 else amount.
Then take the sum of the calculated column , but this would be like having an additional data .

Determining a Style Changeover by Machine using PowerBI

So I have a table that has the output of all machines in a department with styles. For example:
|Machine| |Style| | QTY| |Time| |Date| etc...
1 001 100 8:00AM 5/21/19
2 001 200 8:05AM 5/21/19
1 001 100 9:00AM 5/21/19
1 004 100 10:00AM 5/21/19
2 001 200 9:05AM 5/21/19
I'm looking to see the amount of times a style is changed for a machine. So in this case, for Machine 1 it was one style change and for Machine 2 it was zero.
I've tried adapting some code to no avail; mainly because I'm having trouble understanding the logic and I can't really think of a good index to work with.
Here is what I got so far:
EarliestChange Over Index =
VAR Temp =
CALCULATE (
MAX ( Table[Index] ),
FILTER (
Table,
[Index] < EARLIER ( [Index] )
&& [Style] <> EARLIER ( [Style])
&& Table[Date] = today()-1
)
)
VAR Temp1 =
CALCULATE (
MIN ( [Index] ),
FILTER (
Table,
[Index] > EARLIER ( [Index] )
&& [Style] <> EARLIER ( [Style])
&& Table[Date] = today()-1
)
)
RETURN
IF ( [Index] > temp && OR ( [Index] < temp1, ISBLANK ( temp1 ) ), temp + 1, 0 )
I tried to restrict it to just one day so that I could evaluate the results so that portion can be dropped. I've tried two different indexes, one was the machine number and the other was the difference in time from today and the min date on the table. In a visual, I've been taking a distinct count of the EarliestChange Over Index and subtracted one since it didn't constitute a "change over."
EDIT:
Issue where multiple styles are logged at the same time causing false change overs.
|Machine| |Style| | QTY| |Time| |Date| etc...
1 001 100 8:00AM 5/21/19
1 001 100 9:00AM 5/21/19
1 004 100 10:00AM 5/21/19
1 004 100 10:00AM 5/21/19
1 004 100 10:00AM 5/21/19
In one department a time would never be duplicated. However, in another department (for whatever reason) might log 3 rolls at the same time. This would cause the equation to log 10:00am as 3 change overs. It might be a system glitch why there isn't unique time stamps per roll but this is the case unfortunately.
One way of doing it:
First, I modified your data as follows:
Added a record for Machine 1 at 11:00AM to capture a situation when a style reverts to the old one;
Added a column for Date-Time (simply Date + Time), to make life easier;
Named the table as "Data"
Measure:
Style Change Count
=
SUMX (
Data,
VAR Current_DateTime = Data[Date-Time]
VAR Current_Style = Data[Style]
VAR Previous_DateTime =
CALCULATE (
MAX ( Data[Date-Time] ),
FILTER ( ALLEXCEPT ( Data, Data[Machine] ), Data[Date-Time] < Current_DateTime )
)
VAR Previous_Style =
CALCULATE (
VALUES ( Data[Style] ),
FILTER ( ALLEXCEPT ( Data, Data[Machine] ), Data[Date-Time] = Previous_DateTime )
)
RETURN
IF ( Current_Style = Previous_Style || ISBLANK ( Previous_Style ), 0, 1 )
)
Result:
How it works:
We need to use SUMX to make sure that our subtotals and totals are correct;
SUMX iterates over Data table and for each record computes "Previous date-time", which is simply the max datetime less than the current datatime, per machine (hence ALLEXCEPT);
Then, we can calculate Previous Style, which is a style where date-time = previous date-time;
Finally, we compare current style and previous style. If they are not the same, we add 1;
In addition, I added a test for the starting condition - first occurrence of a machine, for which previous style does not exist yet. I did not treat such records as "style change". If you want to count initial records as style change, remove ISBLANK() part.