Determining a Style Changeover by Machine using PowerBI - 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.

Related

Power BI, Line Chart with Base Value 100

I need to build a line chart that is supposed to show the cumulative increase from Day 1 to the End.
An Example would be like this:
Date Capital Value
31-Jan 237 100.00
28-Feb 250 105.48
31-Mar 210 88.60
30-Apr 300 126.58
In other words, is dividing every value by the first value, not of the whole table but the dates that are been displayed in the chart. The first date is dynamically changed as time passes, making it impossible to fix the formula to 31-Jan for example
Kind Regards
Try this measure:
% Capital running total in Date =
VAR runtotal =
CALCULATE (
SUM ( 'YourTable'[Capital] ),
FILTER (
ALLSELECTED ( 'YourTable'[Date] ),
ISONORAFTER ( 'YourTable'[Date], MAX ( 'YourTable'[Date] ), DESC )
)
)
VAR baseval =
CALCULATE (
SUM ( 'YourTable'[Capital] ),
FIRSTDATE ( ALL ( YourTable[Date] ) )
)
RETURN
DIVIDE ( runtotal, baseval )
Note that for calculating the Running Total Power BI can help you with a Quick Measure.

DAX PowerBI: Replaced blank values with zero and issue with chart

I tried to create report in Power BI with sales month by month for last 20 months, when sales is blank I want to see month with 0 value.
I decided to change Blank() values with zero adding a 0 at the end of calculation.
It works great, however I have an issue with limitaton date hierarchy, because now my chart contains a lot of months without value (first value begins in 2017, date hierarchy first value begins in 2000).
Test:=
CALCULATE (
SUM( quantity ),
flag = 1,
title = "WEEKS"
) + 0
Instead of a plain 0, you could add an IF to specify to only add that after the first value. Something like this:
Test:=
VAR FirstDate = CALCULATE ( MIN ( date ), ALL( Dates ), quantity > 0 )
RETURN
CALCULATE (
SUM( quantity ),
flag = 1,
title = "WEEKS"
) + IF( date > FirstDate, 0 )
If the condition is false, the IF returns a blank and it shouldn't show up.

How to find DateDiff from same column with condition in Power BI

I am having one table in PowerBI which is having 3 columns: 1.EnrollId 2.Status 3.StatusChangeDate. One EnrollId is having 4 statuses and their particular statusChangeDates. I want to find no. of days between two dates with status condition.
EnrollId Status StatusChangeDate
101 AppStart 15/02/2019
101 Application 27/03/2019
101 Enrollment 03/04/2019
101 Complete 28/04/2019
I want to create formula in DAX like
[StatusChangeDate (where Status="Enrollment") - StatusChangeDate(where status="AppStart)]
or
[StatusChangeDate (where Status="Complete")- StatusChangeDate(where status="Enrollment)]
i.e. 03/04/2019 - 15/02/2019 = 44 Days
28/04/2019 - 03/04/2019 = 25 Days
My advice is go for a Calculated Column, you can try and adjust this to a measure but there is more factors to consider regarding the filter context.
For a calculated column:
DaysLastChange =
VAR _currentStatusChangeDate = [StatusChangeDate]
VAR _currentEnrollId = [EnrollId]
RETURN
DATEDIFF(
CALCULATE(MAX('Table1'[StatusChangeDate]);FILTER('Table1';_currentEnrollId = [EnrollId] && _currentStatusChangeDate > [StatusChangeDate] ));
_currentStatusChangeDate;
DAY
)
If you want measures, try something like this. For each period between two Statuses, you need to create another measure. When more than one [EnrollId] is selected, the measure returns a blank.
Days Enrollment - Complete =
VAR scDateEnrollment =
CALCULATE ( MAX ( 'Table'[StatusChangeDate] ), 'Table'[Status] = "Enrollment" )
VAR scDateComplete =
CALCULATE ( MAX ( 'Table'[StatusChangeDate] ), 'Table'[Status] = "Complete" )
RETURN
IF (
HASONEVALUE ( 'Table'[EnrollId] ),
DATEDIFF ( scDateEnrollment, scDateComplete, DAY )
)
Cardvisuals or a table-visual would look like this. As you can see the table-visual is not affected by the slicer.
EDIT
This is the table i used:

Calculation Error: A table of multiple values was supplied where a single value was expected

I want to show this year store count based on to date and from date.The following code is throwing this error when i am placing this column in table.
ThisYearStoreCount =
IF (
[DateDiff] > 365,
0,
IF (
DATESBETWEEN ( WUSA_CAL_DIM[End_Date], [From_Date], [To_Date] ),
DISTINCTCOUNTNOBLANK ( WUSA_STORE_DIM[Store Code] )
)
)
#RADO is spot on,
If I understand your logic, something like this should work.
ThisYearStoreCount =
IF (
[DateDiff] > 365,
0,
IF (
[From_Date] >= WUSA_CAL_DIM[End_Date]
&& [To_Date] <= WUSA_CAL_DIM[End_Date]
&& WUSA_STORE_DIM[Store Code] <> BLANK(),
1
)
)
That error usually means that you're using a function that returns a set of data, in a column context ( column contexts only allow a single value to exist per row ).
I'm reading between the lines a little bit, but the new column formula above will check if the end date is between the From_Date and To_Date before allowing the number 1 through.
Then you can just add the ThisYearStoreCount to any visual and it will sum wherever appropriate.
Your existing code should work as a measure ( instead of a column ) I think but it's impossible to tell without more information.

Cumulative number of entries in dates range

I'm trying to get cumulative number of unique IDs in given timeframe.
My DAX look like this:
Cumulative = CALCULATE(SUM(Data[ID]));DATESBETWEEN(Data[ack_date];DATE(YEAR(NOW());4;1);DATE(YEAR(NOW());11;30)))
There is similar measure for Year-1: [YEAR(NOW())-1]
What I want to achieve is area chart showing growing number od IDs in time comparing same periods this and previous year. When I give those measures as Values for chart and "ack_date" as its Axis what I get is values comparison month by month but not cumulative, just value for certain month.
Try this code. Adjust for year -1.
=
CALCULATE (
DISTINCTCOUNT ( Data[ID] ),
FILTER (
ALL ( Data ),
AND (
Data[Ack_date] <= MAX ( Data[Ack_date] ),
AND (
Data[Ack_date] <= DATE ( YEAR ( NOW () ), 11, 30 ),
Data[Ack_date] >= DATE ( YEAR ( NOW () ), 4, 1 )
)
)
)
)