PowerBi, time in hours plus 2 - powerbi-desktop

I have a table with start time and I would like to take just the hour part and then add 2 hours to it.
So in Start time I have 21.04.2016 07:00:40 and then the result in the new column would be 09:00:00
I've called the new column: HourPart
HourPart = TIME(Hour([StartTime]); 0; 0)
This however gives me 30.12.1899 07:00:00
Can someone point me in the right direction?
Thanks

Ok resolved it: I created a new column:
New Again = [StartTime] + Time(2;0;0)
Then another column:
HourPart = FORMAT([New Again]; "hh:mm:ss")

You could do it in a single column rather than creating a junk column as an interim step:
HourPart =
FORMAT ( [StartTime] + TIME ( 2, 0, 0 ), "hh:mm:ss" )
(using semi-colons for the delimiter in the time function if your locale is set that yas

Related

Select records with at one day in centain year

I want to make a selection of records where date range (start date - end date) includes at least 1 day of 2020. So for example: if a record has 2018-01-13 as the start date and 2020-01-09 as end date, it has to be included in my selection, as there is at least one day active in 2020.
Sample data:
How can I achieve this?
Just check the years with the standard between notation
Start Year <= 2020 <= End Year
=AND(YEAR(A1)<=2020,YEAR(B1)>=2020)
=IF(OR(YEAR(A2)=2020,YEAR(B2)=2020),1,0)
Excel store dates as numbers, so, you must compare them like numbers
Make a column with: =IF(OR(B2 < $C$2; $D$2 < A2); 0; 1)
where
A: start date
B: final date
C: 2020-01-01
D: 2020-12-31
, Now you can use only values 1

Create a rolling date list in Excel

I want to create a rolling list of dates in Excel like so:
Day Date
Day 1 01-Jul-19
Day 1 02-Jul-19
Day 1 03-Jul-19
Day 1 04-Jul-19
Day 1 05-Jul-19
Day 1 06-Jul-19
Day 1 07-Jul-19
Day 2 02-Jul-19
Day 2 03-Jul-19
Day 2 04-Jul-19
Day 2 05-Jul-19
Day 2 06-Jul-19
Day 2 07-Jul-19
Day 2 08-Jul-19
Day 3 03-Jul-19
Day 3 04-Jul-19
Day 3 05-Jul-19
Day 3 06-Jul-19
Day 3 07-Jul-19
Day 3 08-Jul-19
Day 3 09-Jul-19
Day 4 04-Jul-19
. .
. .
. .
So essentially what's happening is that the 7-day range moves forward by one day each time, from a specific start date (in the example above, 01-07-19) until it reaches an end date. Is there an automated way of doing this?
#ashvin10 you can do this in vba, but you can also accomplish this with 2 formulas without using vba at all, here's how:
for illustration purposes we'll just assume you are starting with 07/01/2019 on the first row and your information will be displayed in columns A and B.
in A1 enter the string Day 1
in B1 enter your starting date, like 07/01/2019
in A2 enter this formula: ="Day " & IF(MOD(ROW(A2),7)<>0, MID(A2,5,(LEN(A2)-4)), MID(A2,5,(LEN(A2)-4))+1)
in B2 enter this formula: =IF(A2=A1,B1+1,OFFSET(B2,-7,0)+1)
highlight cells A2 and B2
click on the cross that becomes available on the bottom right hand corner of cell B2
drag down the formula till you hit the end date you desire
the cells are populated with the values you requested in the format you requested
If you absolutely have to have it done using vba please let me know and I can show you how to do it that way as well, but this way is much easier.
EDIT: #ashvin10 I'm so sorry, the original formula I instructed you to put into A2 only works for Day 1 through Day 9, if you go into days past 9 it won't display correctly. I've fixed the formula that should be pasted into A2 so now it will work no matter how many days you go down. I'm so sorry for the confusion.
Alternatively, this can also be done in Python.
import datetime
start_date = '01-07-2019'
end_date = '31-01-2020'
output_file_name = 'rolling dates'
output_file_extension = '.CSV'
delimiter = '\t'
with open((output_file_name + output_file_extension.lower()), 'w+') as file:
header = "Day" + delimiter + "Date" + '\n'
file.write(header)
start_date_object = datetime.datetime.strptime(start_date, '%d-%m-%Y').date()
end_date_object = datetime.datetime.strptime(end_date, '%d-%m-%Y').date()
number_of_days = abs((end_date_object - start_date_object).days)
next_day = start_date_object
for i in range(1, number_of_days + 2):
for j in range(7):
file.write(("Day {0}" + delimiter + next_day.strftime('%d-%m-%Y') + '\n').format(i))
next_day += datetime.timedelta(days=1)
start_date_object += datetime.timedelta(days=1)
next_day = start_date_object
After running the code above, I simply created a blank Excel file and then imported the data from the CSV file output by this code.
This is arguably more complicated than #gharbad-the-weak's answer but thought I'd include this anyway.

SLA COUNT IN EXCEL FORMULA

I'm looking for an excel (Office 2016 packet) formula that count SLA for ticket resolution.
the SLA counter must consider :
1) Monday To Saturday as work day ;
2) Holiday as no working days (must be skipeed as work day);
3) SLA start for ticket generate in workdays during range-time "08:00-20:00" out of this time-range SLA count is "0" ;
4) Output should result as R1= first 24Hours ; R2= from 25 to 48 hours ; R3= from 49 to 72 hours; "Out of Sla" = since 72 hours
Data to count SLA is formatted as below for either for ticket open and closure:
Column N Column O Column P Column Q
"TICKET START DATE" "TICKET STOP DATE" "SLA" "HOLIDAY"
28/4/18 13:30 30/4/18 19:20 2 25/04/2018
28/4/18 13:11 29/4/18 13:11 1 01/05/2018
28/4/18 12:57 28/4/18 12:57 1
I solved point 1) & 2) with NETWORKDAYS.INTL formula using the first column as start_date ; the second column as end_date; 11 as third formula's field to exclude Sunday as workdays; fourth formula's value pointing a column where are listed the "holidays" date .
I could not find a solution for point 3).
It will also appreciate a possible solution for point 4) .
Thank you in advance.
Formula example of above fields :
=NETWORKDAYS.INTL(N15;O15;11;Q16:Q17)
Alessandro.
I've needed to do something similar in the past. Please see the formula below. It will give you the amount of time between working days (Monday-Friday), only taking into account times 8:30am to 5:30pm. Try inserting your specific parameters.
=IFERROR((NETWORKDAYS.INTL(H8,I8,,)-1)*("17:30"-"8:30")
+IF(NETWORKDAYS.INTL(I8,I8,,),MEDIAN(MOD(I8,1),"8:30","17:30")
,"17:30")-MEDIAN(NETWORKDAYS.INTL(H8,H8,,)*MOD(H8,1),"8:30","17:30"),"")
H8 = Start Date
I8 = End Date
8:30 = Start Time
17:30 = End Time

Add x number of working days to a date in a custom column

I'm trying to add a custom column in Power Query that adds 3 business days if a condition is met, else add 2 business days.
I can have it conditionally add days without issue, but am having trouble adding workdays instead. I know this is easily done in excel using =IF X = 1,WORKDAY([REFERENCE],3),WORKDAY([REFERENCE],2) but how can I do the same thing as a custom column in the query editor?
Below is what I have, which does days including weekends:
=if [REF]="1" then Date.AddDays([ETA],3) else Date.AddDays([ETA],2)
I wrote a custom function that solves this problem, unless you need to take holidays into account too. It is a bit universal, it works with 2 or 200 or 2000 added days. Equals =WORKDAY(date;days) function in Excel.
UPDATE: Bibake Uppal suggested there should be subtraction supported. Added subtraction for negative days.
Here it is:
(startDate, days) =>
let
Sign = if days < 0 then -1 else 1, //add a multiplier to enable negative 'days' to be subtracted rather than added
Step1 = List.Dates(Date.AddDays(startDate, Sign), Sign*days + Number.RoundUp(Sign*days/7*3+4,0), #duration(Sign,0,0,0)), //provision list of added days with 3 more days per each added week, starting from startDate + 1 day; This is a bit over-provisioning, but ensures the list is long enough.
Step2 = List.Select(Step1, (item) => Date.DayOfWeek(item, Day.Monday) < 5), // select only workdays
Step3 = List.FirstN(Step2, Sign*days), //select required number of workdays
Output = if days = 0 then startDate else List.Last(Step3)
in
Output
You can save it as a query, name it, say, AddWorkdays, and use as this:
YourStepName = Table.AddColumn(yourTable, "CustomColumnName",
each AddWorkdays([ETA], if [REF]="1" then 3 else 2)
//note that [REF]="1" filters a text value, not number!
Otherwise you can insert this function in your code as
fnAddWorkdays = (startDate, days) =>
let
Sign = if days < 0 then -1 else 1, //add a multiplier to enable negative 'days' to be subtracted rather than added
Step1 = List.Dates(Date.AddDays(startDate, Sign), Sign*days + Number.RoundUp(Sign*days/7*3+4,0), #duration(Sign,0,0,0)), //provision list of added days with 3 more days per each added week, starting from startDate + 1 day; This is a bit over-provisioning, but ensures the list is long enough.
Step2 = List.Select(Step1, (item) => Date.DayOfWeek(item, Day.Monday) < 5), // select only workdays
Step3 = List.FirstN(Step2, Sign*days), //select required number of workdays
Output = if days = 0 then startDate else List.Last(Step3)
in
Output
A solution is to break it down into 2 components:
Based on your initial condition, add 2 or 3 days
If this ends on a Saturday or Sunday, add 2 additional days to skip over the weekend
You have already implemented Step 1.
You now just need to implement Step 2, which is to add the 2 additional days if required. You can use DayOfWeek function to determine where you started, to determine if you'll need the extra 2 days:
If you're adding 3 business days, then you'll need to add 2 additional days if the initial day is Wed, Thu or Fri:
if Date.DayOfWeek([ETA], Day.Wednesday) <= 2 then {Add 2 more days}
If you're adding 2 business days, then you'll need to add 2 additional days if the initial day is Thu or Fri:
if Date.DayOfWeek([ETA], Day.Thursday) <= 1 then {Add 2 more days}
You can incorporate these into your initial statement.
Slight improvement on Eugene's response to allow for the subtraction of workdays as well.
= (startDate, days) =>
let
Step1 = if days >= 0 then List.Dates(Date.AddDays(startDate, 1), days + Number.RoundUp(days/7*3+4,0), #duration(1,0,0,0))
else List.Dates(Date.AddDays(startDate, -1), -days + Number.RoundUp(-days/7*3+4,0), #duration(-1,0,0,0)), //provision list of added days with 3 more days per each added week, starting from startDate + 1 day
Step2 = List.Select(Step1, (item) => Date.DayOfWeek(item, Day.Monday) < 5), // select only workdays
Step3 = List.FirstN(Step2, Number.Abs(days)), //select required number of workdays
Output = List.Last(Step3)
in
Output
Simply put in a negative value for days.

Determine if date is a workday in Excel

I have a date in column H10 and need to add 45 days to this date in the next Column I
If there are not dates Column I must be blank
If the 45th day falls on a weekend the calculation must move to the next workday which is Monday
You need to combine two fundamental functions.
First, DATE + INT = DATE. For example, if H10 = 1/8/2015 and H11 = H10 + 10 then H11 will show 1/18/2015.
In your case, you want to use H10 + 45.
Second, you can use the Weekday(date,mode) function to determine the day of the week. Personally, for your purpose, you could use weekday(h10 + 45, 2) which would give a 1-5 for MTWRF, and a 6-7 for a weekend day. So something like
=if(weekday(h10+45,2) < 6, "weekday", "weekend")
=if(weekday(h10+45,2) = 1, "Monday!!", "not monday...")
But we aren't done yet - you need to make sure your day actually ends up on a weekday. So we can do something like this - when determining a weekday, we can use that to determine how much we need to add. If we end up with a 6 (Saturday) we want to add 2 days to push it to a Monday. In the case of a 7, we want to add 1 day to push it to a Monday. Thus, we can simply take the 8 - weekday(h10+45) to add when it's a weekday. So our add value becomes
// determine day type weekday weekend, so add the offset
= if(weekday(h10+45) < 5, h10+45, h10 + 45 + (8 - weekday(h10+45))
You also have a requirement about being blank, so you'll want to wrap whatever you use with
=if(isblank(h10),"", /* your real function here */)
You can combine the functions for IF(), WEEKDAY() and WORKDAY() to calculate your finish date and ensure that it does not fall on a weekend.
I've used
WEEKDAY(WORKDAY(H10+45),16)
to have Saturday and Sunday be represented as days 1&2 respectively.
IF(WEEKDAY(WORKDAY(H10,45),16)=1,WORKDAY(H10,45)+2,IF(WEEKDAY(WORKDAY(H10,45),16)=2,WORKDAY(H10,46),H10))

Resources