I have the below query which gives me the headcount per month when I select a month from the date table which is fine and works correctly.
I would like the average headcount for the past 12 months from the selected date e.g if i select Aug 2020, the average should be a sum of headcount from July 2019 - Aug 2020
Active Employees =
var currentdate = 'Date'[Max Date]
RETURN
CALCULATE(
DISTINCTCOUNT('Joined Query'[EMP_NO]),
'Joined Query'[DATE_OF_EMPLOYMENT] <= currentdate,
or(
ISBLANK('Joined Query'[DATE_OF_LEAVING]),
'Joined Query'[DATE_OF_LEAVING] > currentdate
)
)
You can reuse your headcount measure per month like this:
average 12 months =
VAR currentdate = SELECTEDVALUE('Date'[Max Date])
RETURN AVERAGEX(
SUMMARIZE(
FILTER(ALL('Date'),
'Date'[Max Date] <= currentdate && 'Date'[Max Date] >= EDATE(currentDate,-11)),
'Date'[Max Date],
"headcount", [Active Employees]
), [headcount])
Please note, -11 gives you the date 12 months ago in regards to the current selected date. In your question you say if you select August you want to see from July last year, and that would actualy be 14 months, not 12. If that's your use case you need to change it to -13.
Create 2 following measures for 12 month start and end date-
12 Month End Date =
VAR D1 =
DATEVALUE(
SELECTEDVALUE('Date'[Max Date].[MonthNo])
& "/1/"
& SELECTEDVALUE('Date'[Max Date].[Year])
)
RETURN D1-1
12 Month Start Date =
VAR D1 =
DATEVALUE(
SELECTEDVALUE('Date'[Max Date].[MonthNo])
& "/1/"
& SELECTEDVALUE('Date'[Max Date].[Year])
)
RETURN EDATE(D1,-12)
Now create this following measure for average calculation-
12_month_average =
(
CALCULATE(
DISTINCTCOUNT('Joined Query'[EMP_NO]),
DATESBETWEEN(
'Date'[Max Date],
[12 Month Start Date],
[12 Month End Date]
)
) + 0
) / 12
Related
I'd like to have a range of day 20th - 25th in each month in BigQuery but i dont know what syntax should i use. For ex:
Jan 20 - 25
Feb 20 - 25
and so on
I only can think of creating a CTE for every month then union all those.
Consider below query.
SELECT DATE_ADD(month, INTERVAL day - 1 DAY) date_range,
FROM UNNEST(GENERATE_DATE_ARRAY('2022-01-01', '2022-03-01', INTERVAL 1 MONTH)) month,
UNNEST(GENERATE_ARRAY(20, 25)) day;
Query results
Below seem to be more simple than my original answer and you could adjust date range by specifying condition on WHERE clause.
SELECT *
FROM UNNEST(GENERATE_DATE_ARRAY('2022-01-01', '2022-12-31', INTERVAL 1 DAY)) date_range
WHERE EXTRACT(DAY FROM date_range) BETWEEN 21 AND 25
For the usecase that you commented,
WHERE EXTRACT(DAY FROM date_range) >= 21 OR EXTRACT(DAY FROM date_range) = 1
im working on a calculated column in a SSAS model.
My Idea is to generate every month as date ( f.e 2021-01-01) based on a start and end date, who are also columns of that entry. Basically i wanna duplicate every entries with the month column for further calculations
when i have startdate 2021-04-28 and enddate 2022-02-28
i want to have 2021-04-01 , 2021-05-01 etc... till 2022-02-01
how do i write this in DAX?
Try this way:
Calendar =
VAR start_date = DATE( 2021, 4, 28 )
VAR end_date = DATE( 2022, 2, 28 )
VAR start_date_first = DATE( YEAR( start_date ), MONTH( start_date ), 1 )
VAR end_date_eom = EOMONTH( end_date, 0 )
VAR result =
FILTER(
CALENDAR( start_date_first, end_date_eom ),
DAY( [Date] ) = 1
)
RETURN
result
I want to have a date table created with DAX for a power BI report where it will have week index numbers 0,-1,-2,etc to go back in time and make comparisons of weeks. I found the below out on the web (I dont remember where)
DATE =
GENERATE (
CALENDAR( DATE( YEAR( TODAY() ) - 2, MONTH( TODAY() ), DAY( TODAY()) ), TODAY()),
VAR startOfWeek = 1 // Where 1 is Sunday and 7 is Saturday, thus a 3 would be Tuesday
VAR currentDay = [Date]
VAR days = DAY( currentDay )
VAR months = MONTH ( currentDay )
VAR years = YEAR ( currentDay )
VAR nowYear = YEAR( TODAY() )
VAR nowMonth = MONTH( TODAY() )
VAR dayIndex = DATEDIFF( currentDay, TODAY(), DAY) * -1
VAR todayNum = WEEKDAY( TODAY() )
VAR weekIndex = INT( ROUNDDOWN( ( dayIndex + -1 * IF( todayNum + startOfWeek <= 6, todayNum + startOfWeek, todayNum + startOfWeek - 7 )) / 7, 0 ) )
RETURN ROW (
"day", days,
"month", months,
"year", years,
"day index", dayIndex,
"week index", weekIndex,
"month index", INT( (years - nowYear ) * 12 + months - nowMonth ),
"year index", INT( years - nowYear )
)
)
This report should be updated daily but I noticed that today VAR Week index is not calculating correctly. Today is 4/1/2020 so I would expect 3/29/2020 - 4/1/2020 to have a week index of 0 however only 4/1/2020 and 3/31/2020 have week index 0.
I have played around with this formula a bit and cannot seem to get something that would consistently work for a report that is updated daily. I am probably going to just begin using the ISO week and ISO year values to get at my comparisons but I will lose some functionality that the week index number gives me. Can anyone please help with the a working formula?
Thank you!
The weekIndex can be simpler calculated, you can change it to:
VAR weekIndex = ROUNDDOWN((dayIndex + todayNum - startOfWeek - 6)/7;0)
Hi I would like to store the number of working hours for a given week in a table with,
hours, year, week
so I can aggregate the hours for a week quickly, where week is the ISO week number.
I then want to do a date range filter query on this table, let's say
From 2018-12-24 to 2019-01-21 (year 2018 week 52 to 2019 week 4).
If the user pass the year and week, then I would need to do a range check on a compound index where you compare the year value first, and then the week number.
How should I structure the query and index to efficiently retrieve records with this range?
This is a basic attempt, given a start year and start week, and an end year and end week:
select year, week, hours
from
weekly hours
where((year = 2018 and week >= 52) OR year > 2018) AND
((year = 2019 and week <=3) OR year < 2019)
You can compare more than one column with the <= or >= operator:
select *
from weekly_hours
where (year, week) >= (2018,52)
and (year, week) <= (2019,3);
That query can make use of an index on both columns, e.g.
create index on weekly_hours (year, week);
You can calculate the week as an absolute number and use this for your range queries. An expression index can be used for the absolute week calculation.
create table weekly_hours (
year int,
week int,
hours int
);
insert into weekly_hours
(year, week, hours)
values
(2018, 52, 10),
(2019, 4, 9),
(2019, 10, 9);
-- expression index that generates the absolute week
create index weekly_hours_absweek_idx on weekly_hours((year * 53 + week));
-- range query
select year, week, hours
from weekly_hours
where
year * 53 + week >= 2018 * 53 + 52
and year * 53 + week <= 2019 * 53 + 4;
I wanted to show the forecast dates with the current date plus frequency up to one year in DB2.
date :Current date
if frequency is :2
upto : 2020-01-01
output be like :
2019-05-22,
2019-07-22,
2019-09-22,
2019-11-22
Try the following RCTE:
with t(dt) as (
values current date
union all
select dt + 2 month
from t
where year(dt + 2 month) = year(current date)
)
select dt
from t;