I need to create a schedulle with that pattern
into a dinamic table, like this
what i tryed :
date = FILTER(CALENDARAUTO(12),[DATE] >=2022)
isDayOff =
var iniDayOff1 = DATE(2022,12,5)
var iniDayOff2 = DATE(2022,12,8)
return IF( WEEKNUM([DATE])+WEEKDAY(IniDayOff1)-2 || WEEKNUM([DATE])+WEEKDAY(IniDayOff2)-2 = WEEKDAY([DATE]), "X", )
My problem is when WEEKNUM([DATE])+WEEKDAY(IniDayOff1)-2 is bigger then 7, i need that formula to reset, because when dayoff is saturday (WEEKDAY 7), the next dayoff is sunday (WEEKDAY 1)
Use this formula instead:
isDayOff =
VAR iniDayOff1 = DATE(2022, 12, 5)
VAR iniDayOff2 = DATE(2022, 12, 8)
RETURN
IF(
MOD(DATEDIFF(IniDayOff1, 'date'[Date], DAY), 8) = 0
|| MOD(DATEDIFF(IniDayOff2, 'date'[Date], DAY), 8) = 0,
"X",
BLANK()
)
Related
I've calculated beginning headcount of employees using DAX below:
Date = CALENDARAUTO()
Beginning HC =
VAR last_date = MAX('Date'[Date])
VAR first_date = MIN('Date'[Date])
VAR begin_hc = CALCULATE (
COUNT(Table1[EmployeeID]),
Table1[orighiredate_key] <= last_date,
Table1[terminationdate_key] >= first_date
)
RETURN begin_hc
and i want to calculate ending headcount, where current month ending headcount equals next month beginning headcount, e.g. 2021 Feb beginning headcount = 2021 Jan ending headcount, how can i achieve that?
i've tried:
Ending HC = CALCULATE(
[Beginning HC],
NEXTMONTH('Date'[Date])
)
but it's not working
sample output as below:
dataset used: https://www.kaggle.com/datasets/HRAnalyticRepository/employee-attrition-data
I've found a workaround to do it
Since NEXTMONTH is not working in this case, I do it by getting end of month of next month and start of month of next month
Ending HC =
VAR last_date = DATE(
YEAR(EOMONTH(MAX('Date'[Date]), 1)),
MONTH(EOMONTH(MAX('Date'[Date]), 1)),
DAY(EOMONTH(MAX('Date'[Date]), 1))
)
VAR first_date = EOMONTH(
MIN('Date'[Date]), 0
) + 1
VAR ending_hc = CALCULATE(
COUNT(Table1[EmployeeID]),
Table1[orighiredate_key] <= last_date,
Table1[terminationdate_key] >= first_date
)
RETURN ending_hc
This is a very odd request. It is difficult to explain in words, so let me show you what I am trying to do. Lets say I have a table with the following values.
Category Sales Qty Current Year % Of Sales Compared to Helmets
Helmets 150 150/150 = 100%
Gloves 70 70/150 = 47%
Pants 80 80/150 = 53%
I have tried doing this but it only works for the first row. The rest of the rows it will not calculate.
CY% =
VAR _myPercent =
CALCULATE(
SUM(Production[Sales]),
FILTER(ALL(Production), Production[Category] = "Helmets"),
FILTER(Production, Production[Week] <= Max(DatesTable[CurrentWeek])),
FILTER(Production, Production[Year] = Max(DatesTable[CurrentYear]))
)
Return
CALCULATE(
SUM(Production[Sales]),
FILTER(Production, Production[Week] <= Max(DatesTable[CurrentWeek])),
FILTER(Production, Production[Year] = Max(DatesTable[CurrentYear]))
)
/
myPercent
Lets check this code
CY% =
VAR maxWeek = Max(DatesTable[CurrentWeek])
VAR maxYear = Max(DatesTable[CurrentYear])
VAR _myPercent =
CALCULATE(
SUM(Production[Sales]),
Production[Category] = "Helmets",
KEEPFILTERS(Production[Week] <= maxWeek) ,
Production[Year] = maxYear
)
Return
CALCULATE(
SUM(Production[Sales]),
KEEPFILTERS(Production[Week] <= maxWeek),
Production[Year] = maxYear
)
/
myPercent
I was able to get it working with your help. I needed to add ALLSELECTED to each filter. Here is the final solution:
CY% =
VAR _myPercent =
CALCULATE(
SUM(Production[Sales]),
FILTER(ALLSELECTED(Production), Production[Category] = "Helmets"),
FILTER(ALLSELECTED(Production), Production[Week] <= Max(DatesTable[CurrentWeek])),
FILTER(ALLSELECTED(Production), Production[Year] = Max(DatesTable[CurrentYear]))
)
Return
CALCULATE(
SUM(Production[Sales]),
FILTER(Production, Production[Week] <= Max(DatesTable[CurrentWeek])),
FILTER(Production, Production[Year] = Max(DatesTable[CurrentYear]))
)
/
_myPercent
I need to create the calculated column - IsImproved - in the following calculated table - tbl_QOL (screenshot below)
It should compare AvgQOL (another calculated column) by MetricDate for each Client (EHRClientFK)
If - for any particular EHRClientFK AvgQOL value where IsDateMax = 1 is higher than AvgQOL value for IsDateMin = 1, then IsImproved value = 1; any other cases IsImproved = 0
For example:
EHRClientFK = 2666; AvgQOL for the (highest(max) date=1) = 3.66; 3.66 value is not higher than 3.83, means IsImproved for EHRClientFK 2666 = 0
EHRClientFK = 3444; AvgQOL for the (highest(max) date=1) = 3.5; 3.5 value IS HIGHER than 2, means IsImproved for EHRClientFK 3444 = 1
All shown in the picture below
Applied the following code.
But it doesn't work correctly.
It puts 1 even if AvgQOL for IsDateMax = 1 is less than AvgQOL for IsDateMin = 1, while I expect 0 there - like it is for AvgQOL = 3.66 (for the bigger date - 5/13/2021)
enter
IsImproved =
var _DateRecent = QOL[IsDateMax]
var _DateLatest = QOL[IsDateMin]
var _max = calculate(max(QOL[AvgQOL]), FILTER(ALL('QOL'), _DateRecent=1))
var _min = calculate(min(QOL[AvgQOL]), FILTER(ALL('QOL'), _DateLatest=1))
var _min1 = calculate(max(QOL[AvgQOL]), FILTER(ALL('QOL'), _DateRecent=0))
var _min2 = calculate(min(QOL[AvgQOL]), FILTER(ALL('QOL'), _DateRecent=1))
return if(_max > _min || _max >_min1 || _max > _min2, 1, 0)
I am super new to DAX, plz help!
I'd prefer to use LOOKUPVALUE here:
VAR ClientFK = tbl_QOL[EHRClientFK]
VAR AvgQOLForMin =
LOOKUPVALUE (
tbl_QOL[AvgQOL],
tbl_QOL[IsDateMin], 1,
tbl_QOL[EHRClientFK], ClientFK
)
VAR AvgQOLForMax =
LOOKUPVALUE (
tbl_QOL[AvgQOL],
tbl_QOL[IsDateMax], 1,
tbl_QOL[EHRClientFK], ClientFK
)
RETURN
0 + ( AvgQOLForMax > AvgQOLForMin )
I have the below table and calculating the durations between First_change_Date and Create_date using this DAX formula:
Response_time =
VAR Minutes = DATEDIFF('otrs ticket Response'[otrs ticket.create_time], 'otrs ticket Response'[First_Change_time],MINUTE)
var days =INT(Minutes/1440)
var hourNo=INT(MOD(Minutes,1440) / 60)
var minuteNO=MOD(Minutes,60)
RETURN
CONCATENATE( CONCATENATE( CONCATENATE(days,"d "), CONCATENATE(hourNo, "H ")), CONCATENATE(minuteNO, "m "))
I want to exclude the weekends (Friday, Saturday in my case) and non working hours (5:00pm - 9:00am)
Data:
For my client, I have created a logic. First created a WorkingHoursTable.
Then created a calculated column with the following formula, in the table which has the start and end dateTime's.
Working Hours Between Dates =
var startDate = [yourStartDateTime].[Date]
var startTime = [yourStartDateTime] - startDate
var endDate = [yourEndDateTime].[Date]
var endTime = [yourEndDateTime] - endDate
var firstFullDay = startDate + 1
var lastFullDay = endDate - 1
var inBetweenWorkingHours =
IF(
firstFullDay > lastFullDay,
0,
SUMX(CALENDAR(firstFullDay, lastFullDay), LOOKUPVALUE(WorkingHoursTable[WorkingHoursInAllDay], WorkingHoursTable[WeekDay], WEEKDAY([Date], 2)))
)
var firstDayStart = LOOKUPVALUE(WorkingHoursTable[StartTime], WorkingHoursTable[WeekDay], WEEKDAY(startDate, 2))
var firstDayEnd = LOOKUPVALUE(WorkingHoursTable[EndTime], WorkingHoursTable[WeekDay], WEEKDAY(startDate, 2))
var lastDayStart = LOOKUPVALUE(WorkingHoursTable[StartTime], WorkingHoursTable[WeekDay], WEEKDAY(endDate, 2))
var lastDayEnd = LOOKUPVALUE(WorkingHoursTable[EndTime], WorkingHoursTable[WeekDay], WEEKDAY(endDate, 2))
var effectiveStartTime = IF(startTime < firstDayStart, firstDayStart, startTime)
var effectiveEndTime = IF(endTime > lastDayEnd, lastDayEnd, endTime)
return
IF(
startDate = endDate,
24 * IF(effectiveEndTime > effectiveStartTime, effectiveEndTime - effectiveStartTime, 0),
var firstDayWorkingHour =
24 *
IF(
startTime > firstDayEnd,
0,
firstDayEnd - effectiveStartTime
)
var lastDayWorkingHour =
24 *
IF(
endTime < lastDayStart,
0,
effectiveEndTime - lastDayStart
)
return firstDayWorkingHour + lastDayWorkingHour + inBetweenWorkingHours
)
In this formula you just set the first 4 variables correctly. Then it will calculate total working hours. Unit will be in hours.
Edit: As I see from your post that your weekends are Friday and Saturday, you will need to use WEEKDAY functions slightly different. You can send 1 as a second parameter to WEEKDAY function, instead of 2. You will also need to modify the WeekDay column of WorkingHoursTable.
After this one, you can parse it to '9d 20H 52m' with your formula.
I split the date/time in to date and time columns. I then use a date dimension, where one of the columns is "Is Working Day" = TRUE(), based on which day of the week it is (a simple calculated column). In the time dimension, you do the same to identify "Working Hour" = TRUE(), again, a simple calculation.
Once you have the dimensions in place it then becomes very easy to build your calculations to include / exclude.
I have a question on how to calculate the sum of the last previous working day.
Here is my data set:
Date Name Amount
16/09/20 A 10
17/09/20 A 10
17/09/20 B 30
18/09/20 A 50
21/09/20 A 20
21/09/20 B 60
22/09/20 B 50
In my dashboard, I have a filter to choose a date of display and I see the sum of today; last previous working day and second last previous working day.
So when I put myself at the date 17/09/2020, I should see this:
Sum for D (17/09/2020) -> 40
Sum for D-1 (16/09/2020) -> 10
Sum for D-2 (15/09/2020) -> blank
When I put myself at the date 18/09/2020, I should see this:
Sum for D (18/09/2020) -> 50
Sum for D-1 (17/09/2020) -> 40
Sum for D-2 (16/09/2020) -> 10
When I put myself at the date 21/09/2020, I should see this:
Sum for D (21/09/2020) -> 80
Sum for D-1 (18/09/2020) -> 50
Sum for D-2 (17/09/2020) -> 40
I don't find a way to sum for a previous day using calculate or sum and previousday is not helpful in my case.
Thanks in advance for the help,
I strongly recommend to add a Date dimension to your model. That will make date-based analytics easier.
Here's my first attempt. I assumed only Saturdays and Sundays as holidays. If you need to consider other holidays, you'd need a Date dimension table. Also note that I used ALL(Data) inside the CALCULATE. This will remove any filters applied to the Data table.
Output =
VAR _selectedDate =
MAX ( 'Data'[Date] )
VAR _selectedDateDay =
WEEKDAY ( _selectedDate )
VAR _selectedDateminus2 =
SWITCH (
TRUE,
_selectedDateDay = 2, _selectedDate - 4,
_selectedDateDay = 3, _selectedDate - 5,
_selectedDate - 2
)
RETURN
CALCULATE (
SUM ( 'Data'[Amount] ),
FILTER (
ALL ( 'Data' ),
'Data'[Date] <= _selectedDate
&& 'Data'[Date] >= _selectedDateminus2
)
)
Create these below 3 measures-
D_0 =
CALCULATE(
SUM('your_table_name'[Amount]),
FILTER(
ALL('your_table_name'),
your_table_name[Date] = SELECTEDVALUE(your_table_name[Date])
)
)
D_1 =
CALCULATE(
SUM('your_table_name'[Amount]),
FILTER(
ALL('your_table_name'),
your_table_name[Date] = SELECTEDVALUE(your_table_name[Date]) - 1
)
)
D_2 =
CALCULATE(
SUM('your_table_name'[Amount]),
FILTER(
ALL('your_table_name'),
your_table_name[Date] = SELECTEDVALUE(your_table_name[Date]) -2
)
)
Here is the output when you select date "18/09/20" from the slicer-
I just found a solution, here I post it:
For D:
wd = SUM(data[Amount])
For D-1:
lwd =
var ad = DATEADD(data[Date];0;DAY)
var lwd = IF(
WEEKDAY(ad) = 1; //sunday
ad - 2;
IF(
WEEKDAY(ad) = 2; //monday
ad - 3;
ad - 1 //others
)
)
var sumLWD = CALCULATE(SUM(data[Amount]);data[Date]=lwd)
return sumLWD
For D-2:
l2wd =
var ad = DATEADD(data[Date];0;DAY)
var lwd2 = IF(
WEEKDAY(ad) = 2; //monday
ad - 4;
IF(
WEEKDAY(ad) = 3; //tuesday
ad - 4;
ad - 2 //others
)
)
var sumLWD2 = CALCULATE(SUM(data[Amount]);data[Date]=lwd2)
return sumLWD2
Thanks all for your help and time.
Regards,