DAX to convert to Minutes and Seconds - powerbi

I have the following DAX:
(sum(usage[session_duration])/100)/sum(usage[engaged_sessions])
The problem I have is it returns depending on the filters 2.49, when it should be returning 4.09. 4 for the number of minutes, and 9 for the seconds.
I wrote the following:
Var FirstTotal = (sum(usage[session_duration])/100)/sum(usage[engaged_sessions])
Var SecondTotal = FirstTotal*100
Var ThirdTotal = SecondTotal/60
VAR WholeNumberThirdTotal = INT(ThirdTotal)
Var FirstDot = FIND(".",ThirdTotal,1,0)
Var FourthTotal = MID(ThirdTotal, FirstDot, 2)
Var FifthTotal = FourthTotal*60
RETURN WholeNumberThirdTotal & "." & FifthTotal
and this seems to return the correct results. The problem I have with the above though is that I can't use it within a graph with a Date field. It only works within a Card.
Anyone know how I can improve this?

It appears to me the following returns a value in units of seconds:
SUM ( usage[session_duration] ) / SUM ( usage[engaged_sessions] )
I.e., without the /100 you have 249s, which is the same as 4m + 9s.
You can add formatting to this to get the display you're after:
VAR Sec = SUM ( usage[session_duration] ) / SUM ( usage[engaged_sessions] )
RETURN
FORMAT ( INT ( Sec / 60 ), "0" ) & "." & FORMAT ( MOD ( Sec, 60 ), "00" )
As you point out, this is a text value so it can't be used as a numerical value in chart visuals. You can convert 249 into the decimal value 4.09 just as easily:
VAR Sec = SUM ( usage[session_duration] ) / SUM ( usage[engaged_sessions] )
RETURN
INT ( Sec / 60 ) + MOD ( Sec, 60 ) / 100
If you use this in a bar chart, the height of the bars will be proportional to 4.09 minutes (i.e. 4m + 5.4s) rather than 4m + 9s (4.15 m).
Since Power BI doesn't have a duration type available for measures, if you're using a single measure, you must either sacrifice some accuracy in charting or not show the labels on the chart in the format you prefer. If you don't need to show labels on your graph, you can just use Sec / 60 for the graph and the formatted version wherever the numbers are displayed.

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, 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 Help (if blank then blank)

I am using this formula to Convert Seconds into D:H:M:S but if its blank or 0 i would like the ::: to not show up. any help would be appreciated.
2. String AVG Duration =
var vSeconds=[1. Time Lapse Sec M AVG]
var vMinutes=int( vSeconds/60)
var vRemainingSeconds=MOD(vSeconds, 60)
var vHours=INT(vMinutes/60)
var vRemainingMinutes=MOD(vMinutes,60)
var vDays=INT(vHours/24)
var vRemainingHours=MOD(vHours,24)
return
vDays&":" &
vRemainingHours&":"&
vRemainingMinutes&":"&
vRemainingSeconds& ""
Image
... Return
if ( isblank ([1. Time Lapse Sec M AVG]) || [1. Time Lapse Sec M AVG] = 0
, blank (), vdays&":"&vRemainghours...
You can simplify this quite a bit with FORMAT.
2. String AVG Duration =
VAR seconds = [1. Time Lapse Sec M AVG]
VAR days = INT ( seconds / 86400 )
VAR partial = seconds / 86400 - days
RETURN
IF (
NOT ISBLANK ( seconds ),
FORMAT ( days, "0:" ) & FORMAT ( partial, "hh:nn:ss" )
)

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.

Changing Average of Seconds in minutes and seconds

I have a table below:
I am trying to get the average of "someNumber" (which is number of seconds) per date and then convert that number into miutes and seconds.
So, I am drawing the graph below:
As you can see, I used the average filter and it calculated the average of "someNumber" per date. For instance, the average on 1st is 13. Now, how do I convert 13 into minutes and seconds so that I can display it as a tooltip?
Create a measure to get the average in seconds.
avg = AVERAGE(Table[SomeNumber])
Then create a measure for the tooltip lets call it Time, this helpful measure was created by #konstantinos and #smoupre in the Power BI community blog.
Time =
VAR Duration = [avg] // Here use the average measure created before
VAR Hours =
INT ( Duration / 3600)
VAR Minutes =
INT ( MOD( Duration - ( Hours * 3600 ),3600 ) / 60)
VAR Seconds =
ROUNDUP(MOD ( MOD( Duration - ( Hours * 3600 ),3600 ), 60 ),0)
VAR H =
IF ( LEN ( Hours ) = 1,
CONCATENATE ( "0", Hours ),
CONCATENATE ( "", Hours )
)
VAR M =
IF (
LEN ( Minutes ) = 1,
CONCATENATE ( "0", Minutes ),
CONCATENATE ( "", Minutes )
)
VAR S =
IF (
LEN ( Seconds ) = 1,
CONCATENATE ( "0", Seconds ),
CONCATENATE ( "", Seconds )
)
RETURN
CONCATENATE ( M, CONCATENATE ( ":", S ) )
Now use the [Time] measure as tooltip in your chart
Hope it helps.