I am trying to make a report that gives an 'active snapshot' for a past month. For example - how many active women are employed in October?
The date (a particular month) I am going for the active snapshot will change. I am using the following fields in my formula employee.hire_date is the date the employee started with our organization, employee.term_date is the date employment ended.
I am then grouping employees by gender and using the employee.gender and then the employee.name
What do you guys think about this logic?
The employee.hire_date can be anytime during that month, minus the last day? The employee.term_date can be anytime during that month, minus the last day? I am saying minus the last day because the employee had to have worked at least 1 day during the month. I am stumped....
Then make a formula, and set the parameters to the first and last of the month?
I think you are in the right direction and it might help if i tell that i think you need to approach 6 different situations for a given month. The point is if there is an intersection between the employee working period and the given month.
Suppose:
h is the hire_date
T is the term_date
m is the fist day of a given month
M is the last day of a given month
See these "timelines": (sorry, a little bit confusing)
h..T..m........M...... (1- start and end before the month, no intersection)
......m..h..T..M...... (2- start and end inside the month, there is intersection)
......m........M..h..T (3- start and end after the month, no intersection)
h.....m........M.....T (4- start before the month, end after the month, intersection)
h.....m....T...M...... (5- start before the month, end inside the month, intersection)
......m...h....M.....T (6- start inside the month, end after the month, intersection)
So, there is intersection if h <= M and T >= m (hurray, karnaugh map!).
To ignore the last day, as you said, maybe it would be: h < M and T > m.
Create a running total field that count the employees by gender if there is intersection.
EDIT: Maybe the full formula should be:
{EMPLOYEE.Hire_Date}<={?Last of Month} and (
isnull({EMPLOYEE.Termination_Date}) OR
{EMPLOYEE.Termination_Date}>={?First of Month}
)
Related
I am having trouble with a task in Tableau. I need to pull in a value for each month, but the value originates in the following month (month + 1). For example, in January, I need to pull a beginning value from February, and then in February I need to pull a beginning value from March. The idea is to take the beginning value from the following month to calculate the difference from the ending value from the current month. The data I have is snapshot based, so there is one column which is the Date column, which can be 1/1/2020, or 2/1/2020 etc. (its done by month, not days).
My instinct is to create a calculated field and say something along the lines of 'where snapshot = snapshot (current) + 1 month, take in this value' but that does not work because they are not equal. I need to create a separate column or LOD for the future month I think? or a parameter?
ex.
{fixed [date], [ID]: SUM( [Date] = Dateadd('month',1,[Date]) Then [Begin Value] End)}
I found the answer. I needed to use the LOOKUP() function.
I have a dataset of customers buying items in multiple batches of consecutive days over the year e.g. Customer A buys on the 1st of January, the 2nd of January and the 3rd of January, stops, then buys again on the 1st of February, the 2nd of February and the 3rd of February.
I'm looking to capture the first and last date of each consecutive batch for each customer (so the usual MIN / MAX will miss out of batches in between dates).
I've experimented with RETAIN and LAG and I'm getting close but its not quite what I want.
How do I create a query that will display two rows for Customer A? i.e. row 1 showing start date of the 1st of January and end date of the 3rd of January; row 2 showing start date of the 1st of February and end date of the 3rd of February.
You are asking to group the values based on the presence of a gap between the dates. So test for that and create a new group number variable. Then you can use that new grouping variable in your analysis.
data want ;
set have ;
by id date;
dif_days = dif(sales_date);
if first.id then group=1;
else if dif_days > 1 then group+1;
run;
You can adjust the number of days in the last IF statement to adjust how large of a gap you want to allow and still consider the events as part of the same group.
I have a date column which I am trying to query to return only the largest date per month.
What I currently have, albeit very simple, returns 99% of what I am looking for. For example, If I list the column in ascending order the first entry is 2016-10-17 and ranges up to 2017-10-06.
A point to note is that the last day of every month may not be present in the data, so I'm really just looking to pull back whatever is the "largest" date present for any existing month.
The query I'm running at the moment looks like
SELECT MAX(date_col)
FROM schema_name.table_name
WHERE <condition1>
AND <condition2>
GROUP BY EXTRACT (MONTH FROM date_col)
ORDER BY max;
This does actually return most of what I'm looking for - what I'm actually getting back is
"2016-11-30"
"2016-12-30"
"2017-01-31"
"2017-02-28"
"2017-03-31"
"2017-04-28"
"2017-05-31"
"2017-06-30"
"2017-07-31"
"2017-08-31"
"2017-09-29"
"2017-10-06"
which are indeed the maximal values present for every month in the column. However, the result set doesn't seem to include the maximum date value from October 2016 (The first months worth of data in the column). There are multiple values in the column for that month, ranging up to 2016-10-31.
If anyone could point out why the max value for this month isn't being returned, I'd much appreciate it.
You are grouping by month (1 to 12) rather than by month and year. Since 2017-10-06 is greater than any day in October 2016, that's what you get for the "October" group.
You should
GROUP BY date_trunc('month', date_col)
I currently have a dataset where the range of results span between the dates: 15-01-2017 to 30-04-2018
So therefore, my min(date) would be 15-01-2017.
However, I want to return results whereby if my min(date) is not a complete month, it should give me the results from next full month onwards (i.e.: 01-02-2017 to 30-04-2018).
Hoping to get some direction
Thank you
This will return the start of the next month unless it's the first of the month:
trunc(add_months(min(datecol)-1,1), 'mon')
Add one month to the day before the min date (i.e. only the 1st will result in the previous month), then get the 1st of that month.
Depending on your actual query you might filter using
qualify datecol >= trunc(add_months(min(datecol) over (partition by ??)-1,1), 'mon')
I have a report I am trying to modify in Crystal. It has a data field that has a formula in it, but I want to use another formula.
This is an example of what I am trying to do.
[((# Days in January) – 15) x (Market Rent/(# Days in January))]
+ [((# Days in February) – 0) x (Market Rent/(# Days in February))]
+ [14 x (Market Rent/(# Days in March))]
I have ADO commands built out for the market rent, and a start date and end date. The months in my example are just that an example. I am not sure how to take my ADO command dates that are entered in on a filter page, and put them in a formula like the one above. Any ideas?
Also, in the first and last para. the -15 and the 14 are for a date in the middle of the month. So if the start date was on the 15th of Jan, and the End date was the 15th of march. This formula would calculate my loss of rent during vacancy.
If I'm reading your question correctly, you want to take a date field and find out how to measure the number of days in that month, the month before it, and the month after it. Here's some Crystal formulas to help you out. Let's assume your date field is called {#DateFld}:
To find the number of days in a particular month relative to a particular date, try this:
local datevar X:=cdate(dateadd("m",0,{#DateFld}));
datediff(
"d",
date(year(X),month(X),1),
date(year(X),month(X)+1,1)
)
I recommend you copy & paste this in 3 different formulas:
- In the 1st formula, replace the "0" with a -1 to get the number of days in the previous month.
- In the 2nd formula, don't change anything. That'll get you the number of days in the current month (i.e. the month that {#DateFld} is in)
- In the 3rd formula, replace the "0" with a +1 to get the number of days in the next month.
For example, if {#DateFld} is March 10th, 2011, the 1st formula will give you 28, the 2nd will give you 31, and the 3rd will give you 30.