Tool recommendation for data transform - powerbi

I have large amounts of raw fault data in Power BI.
code time status
x123 2019-04-22T23:57:00 ok
x123 2019-04-23T01:00:00 faulty
x123 2019-04-23T02:00:00 ok
x123 2019-04-23T23:00:00 faulty
x123 2019-04-24T01:00:00 ok
I need to transform this to show how long an item has been in a faulty state on a given day. So on the 23rd, the item was in a faulty state between 1 and 2a.m and then again between 11pm until past midnight.
code day % of day faulty
x123 23/04/2019 8.30% (2 hours)
Can I do this easily in Power BI or should I use another tool such as Azure Data Factory?

Add the following Calculated Columns to your table:
Report Date = Table1[time].[Date]
Fault Duration =
VAR CurrentTime = Table1[time]
VAR CurrentCode = Table1[code]
VAR PreviousTime =
CALCULATE (
MAX ( Table1[time] ),
FILTER (
Table1,
Table1[time] < CurrentTime &&
Table1[code] = CurrentCode
)
)
VAR NextTime =
CALCULATE (
MIN ( Table1[time] ),
FILTER (
Table1,
Table1[time] > CurrentTime &&
Table1[code] = CurrentCode
)
)
VAR FaultyFrom =
IF(
Table1[status] = "faulty",
Table1[time],
IF (
DAY(PreviousTime) = DAY(Table1[time]),
BLANK(),
Table1[time].[Date]
)
)
VAR FaultyTo =
IF (
Table1[status] = "ok",
Table1[time],
IF (
DAY(NextTime) = DAY(Table1[time]),
NextTime,
Table1[time].[Date] + 1
)
)
RETURN
IF(
ISBLANK ( PreviousTime ) || ISBLANK ( NextTime ) || ISBLANK ( FaultyFrom ),
BLANK(),
FaultyTo - FaultyFrom
)
Now create measures:
Faulty Hours = SUM ( Table1[Fault Duration] )
Faulty % Day =
IF (
HASONEVALUE ( Table1[Report Date] ),
DIVIDE (
[Faulty Hours],
DISTINCTCOUNT ( Table1[code] ),
BLANK()
),
BLANK()
)
Output:
See https://pwrbi.com/so_55825688/ for a worked example PBIX file

Related

EARLIER Function not working in DAX - ATR of Stocks

I am making ATR34 column in PowerBI in which I have a table that consists of Stocks, Date, High, Low, Close, PrevClose and Series (contains Equity,future).
I have made a calculated column of ATR from the High Low Close and Prev Close. I need to calculate the MA of 34 days of ATR calculated filtered by Stocks and Equity.
For that I am first calculating the Daynumber filtering Stocks and EQ; then 34 days and making an average of it.
However when i am using the below mentioned code in DAX EARLIER function is not working and i am unable to calculate it.
I am beginner in POWERBI.
DayNumber =
COUNTROWS (
FILTER ('Table','Table'[Date] <= EARLIER ( 'Table'[Date] )),
FILTER('Table'[Stock] = EARLIER ( 'Table'[Stock] )),
FILTER('Table'[EQ] = EARLIER ( 'Table'[EQ] ))
)
Another Caulcated Measured Column :
FirstDateOfRange34 =
CALCULATE (
VALUES ( 'Table'[Date] ),
FILTER ( 'Table','Table'[DayNumber] = EARLIER ( 'Table'[DayNumber] ) - 34),
FILTER('Table'[Stock] = EARLIER ( 'Table'[Stock] )),
FILTER('Table'[EQ] = EARLIER ( 'Table'[EQ] ))
)
Another Calculated Measured Column :
ATR34 =
CALCULATE (
AVERAGE ( 'Table'[ATR] ),
FILTER ( 'Table','Table'[Date] >= EARLIER ( 'Table'[FirstDateOfRange34] ))
FILTER ('Table','Table'[Date] <= EARLIER ( 'Table'[Date] ))
FILTER('Table'[Stock] = EARLIER ( 'Table'[Stock] ))
FILTER('Table'[EQ] = EARLIER ( 'Table'[EQ] ))
)
Your syntax is off. COUNTROWS expects a single table argument, not three arguments and FILTER needs a table for the first argument, not a column.
Try this intead:
DayNumber =
COUNTROWS (
FILTER (
'Table',
'Table'[Date] <= EARLIER ( 'Table'[Date] )
&& 'Table'[Stock] = EARLIER ( 'Table'[Stock] )
&& 'Table'[EQ] = EARLIER ( 'Table'[EQ] )
)
FirstDateOfRange34 =
CALCULATE (
VALUES ( 'Table'[Date] ),
FILTER (
'Table',
'Table'[DayNumber] = EARLIER ( 'Table'[DayNumber] ) - 34
&& 'Table'[Stock] = EARLIER ( 'Table'[Stock] )
&& 'Table'[EQ] = EARLIER ( 'Table'[EQ] )
)
)
ATR34 =
CALCULATE (
AVERAGE ( 'Table'[ATR] ),
FILTER (
'Table',
'Table'[Date] >= EARLIER ( 'Table'[FirstDateOfRange34] )
&& 'Table'[Date] <= EARLIER ( 'Table'[Date] )
&& 'Table'[Stock] = EARLIER ( 'Table'[Stock] )
&& 'Table'[EQ] = EARLIER ( 'Table'[EQ] )
)
)

Power BI Dax Group By Measure - Display Dates Before Today

I am trying to create a measure that will show booked revenue over time, grouped by a few categories. I also want to show as 0 within line charts when the data is BLANK(), so I have it in an IF statement. Everything works just fine except for whatever reason I cannot get it to only show dates prior to today.
Here is the current Measure:
M_BookedRevenue =
VAR CurrentAccountingYear =
CALCULATE ( MIN ( 'Date'[AccountingYear] ), 'Date'[Date] = TODAY () )
VAR CurrentDate =
CALCULATE ( MIN ( 'Date'[SortDate] ), 'Date'[Date] = TODAY() )
RETURN
CALCULATE (
IF (
ISBLANK (
SUMX (
FILTER (
GROUPBY (
RevenueChannel,
RevenueChannel[ChannelBucket],
RevenueChannel[Channel],
'Date'[Date],
'Date'[SortDate],
'Date'[AccountingWeek],
'Date'[WeekNumber],
'Date'[AccountingMonthEnglishAbbrYear],
'Date'[AccountingMonth],
'Date'[AccountingYear],
"Revenue", SUMX ( CURRENTGROUP (), RevenueChannel[TransactionRevenue] )
),
'Date'[AccountingYear] >= CurrentAccountingYear
&& 'Date'[SortDate] < CurrentDate
),
[Revenue]
)
),
0,
SUMX (
FILTER (
GROUPBY (
RevenueChannel,
RevenueChannel[ChannelBucket],
RevenueChannel[Channel],
'Date'[Date],
'Date'[SortDate],
'Date'[AccountingWeek],
'Date'[WeekNumber],
'Date'[AccountingMonthEnglishAbbrYear],
'Date'[AccountingMonth],
'Date'[AccountingYear],
"Revenue", SUMX ( CURRENTGROUP (), RevenueChannel[TransactionRevenue] )
),
'Date'[AccountingYear] >= CurrentAccountingYear
&& 'Date'[SortDate] < CurrentDate
),
[Revenue]
)
)
)

How to extract the resulted values from the matrix table to create anothe SUMX measure

What I am trying to do is to take out from the table below, just the result from Madrid (70,89%) and Barcelona (83,92%) and consolidate both results weighting them according to "total production" measure.
The expected result would be the following = ( 70,89% x 52.550 + 83,92 x 135.100 ) / ( 52.550 + 135.100 ) = 80,27%
Here are the measures that I have created to build my matrix table
Total Production = sum(Database[Production])
Total Working Hours = sum(Database[Working Hours])
TotalExpectedProduction = sum(Database[Expected Production])
Avexpectedprod = divide(sumx(Database;[TotalExpectedProduction]*[Total Working Hours]);sum(Database[Working Hours]))
YTD Productivity =
CALCULATE (
DIVIDE (
SUMX (
SUMMARIZE (
Database;
Database[Matchine];
"AA"; [Total Production] / ( [Total Working Hours] * [Avexpectedprod] )
);
[AA] * [Total Working Hours]
);
[Total Working Hours];
0
);
DATESYTD ( Calendar[Date]; "30/06" );
FILTER (
ALL ( 'Calendar' );
'Calendar'[Date] <= MAX ( 'Calendar'[Date] )
&& 'Calendar'[Fiscal Year] = MAX ( 'Calendar'[Fiscal Year] )
)
)
Here my data table structure
data table
Here the matrix table and the expected result
Image
Thank you so much for you Support
RBN
Done!!!
New2 =
DIVIDE (
CALCULATE (
SUMX ( VALUES ( Database[Location] ); [YTD Productivity] * [Total Production] );
Database[Location] = "Barcelona"
|| Database[Location] = "Madrid"
);
CALCULATE (
SUM ( Database[Production] );
Database[Location] = "Barcelona"
|| Database[Location] = "Madrid"
)
)

Percentage change from category to category

I will like to create a new column that calculates the percentage change from JAN-FEB-MAR-APR-MAY-JUN which are in the COLUMN "MONTH" based on the COLUMN "TOTAL".
Here is a script I have tried but its not working
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( Sheet1[TOTAL] ),
FILTER (
Sheet1,
Sheet1[MONTH]
= ( EARLIER ( Sheet1[MONTH] ) - 1 )
&&Sheet1 = EARLIER ( Sheet1[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH ),
0,
( Sheet1[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH
Here is the link to the power bi file. enter link description here
The Column 'Month' is not of type date. How would PowerBi know the text APR represents April? You need to make this column a date.
Now you need to change the script to work with DateDiff:
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( Sheet1[TOTAL] ),
FILTER (
Sheet1,
DATEDIFF(Sheet1[MONTH], EARLIER ( Sheet1[MONTH] ),MONTH) = 1
&& Sheet1[CATEGORY] = EARLIER ( Sheet1[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH );
0;
( Sheet1[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH)

Calculation based on previous row in same column and previous row in another column in power bi

I am trying to calculate NoofEventsInQueue Column as below in power Bi. 157 is the total no of Events
In a calculated column:
NumberOfEventsInQueue =
IF (
ISBLANK ( Table1[NoOfEventsAtATime] ),
BLANK(),
CALCULATE (
SUM ( Table1[NoOfEventsAtATime] ),
FILTER (
ALL ( Table1 ),
Table1[Ref_Time] >= EARLIER ( Table1[Ref_Time] )
)
)
)
If you have blank rows in the middle of your NoOfEventsAtATime column, and still want a value returned on your calculated column, you could use something like:
NumberOfEventsInQueue =
VAR TimeMin =
CALCULATE (
MIN ( Table1[Ref_Time] ),
FILTER (
ALL ( Table1 ),
NOT ISBLANK ( Table1[NoOfEventsAtATime] )
)
)
VAR TimeMax =
CALCULATE (
MAX ( Table1[Ref_Time] ),
FILTER (
ALL ( Table1 ),
NOT ISBLANK ( Table1[NoOfEventsAtATime] )
)
)
VAR Output =
IF (
Table1[Ref_Time] >= TimeMin && Table1[Ref_Time] <= TimeMax,
CALCULATE (
SUM ( Table1[NoOfEventsAtATime] ),
FILTER (
ALL ( Table1 ),
Table1[Ref_Time] >= EARLIER ( Table1[Ref_Time] )
)
),
BLANK()
)
RETURN Output