how to find time band using dax in power bi? - powerbi

I would like to find time band of half hour.Suppose i have a time stamp :
2019-03-12 01:20:00 this exists between time band of 1:00-1:30, similarly
2019-03-12 04:33:00 this exists between time band of 4:30-5:00.
I have column of n number of timestamp.
Is there any dax function that i can write to find the time band or there is any other process?
Thanks in advance!!

Consider something like this:
TimeBand =
FORMAT(FLOOR(MyTable[Timestamp], 1/48),"h:mm")
& " - "
& FORMAT(CEILING(MyTable[Timestamp], 1/48),"h:mm")
Floor and Ceiling are very similar functions -- they essentially round to the nearest multiple. Floor picks the biggest multiple that is smaller, and ceiling picks the smallest multiple that is bigger. Usually we round to powers of 10, but with this function we can round to the nearest 5, or 7, or 1/3.
Combine this with the understanding that PowerBI values all datetimes as a special kind of number -- it is simply the count of days since 12/30/1899. (Try it, create a calculated column and set its value to 0, then display it as a date time). So in this system "1" is one day. That means 1/24 is one hour, and 1/24/60 is one minute. It follows, then, that 30/24/60 is 30 minutes, and that reduces to 1/48.
So now we can take Floor/Ceiling, give it a time, and have it round that datetime to the nearest half-hour -- either down or up depending on which function you choose.
Use format to convert everything to text, and you're all set.
Here's some sample data generated in Power Query:
let
Source = List.Generate(()=> #datetime(2020,3,1,0,0,0), each _ < #datetime(2020,3,1,0,0,0) + #duration(1,0,0,0) , each _ + #duration(0,0,5,0)),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Timestamp"}, null, ExtraValues.Error)
in
#"Converted to Table"
And the results of our DAX:
Hope it Helps!

Related

Dax - Reverse margin calculation on a slider slicer

I have a measure that calculates the reverse margin following this principle:
RMS=C/(1−(MP/100))
Where RMS is the Reverse Margin Sell Price (£)
C is the cost of the product (£)
MP is the margin percentage (%)
The DAX measure itself looks like this:
_rms20% =
VAR newGP = DIVIDE([_cost], (1-20/100))
RETURN
IF([_%currentGP]>0.2, BLANK(), newGP)
So if the current GP percentage is higher than 20% blank space is returned, if it is lower I have the RMS calculation returned.
This works nicely but problem occurs when I create a "What-IF" parameter slider as follows:
Increase = GENERATESERIES(0, 100, 1)
Increase Value = SELECTEDVALUE('Increase'[Increase])
and use this parameter with the RMS measure:
_slider% = [_rms20%] * (100+'Increase'[Increase Value])/100
For example if my Cost is £27.26 and desired gp is 20% than cost has to be increased to £34.08 - this is done by the basic calculation following the principle mentioned above.
If I put this on a slider and increase it by 5 to 25% the value it shows is £35.78 while in fact it should be £36.34.
I have been trying to fix this for some time now so any advice/recommendation would be very appreciated.

Hour:Minute format on an APEX chart is not possible

I use Oracle APEX (v22.1) and on a page I created a (line) chart, but I have the following problem for the visualization of the graphic:
On the y-axis it is not possible to show the values in the format 'hh:mi' and I need a help for this.
Details for the axis:
x-axis: A date column represented as a string: to_char(time2, 'YYYY-MM')
y-axis: Two date columns and the average of the difference will be calculated: AVG(time2 - time1); the date time2 is the same as the date in the x-axis.
So I have the following SQL query for the visualization of the series:
SELECT DISTINCT to_char(time2, 'YYYY-MM') AS YEAR_MONTH --x-axis,
AVG(time2 - time1) AS AVERAGE_VALUE --y-axis
FROM users
GROUP BY to_char(time2, 'YYYY-MM')
ORDER BY to_char(time2, 'YYYY-MM')
I have another problem to solve it in another way: I am not familiar with JavaScript, if the solution is only possible in this way. Because I started new with APEX, but I have seen in different tutorials that you can use JS. So, when JS is the only solution, I would be happy to get a short description what I must do on the page.
(I don't know if this point is important for this case: The values time1 and time2 are updated daily.)
On the attributes of the chart I enabled the 'Time Axis Type' under Settings
On the y-axis I change the format to "Time - Short" and I tried with different pattern like ##:## but in this case you see for every value and also on the y-axis the value '01:00' although the line chart was represented in the right way. But when I change the format to Decimal the values are shown correct as the line chart.
I also tried it with the EXTRACT function for the value like 'EXTRACT(HOUR FROM AVG(time2 - time1))|| ':' || EXTRACT(MINUTE FROM AVG(time2 - time1))' but in this case I get an error message
So where is my mistake or is it more difficult to solve this?
ROUND(TRUNC(avg(time2 - time1)/60) + mod(avg(time2 - time1),60)/100, 2) AS Y
will get close to what you want, you can set Y Axis minimum 0 maximum 24
then 12.23 means 12 hour and 23 minutes.

Convert a number column into a time format in Power BI

I'm looking for a way to convert a decimal number into a valid HH:mm:ss format.
I'm importing data from an SQL database.
One of the columns in my database is labelled Actual Start Time.
The values in my database are stored in the following decimal format:
73758 // which translates to 07:27:58
114436 // which translates to 11:44:36
I cannot simply convert this Actual Start Time column into a Time format in my Power BI import as it returns errors for some values, saying it doesn't recognise 73758 as a valid 'time'. It needs to have a leading zero for cases such as 73758.
To combat this, I created a new Text column with the following code to append a leading zero:
Column = FORMAT([Actual Start Time], "000000")
This returns the following results:
073758
114436
-- which is perfect. Exactly what I needed.
I now want to convert these values into a Time.
Simply changing the data type field to Time doesn't do anything, returning:
Cannot convert value '073758' of type Text to type Date.
So I created another column with the following code:
Column 2 = FORMAT(TIME(LEFT([Column], 2), MID([Column], 3, 2), RIGHT([Column], 2)), "HH:mm:ss")
To pass the values 07, 37 and 58 into a TIME format.
This returns the following:
_______________________________________
| Actual Start Date | Column | Column 2 |
|_______________________________________|
| 73758 | 073758 | 07:37:58 |
| 114436 | 114436 | 11:44:36 |
Which is what I wanted but is there any other way of doing this? I want to ideally do it in one step without creating additional columns.
You could use a variable as suggested by Aldert or you can replace Column by the format function:
Time Format = FORMAT(
TIME(
LEFT(FORMAT([Actual Start Time],"000000"),2),
MID(FORMAT([Actual Start Time],"000000"),3,2),
RIGHT([Actual Start Time],2)),
"hh:mm:ss")
Edit:
If you want to do this in Power query, you can create a customer column with the following calculation:
Time.FromText(
if Text.Length([Actual Start Time])=5 then Text.PadStart( [Actual Start Time],6,"0")
else [Actual Start Time])
Once this column is created you can drop the old column, so that you only have one time column in the data. Hope this helps.
I, on purpose show you the concept of variables so you can use this in future with more complex queries.
TimeC =
var timeStr = FORMAT([Actual Start Time], "000000")
return FORMAT(TIME(LEFT([timeStr], 2), MID([timeStr], 3, 2), RIGHT([timeStr], 2)), "HH:mm:ss")

How to to solve divide by zero error (NaN - Not a Number) in my formula in Power BI?

I have a column with a formula which reads:
Utilisation (Excl. Time-off) = Utilisation_Excl_Timeoff[Billable Hours]/(Utilisation_Excl_Timeoff[Available Hours] - Utilisation_Excl_Timeoff[Timeoff Hours (Excl. Public)])
It gives me a "NaN" error in my calculated column for some of the cells due to divide by zero.
I would like to replace the NaN with a 0% instead so that the column displays correctly in my matrix chart.
Take a look at the DIVIDE function (https://msdn.microsoft.com/en-us/library/jj677276.aspx).
This is a 'safe divide` function with the option to return an alternative value if the division returns an error.
-JP
If you are looking for solution in M, then add conditional column:
Set up Otherwise temporarily to dividend column, here I took [Value] column. After doing this change in editor [Value] to [Value]/[Units]. This returns null wherever Units is 0. You may change returned output to 0% according to thy wish.
Alternatively, you can do it as well by adding this step:
= Table.AddColumn(#"Previous Step", "UnitPrice", each if [Units] = 0 then "0%" else [Value]/[Units])
IFERROR(value, value_if_error) function can do this. MSDN
Utilisation (Excl. Time-off) = IFERROR(Utilisation_Excl_Timeoff[Billable Hours]/(Utilisation_Excl_Timeoff[Available Hours] - Utilisation_Excl_Timeoff[Timeoff Hours (Excl. Public)]), 0)

Python 2.7 Find occurences from datetime and plot

Since I didn't find anywhere else this topic I will ask it here. I am getting data from CSV file, I have written datetime format in one of columns. I get that column with pandas module and then I need to count occurrences in specific time slots and plot that with matplotlib. Bellow you can see example of column.
Time and Date
0 2015-08-21 10:51:06.398000
1 2015-08-21 10:51:00.017000
2 2015-08-21 10:52:06.402000
3 2015-08-21 10:54:06.407000
...
I know how I can split time like so:
pd.date_range("10:50", "12:30", freq="1min").time
But how can I assign occurrences of my read values from CSV and then plot it? Any advice or direction would help.
It's hard to tell what you want as you haven't posted desired output but if I understand you correctly you want to count the number of rows in time intervals of certain length. You can do this by combining resample and len. To use resample, first set the index to 'Time and Date:
df.set_index('Date and Time', drop=False)
Note that drop=False is only necessary if the data frame has no other columns.
Then to get the number of rows in each 1-minute interval do
counts = df.resample('1min', len).astype(int)
If there are multiple dates and you want to sum the counts for each time interval over dates do
counts.groupby(lambda ts: ts.time()).sum()