Let me start by saying that I am somewhat new to SQL/Snowflake and have been putting together queries for roughly 2 months. Some of my query language may not be ideal and I fully understand if there's a better, more efficient way to execute this query. Any and all input is appreciated. Also, this particular query is being developed in Snowflake.
My current query is pulling customer volumes by department and date based on a 45 day window with a 24 day lookback from current date and a 21 day look forward based on scheduled appointments. Each date is grouped based on where it falls within that 45 day window: current week (today through next 7 days), Week 1 (forward-looking days 8-14), and Week 2 (forward-looking days 15-21). I have been working to try and build out a comparison column that, for any date that lands within either the Week 1 or Week 2 group, will pull in prior period volumes from either 14 days prior (Week 1) or 21 days prior (Week 2) but am getting nowhere. Is there a best-practice for this type of column? Generic example of the current output is attached. Please note that the 'Prior Wk' column in the sample output was manually populated in an effort to illustrate the way this column should ideally work.
I have tried several different iterations of count(case...) similar to that listed below; however, the 'Prior Wk' column returns the count of encounters/scheduled encounters for the same day rather than those that occurred 14 or 21 days ago.
Count(Case When datediff(dd,SCHED_DTTM,getdate())
between -21 and -7 then 1 else null end
) as "Prior Wk"
I've tried to use an IFF statement as shown below, but no values return.
(IFF(ENCOUNTER_DATE > dateadd(dd,8,getdate()),
count(case when ENC_STATUS in (“Phone”,”InPerson”) AND
datediff(dd,ENCOUNTER_Date,getdate()) between 7 and 14 then 1
else null end), '0')
) as "Prior Wk"
Also have attempted creating and using a temporary table (example included) but have not managed to successfully pull information from the temp table that didn't completely disrupt my encounter/scheduled counts. Please note for this approach I've only focused on the 14 day group and have not begun to look at the 21 day/Week 2 group. My attempt to use the temp table to resolve the problem centered around the following clause (temp table alias: "Date1"):
CASE when AHS.GL_Number = "DATEVISIT1"."GL_NUMBER" AND
datevisit1.lookback14 = dateadd(dd,14,PE.CONTACT_Date)
then "DATEVISIT1"."ENC_Count"
else null end
as "Prior Wk"*
I am extremely appreciative of any insight on the current best practices around pulling prior period data into a column alongside current period data. Any misuse of terminology on my part is not deliberate.
I'm struggling to understand your requirement but it sounds like you need to use window functions https://docs.snowflake.com/en/sql-reference/functions-analytic.html, in this case likely a SUM window function. The LAG window function, https://docs.snowflake.com/en/sql-reference/functions/lag.html, might also be of some help
I set up a google forms form for my work where my employees pass information and this information is recorded in a spreadsheet. The information, when recorded, automatically inserts a date and time in the first column of form responses. However, when I enter the code = month (a1), it always returns the answer "1" or "January" and this information does not match the date entered in the column. How do I fix this?
If you are entering '=month(a1)' for every row, then you are always taking the month of the top left cell in the sheet. You would need to adjust the row number for the row you are in.
You could use something like '=month(now())' to ensure you are always getting the month of the current date.
I have an Excel tab like this.
It give me information on when a worker is plan to start on a product.
The Columns of my input Table are:
Product Number, Dummy column, Dummy column, Worker ID, Starting Date, Ending Date
The lines in my input Table are:
Product 1,xyz,xyz,Worker 1,13/08/2018 13:50,20/08/2018 15:30
Product 1,xyz,xyz,Worker 2,08/08/2018 03:50,16/08/2018 08:30
Product 1,xyz,xyz,Worker 9,23/08/2018 08:08,03/09/2018 10:00
Product 2,xyz,xyz,Worker 4,10/08/2018 13:50,27/08/2018 15:30
Product 2,xyz,xyz,Worker 9,18/08/2018 03:50,20/08/2018 08:30
Product 3,xyz,xyz,Worker 2,13/08/2018 08:08,13/09/2018 10:00
My Result table should have:
one line per Worker
one Column for each day of the year
The values in the Result Tab should show for a given Worker and a Given Date on which Product the Worker started to work.
So the cell could be empty if at this date the workerX didn't start to work on a product.
Do you have an idea how I can solve this in Excel (or Access if not possible in Excel)?
Access is probably going to be your best bet. You will want to create a column that strips the time value from the Starting Date column, leaving you with only the day value. Let's call that the Starting Day for now. Now create a crosstab query based on your table. The rows will be based on the Worker ID column. The columns will be based on the Starting Day column. The value will use the aggregate function called "First()" on the Product Number field.
I think it should look something like this in SQL (replace <YOURTABLE> with your table name):
TRANSFORM First([Product Number])
SELECT [Worker ID] FROM <YOURTABLE> GROUP BY [Worker ID]
PIVOT [Starting Day]
Good luck!
Transfer your data in column A.
In cells C2 to C20, type the names of the workers Worker 1, Worker 2, etc, in the same way as it is present in the data.
In row 1, enter the dates, starting from cell D1.
In my example, cell D1 is 7 Aug 2018, E1 is 8 Aug 2018., …
In cell D2, enter the following Array Formula
=IF(MAX(IFERROR(FIND($C2&","&TEXT(D$1,"dd/mm/yyyy"),$A$1:$A$1000,1),0))=0,"-","Yes")
Array Formulas are entered using Control + Shift + Enter, instead of Enter.
Drag the formula to the required cells.
The result will be "Yes" if a worker has started a project on a particular date, and "-" if no project has been started by a worker on a particular date.
Change the cell references as required.
In place of "Yes", if you want to get the Product details as the result, then use the below Array Formula in cell D2.
=IFERROR(LEFT(IF((MAX(IF(IFERROR(FIND($C2&","&TEXT(D$1,"dd/mm/yyyy"),$A$1:$A$1000,1),0)>0,ROW($1:$1000),"-")))=0,"-",INDEX($A$1:$A$1000,(MAX(IF(IFERROR(FIND($C2&","&TEXT(D$1,"dd/mm/yyyy"),$A$1:$A$1000,1),0)>0,ROW($1:$1000),"-"))),1)), FIND($C2,IF((MAX(IF(IFERROR(FIND($C2&","&TEXT(D$1,"dd/mm/yyyy"),$A$1:$A$1000,1),0)>0,ROW($1:$1000),"-")))=0,"-",INDEX($A$1:$A$1000,(MAX(IF(IFERROR(FIND($C2&","&TEXT(D$1,"dd/mm/yyyy"),$A$1:$A$1000,1),0)>0,ROW($1:$1000),"-"))),1)),1)-2),"-")
Do let me know if this is what you wanted to do.
Regards,
Vijaykumar Shetye,
Spreadsheet Excellence,
Panaji, Goa, India
I'm trying to create a NetSuite Time search that emulates the chart style display on an employee's weekly time record, with projects listed in rows and days of the week listed in columns, with totals by day and by project. The goal is to have a search auto filtered by "Last Week" that can be used with a drop down selector filter for employees. I know there are better ways, but this is a very specific demand from someone above who believes the NS time record is a "query" and wants it to act like one.
I'm good with NS searches but know almost next to nothing about coding. I tried some basic sum formulas using CASE WHEN but am having 2 issues:
1) Can't figure out how to get CASE WHEN to sort by the weekday output from DAY of the {date} and subsequently total the hours.
2) Not sure how to total hh:mm formatted time in searches, and can't figure out what the system name of the "Duration (Decimal)" field is.
Just need one line of a sum formula to total time data from one day of the week, and a way to solve the hh:mm issue and I am good to go from there.
CASE WHEN to_char({date}, 'D') LIKE 1 THEN {durationdecimal} ELSE 0 END
SUN = 1, MON = 2, etc.
I have an SSAS Cube which tracks the movement of items in a warehouse. Items are scanned into various locations, so, we have a date and time (HH:MM:SS) that the item is scanned. We want to be able to create a calculated measure, which is not pre-defined on the fact table, as we would like it to be dynamic, based on the attributes the query is sliced by.
So, in order to accommodate that, I figured I would create a SET which has the intersection of valid dates and valid times, like this:
WITH SET [Valid Date]
AS FILTER([WMSR Date].[Full Date].[Full Date].MEMBERS,[Measures].[Fact Scans Count]>0)
SET [Valid Time]
AS FILTER(([Valid Date]*[Time].[Full Time].[Full Time].MEMBERS),[Measures].[Fact Scans Count]>0)
So, if I run this:
SELECT {[Measures].[Fact Scans Count]} ON COLUMNS,
[Valid Time] ON ROWS
FROM
WarehouseCube
WHERE
[Piece].[Dim ID Key].&[1127981]
It will give me, for a given item, the date/time of the scan, and the count. How do I get the date/time from the previous row?
Thanks in advance!
You could use
[ValidTime].Item(Rank(([WMSR Date].[Full Date].CurrentMember, [Time].[Full Time].CurrentMember),
[ValidTime])
- 2
)
The Rank function returns the position of a tuple within a set. The Item function returns a tuple at a position from a set.
As Rank returns 1 for the first item, while Item expects 0 for the first item, and you want to go back one position, you need to subtract two.