How do I add a column in PowerBI that evaluates the difference of Latest End time and Earliest Start Time from a large dataset? - sql

I am new to PowerBI. I am trying to make a report of the number of days consumed for a test to complete. There is a large fleet of tests that are run across a week and I would like to subtract the Earliest Start Time from the Latest End time, excluding Saturdays and Sundays and then display the result as a new column next to the Latest actual end as shown in the picture below.
Pardon for any errors above. The data was fetched from a SQL Server using a query (if that helps). Thank you.

query in sql server
select
*
--number of days
,DATEDIFF (day,[Earrliest startTime], [Latest actualend]) diffday
from TestTable
--excluding Saturdays and Sundays
where datepart(weekday,[Earrliest startTime]) not in (6,7)
and datepart(weekday,[Latest actualend]) not in (6,7)
SQL Fiddle
Hope it help you :-)

Create a New Table DateTable to be your calendar table.
DateTable = CALENDARAUTO()
Add a weekday column so you can filter out weekends.
Weekday = WEEKDAY(DateTable[Date])
Now you can create a measure that counts the days between your first and last day:
DayCount = COUNTX(DateTable,
IF(DateTable[Date]+1 > MIN(StartEnd[startTime]) &&
DateTable[Date]+1 < MAX(StartEnd[acutalend]) &&
NOT(DateTable[Weekday] IN {1,7}),
1, BLANK()))
The +1 are there to give you end of day rather than beginning of day.

Related

Oracle SQL: Dynamic timeframe calculation

Greetings all knowing Stack.
I am in a bit of a pickle, and I am hoping for some friendly assistance form the hive mind.
I need to write a query that returns the difference in days between a registration date (stored in a table column) and the first day of the last September.
For example; assuming the query was being run today (24-10-2016) for a record with a registration date of 14-07-2010, I would want the script to return the difference in days between 14-07-2010 and 01-09-2016
However had I run the same query before the end of last August, for example on 12-08-2016, I would want the script to return the difference in days between 14-07-2010 and 01-09-2015.
I'm fine with the process of calculating differences between dates, it's just the process of getting the query to return the 'first day of the last September' into the calculation that is tripping me up!
Any input provided would be much appreciated.
Thankyou =)
Try this approach:
add four months to the current date
truncate this date to the first of year
subtract four months again
Add_Months(Trunc(Add_Months(SYSDATE, 4), 'year'), -4)
Hope this might help.
WITH T AS (SELECT TO_DATE('14-07-2010','DD-MM-YYYY') REG_DATE,
SYSDATE EXEC_DATE
FROM DUAL)
SELECT CASE WHEN TO_CHAR(EXEC_DATE,'MM') >= 9
THEN ADD_MONTHS(TRUNC(EXEC_DATE,'YEAR'),8)
ELSE ADD_MONTHS(TRUNC(ADD_MONTHS(EXEC_DATE,-12),'YEAR'),8)
END
- REG_DATE AS DIFF
FROM T;

Get the month and year now then count the number of rows that are older then 12 months in SQL/Classic ASP

I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12

Return last week data in hive

I am new to hive and sql.
Is there any way if we run a query today with count fields then it should fetch last 7 days data ( example- if i run a query with count fields on monday then I should get the total count from last week monday to sunday) And date in my table is in the format 20150910. (yyyyMMdd).
Kindly please help me on this.
You can use date_sub() in this case. Something like this should work...
select * from table
where date_field >= date_sub(current_date, 7)
assuming that the current day's data is not loaded yet. If you want to exclude the current day's data too, you will have to include that too in the filter condition
and date_field <= date_sub(current_date, 1)
current_date would work if your hive version > 0.12
else, you can explicitly pull the date from unix using to_date(from_unixtime(unix_timestamp()))

SQL Date Issue for Weekly Report Data

I need to run a weekly report based on the arrival date. How can I set the arrival date in the where clause so that I can get the result only for each week. The hard part is I DO NOT want to modify the dates each week. I need the permanent where clause for the date. I have to provide a list of customers who arrived every week and I just want to run the same script without changing the week dates.
Please assist.
Thanks.
SELECT * FROM TABLE WHERE
(ARRIVAL_DATE>DATEFROMPARTS(YEAR(GETDATE()-7), MONTH(GETDATE()-7), DAY(GETDATE()-7)))//7 days before starting at midnight
AND
(ARRIVAL_DATE<DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE()))) //NOW in the YYYY, MM,DD format
This will get everything that happened in the current calendar week (Mon-Sun)
SELECT * FROM Table1
WHERE ArrivalDate BETWEEN
CAST(DATEADD(dd,(((DATEPART(dw,getdate())+##DATEFIRST) % 7)+5) % 7 ,getdate()) as date) AND
CAST(DATEADD(dd,6+((((DATEPART(dw,getdate())+##DATEFIRST) % 7)+5) % 7 ),getdate()) as date)
Edit - Removed extra E in BETWEEN

SQL Stored Procedure: Business Hours

How can I create a stored procedure that accepts a start and end date.(e.g April 1 - April 30
1.) Get the business days including Saturdays x (a value). +
2.) Get Holidays x (a value)
and return the total.
I'm new to this, I guess it would be a tsql function. hmm.
any help would be appreciated.
Thanks
The simplest solution to this problem is to create a Calendar table that contains a value for every day you might want to consider. You could then add columns that indicate whether it is a business day or a holiday. With that, the problem becomes trivial:
Select ..
From Calendar
Where IsBusinessDay = 1
And Calendar.[Date] Between '2010-04-01' And '2010-04-30'
If you wanted the count of days, you could then do:
Select Sum( Case When IsBusinessDay = 1 Then 1 Else 0 End ) As BusinessDayCount
, Sum( Case When IsHoliday = 1 Then 1 Else 0 End ) As HolidayCount
From Calendar
Where Calendar.[Date] Between '2010-04-01' And '2010-04-30'
http://classicasp.aspfaq.com/date-time-routines-manipulation/how-do-i-count-the-number-of-business-days-between-two-dates.html
First, you will need to store all of the holidays into an independant table (Christmas, Easter, New Year Day, etc. with their respective dates (normally timed at midnight));
Second, you will have to generate, into a temporary table maybe, the dates of the office days, it then excludes the dates contained in the Holidays table.
Third, you may set the office hours to these dates depending on what day it is, if you have different working hours on different day.
That is the algorithm for you to find the appropriate code implementation.
Let me know if this helps!