DAX Help (if blank then blank) - powerbi

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" )
)

Related

how to get running sum/cumulative sum of a measure in power bi using DAX (direct query)

i have simple dataset like below where the delta is a measure (not a column) which is the diff of demand and supply.I wanted to calculated the running sum of measure "delta" as shown below.running sum needs to be at material-location-week level.
dax i tried:
Cum =
var mat = MAX('table'[Material])
var pla = MAX('table'[Plant])
var pw =MAX('table'[PWk])
return
CALCULATE(
table[delta],
FILTER(
ALL(table),
'table'[Material]= mat && 'table'[Plant] <= pla && 'table'[PWk]<=pw
)
))
<>
material location week demand supply delta(demand-supply) running_sum??
123 1000 wk1 100 40 60 60
123 1000 wk2 40 30 10 70
123 2000 wk1 30 20 10 10
123 2000 wk2 40 15 25 35
please help. I am stuck with this and dont know`enter code here` where i am going wrong.
Perhaps:
running_sum =
VAR Mat =
MAX ( 'Table'[material] )
VAR Loc =
MAX ( 'Table'[location] )
VAR Wk =
MAX ( 'Table'[week] )
RETURN
CALCULATE (
[delta],
FILTER (
ALL ( 'Table' ),
'Table'[material] = Mat
&& 'Table'[location] = Loc
&& 0 + SUBSTITUTE ( 'Table'[week], "wk", "" )
<= 0 + SUBSTITUTE ( Wk, "wk", "" )
)
)

Powerbi to return a 'NA' using the IF that is used in a std table

I have a code in a powerbi table that displays all. It basically counts how long a particular task is performed for mgt. It was done by someone else. I am trying to find out how to get the rows to display a 'No Data' or 'NA' if there is no data. I tried several approaches but could not get it to work. Any suggestions? Linda from Wisconsin.
TimeTaken =
//----CALCULATIONS
VAR DIFF = 'Opened'[Resolved] - 'Opened'[Opened]
VAR NumOfMinutes = DIFF * 24
* 60
VAR DAYS =
IF ( DIFF >= 1, INT ( DIFF ), BLANK () )
VAR HOURS =
INT ( ( DIFF - DAYS ) * 24 )
VAR MINUTES = NumOfMinutes
- ( DAYS * 24
* 60 )
- ( HOURS * 60 )
//---TEXTS
VAR DaysText =
IF ( DAYS >= 1, FORMAT ( DAYS, "00" ) & "days" )
//ELSE ("No Data") >>>>> this not working.. I deleted some
VAR HoursText =
FORMAT ( HOURS, "00" ) & "hrs"
VAR MinutesText =
FORMAT ( MINUTES, "00" ) & "mins"
RETURN
COMBINEVALUES ( " ", DaysText, HoursText, MinutesText )
Hi You can try below code. Just replace your last two line with this code.
RETURN
IF (
ISBLANK ( DaysText ) && ISBLANK ( HoursText )
&& ISBLANK ( MinutesText ),
"NA",
COMBINEVALUES ( " ", DaysText, HoursText, MinutesText )
)

DAX to convert to Minutes and Seconds

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.

Calculating a Moving Average for 3 months without blank values in DAX Power BI

I have Client Services table with [Billable Minutes] value.
I need to create [Moving Average for 3 months] measure, where the calculation will
only happen - if there are values in all 3 months in [Billable Minutes] exist !
(See below)
If [Billable Minutes] value is blank in at least one month in my Client Services table, then I want [Moving Avg] will show 0 or blank!
My goal:
Client Services table: Result:
Period [Billable Minutes] Period [Moving Avg]
2018-11 200 2019-01 200
2018-12 300
2019-01 100
To reach my goal, I am using the following DAX expression for [Moving Avg 3 months]:
Moving Avg 3 month =
VAR PeriodToUse = DATESINPERIOD('Calendar FY'[Date], LASTDATE('Calendar FY'[Date]), -3, MONTH)
VAR Result = CALCULATE(DIVIDE([Billable Minutes], COUNTROWS ('Calendar FY')), PeriodToUse)
VAR ZeroValue=IF(Minx('Client Services',[Billable Minutes])=0,0,Result)
Return Result
But, unfortunately, my end result is:
Client Services table: Result:
Period [Billable Minutes] Period [Moving Avg]
2018-11 200 2018-11 67
2018-12 300 2018-12 167
2019-01 100 2019-01 200
So, it takes an existing values in Client Services table and divides them by the number of periods.
For example for 2018-11, 2018-10, 2018-09 - it takes (200+0+0)/3=66.6 (67 rounded)
But I need the Moving Avg would be empty for 2018-11, because there are no values for 2018-10, 2018-09 in the Client Services table (same - for 2018-12, should not calculate)
Please, HELP!
Updated:
Here is the solution for this (thx to the answer below)
In order to check if there zero in the selected period, extra measures should be created:
Billable Minutes
Moving Avg Prev Month = Calculate('Client Services'[Billable Minutes],
PREVIOUSMONTH('Calendar FY'[Date]))
Billable Minutes
Moving Avg 2nd Prev Month = Calculate
('Client Services'[Billable Minutes Prev Month],
PREVIOUSMONTH('Calendar FY'[Date]))
Then, when you check whether there are zero values - you need to check it - not
just for [Billable Minutes], but for [Billable Minutes] within 3 months period =
within
[Billable Minutes]+[Billable Minutes Prev Month]+
[Billable Minutes 2nd Prev Month]
See the updated below (worked perfect):
enter
Billable Minutes 3 Months Avg =
VAR PeriodToUse = DATESINPERIOD('Calendar FY'[Date],
LASTDATE('Calendar FY'[Date]), -3, MONTH)
VAR Result = CALCULATE(DIVIDE([Billable Minutes],
COUNTROWS ('Calendar FY')), PeriodToUse)
VAR NMonthsPeriodBlank =
if([Billable Minutes] = BLANK(),0,1) +
if([Billable Minutes Prev Month] = BLANK(),0,1) +
if([Billable Minutes 2nd Prev Month] = BLANK(),0,1)
RETURN IF(NMonthsPeriodBlank < 3, BLANK(), Result)
Follow these below steps-
Step-1: Convert your Period column to Date considering 1st date of each month as below-
Step-2: Get back to report by clicking Close & Apply and create this below 4 measures-
total = SUM('Client Services'[Billable Minutes])
total prev = CALCULATE([total],PREVIOUSMONTH('Client Services'[Period]))
total second prev = CALCULATE([total prev],PREVIOUSMONTH('Client Services'[Period]))
3 month avergae =
VAR devide_by = if([total] = BLANK(),0,1) + if([total prev] = BLANK(),0,1) + if([total second prev] = BLANK(),0,1)
VAR total_amount = [total] + [total prev] + [total second prev]
RETURN IF(
devide_by < 3,
BLANK(),
total_amount/devide_by
)
Here is the final output-
====================
Solution in single measure
====================
You can convert all measures into 1 measure as below-
3 month average new =
VAR this_month = SUM('Client Services'[Billable Minutes])
VAR prev_month =
CALCULATE(
SUM('Client Services'[Billable Minutes]),
PREVIOUSMONTH('Client Services'[Period])
)
VAR second_prev_month =
CALCULATE(
SUM('Client Services'[Billable Minutes]),
PREVIOUSMONTH(
DATEADD(
'Client Services'[Period],
-1,
MONTH
)
)
)
VAR devide_by =
if(this_month = BLANK(),0,1) +
if(prev_month = BLANK(),0,1) +
if(second_prev_month = BLANK(),0,1)
VAR total_amount = this_month + prev_month + second_prev_month
RETURN IF(
devide_by < 3,
BLANK(),
total_amount/devide_by
)

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.