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
Related
I have a dataset similar to the following:
Email Subject
Date Received
Month Year received
Red
1 Jan 21
Jan 21
Blue
1 Jan 21
Jan 21
Green
3 Feb 21
Feb 21
Red
5 Feb 21
Feb 21
What I am looking for is a count of distinct email subjects by month however if a subject appears in the previous month then don't count it.
The ideal outcome using the data above would be a:
Total count (No month breakdown) = 3 ( 3 unique subjects - Red, Blue & Green)
Count of subjects for Jan = 2 (Red and Blue)
Count of subjects for Feb = 1 (Only green because Red appeared in the previous month)
This is so I can count how many email threads were received each month without duplicating a thread that spanned over multiple months.
Any help would be greatly appreciated - been stuck for ages!
Assuming you have no date table (if you do, you can adjust the code accordingly), here you go.
Measure =
VAR month = MONTH(MAX('Table'[Date Received]))
VAR year = YEAR(MAX('Table'[Date Received]))
VAR ddate = MAX('Table'[Date Received])
VAR currentTable =
CALCULATETABLE(VALUES('Table'[Email Subject]),
FILTER(ALL('Table'),
MONTH('Table'[Date Received]) = month && YEAR('Table'[Date Received]) = year)
)
VAR excludeTable =
CALCULATETABLE(VALUES('Table'[Email Subject]),
FILTER(ALL('Table'),
'Table'[Date Received] < EOMONTH(ddate,-1))
)
VAR result = EXCEPT( currentTable,excludeTable)
RETURN IF(HASONEVALUE('Table'[Email Subject]),COUNTROWS(result),DISTINCTCOUNT('Table'[Email Subject]))
You can use a measure like this
Measure =
VAR mxp =
MAX ( 'Fact'[Month Year received] )
VAR _cp =
CALCULATETABLE (
VALUES ( 'Fact'[Email Subject] ),
'Fact'[Month Year received] = mxp
)
VAR _ytd =
CALCULATETABLE (
VALUES ( 'Fact'[Email Subject] ),
'Fact'[Month Year received] < mxp
)
VAR _join =
COUNTX ( EXCEPT ( _cp, _ytd ), [Email Subject] )
RETURN
IF (
HASONEVALUE ( 'Fact'[Month Year received] ),
_join,
COUNTROWS ( VALUES ( 'Fact'[Email Subject] ) )
)
mxp gets what is currently visible to DAX
'Fact'[Month Year received] = mxp gets only the current month's unique email subjects.
'Fact'[Month Year received] < mxp gets anything up-to immediately preceding to whatever is currently visible
_join does a left-anti join that brings whatever unique is visible in the current month.
subtotal are handled little differently through IF(HASONEVALUE('Fact'[Month Year received]),_join,COUNTROWS(VALUES('Fact'[Email Subject])))
I have the dataset:
I've checked the occurrence of each id and if it was the first occurrence per the id, I assigned the value 0, otherwise 1.
If I create a pivot table and sum of occurrence, I will get :
So, my final desired outcome is:
I can achieve it with the countrows and sum of countrows as a calculated column but it is static and as soon as I start using the date filter, the formula doesn't work. Is there a way to achieve it with a measure?
Create these below 2 Measures-
Note: Considered ordering using column ticket_id
occ =
VAR current_ticket_id = MIN(your_table_name[ticket_id])
VAR current_id = MIN(your_table_name[id])
VAR count_id =
CALCULATE(
COUNTROWS(your_table_name),
FILTER(
ALL(your_table_name),
your_table_name[id] = current_id
&& your_table_name[ticket_id] <= current_ticket_id
)
)
RETURN
IF(
count_id = 1,
0,
1
)
sum of occ =
CALCULATE(
COUNTROWS(your_table_name),
FILTER(
ALLEXCEPT(your_table_name,your_table_name[id]),
[occ] = 1
)
) + 0
Here is the output-
I have Client Services table with [Billable Minutes] value.
I need to create [Moving Average for 3 months] measure, where the calculation will
only happen - if there are values in all 3 months in [Billable Minutes] exist !
(See below)
If [Billable Minutes] value is blank in at least one month in my Client Services table, then I want [Moving Avg] will show 0 or blank!
My goal:
Client Services table: Result:
Period [Billable Minutes] Period [Moving Avg]
2018-11 200 2019-01 200
2018-12 300
2019-01 100
To reach my goal, I am using the following DAX expression for [Moving Avg 3 months]:
Moving Avg 3 month =
VAR PeriodToUse = DATESINPERIOD('Calendar FY'[Date], LASTDATE('Calendar FY'[Date]), -3, MONTH)
VAR Result = CALCULATE(DIVIDE([Billable Minutes], COUNTROWS ('Calendar FY')), PeriodToUse)
VAR ZeroValue=IF(Minx('Client Services',[Billable Minutes])=0,0,Result)
Return Result
But, unfortunately, my end result is:
Client Services table: Result:
Period [Billable Minutes] Period [Moving Avg]
2018-11 200 2018-11 67
2018-12 300 2018-12 167
2019-01 100 2019-01 200
So, it takes an existing values in Client Services table and divides them by the number of periods.
For example for 2018-11, 2018-10, 2018-09 - it takes (200+0+0)/3=66.6 (67 rounded)
But I need the Moving Avg would be empty for 2018-11, because there are no values for 2018-10, 2018-09 in the Client Services table (same - for 2018-12, should not calculate)
Please, HELP!
Updated:
Here is the solution for this (thx to the answer below)
In order to check if there zero in the selected period, extra measures should be created:
Billable Minutes
Moving Avg Prev Month = Calculate('Client Services'[Billable Minutes],
PREVIOUSMONTH('Calendar FY'[Date]))
Billable Minutes
Moving Avg 2nd Prev Month = Calculate
('Client Services'[Billable Minutes Prev Month],
PREVIOUSMONTH('Calendar FY'[Date]))
Then, when you check whether there are zero values - you need to check it - not
just for [Billable Minutes], but for [Billable Minutes] within 3 months period =
within
[Billable Minutes]+[Billable Minutes Prev Month]+
[Billable Minutes 2nd Prev Month]
See the updated below (worked perfect):
enter
Billable Minutes 3 Months Avg =
VAR PeriodToUse = DATESINPERIOD('Calendar FY'[Date],
LASTDATE('Calendar FY'[Date]), -3, MONTH)
VAR Result = CALCULATE(DIVIDE([Billable Minutes],
COUNTROWS ('Calendar FY')), PeriodToUse)
VAR NMonthsPeriodBlank =
if([Billable Minutes] = BLANK(),0,1) +
if([Billable Minutes Prev Month] = BLANK(),0,1) +
if([Billable Minutes 2nd Prev Month] = BLANK(),0,1)
RETURN IF(NMonthsPeriodBlank < 3, BLANK(), Result)
Follow these below steps-
Step-1: Convert your Period column to Date considering 1st date of each month as below-
Step-2: Get back to report by clicking Close & Apply and create this below 4 measures-
total = SUM('Client Services'[Billable Minutes])
total prev = CALCULATE([total],PREVIOUSMONTH('Client Services'[Period]))
total second prev = CALCULATE([total prev],PREVIOUSMONTH('Client Services'[Period]))
3 month avergae =
VAR devide_by = if([total] = BLANK(),0,1) + if([total prev] = BLANK(),0,1) + if([total second prev] = BLANK(),0,1)
VAR total_amount = [total] + [total prev] + [total second prev]
RETURN IF(
devide_by < 3,
BLANK(),
total_amount/devide_by
)
Here is the final output-
====================
Solution in single measure
====================
You can convert all measures into 1 measure as below-
3 month average new =
VAR this_month = SUM('Client Services'[Billable Minutes])
VAR prev_month =
CALCULATE(
SUM('Client Services'[Billable Minutes]),
PREVIOUSMONTH('Client Services'[Period])
)
VAR second_prev_month =
CALCULATE(
SUM('Client Services'[Billable Minutes]),
PREVIOUSMONTH(
DATEADD(
'Client Services'[Period],
-1,
MONTH
)
)
)
VAR devide_by =
if(this_month = BLANK(),0,1) +
if(prev_month = BLANK(),0,1) +
if(second_prev_month = BLANK(),0,1)
VAR total_amount = this_month + prev_month + second_prev_month
RETURN IF(
devide_by < 3,
BLANK(),
total_amount/devide_by
)
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,
I want to calculate the sum of MTD sales per day for the same period of last year, only until a specific day - the day of the last sales transaction.
So for example, if the filter is year= 2019 and month= 2, I need the MTD sales for february 2018 until the 5th, calculated day by day:
MTDSales=
VAR MyDay = 5
RETURN
CALCULATE(
TOTALMTD(SUM(Sales); Calendar[Date]);
SAMEPERIODLASTYEAR(Calendar[Date]);
//here I need another filter to stop on the 5th!
)
Edit:
Please have a look at this link to see the sample data.
The measures I'm trying to build are:
Sales MTD CY
Sales MTD LY
Sales MTD CY*
Sales MTD LY*
Sales MTD CY**
Sales MTD LY**
Thanks for helping!
I'm assuming that you are using 5 since today is February 5th.
You can get MTDSales for the current month like this:
MTDSales =
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(TODAY(), - 1) + 1, TODAY() )
RETURN CALCULATE( SUM( Sales ), DateRange )
To match that up with the previous year, just use SAMEPERIODLASTYEAR.
LastYearMTDSales =
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(TODAY(), - 1) + 1, TODAY() )
RETURN CALCULATE( SUM( Sales ), SAMEPERIODLASTYEAR(DateRange) )
If you want to use a different date than TODAY, just specify that date as a variable and pass it into the DateRange variable where TODAY appears.
If you want to find the MTDSales up to the 5th day of the month (assuming you have the month in your filter context), try this
MTDSales =
Var MyDay = 5
VAR MyDate = MIN( Calendar[Date] ) + MyDay - 1
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(MyDate, -1) + 1, MyDate )
RETURN CALCULATE( [Sum of Sales], DateRange )
Then for the previous year, you can reuse that measure, but shifted:
PrevYearMTDSales =
CALCULATE( [MTDSales], ALL( Calendar ), SAMEPERIODLASTYEAR( Calendar[Date] ) )
Edit: After looking at your PBIX, I realized that I had made the wrong assumption about your filter context. Since you are looking to write a measure at the date level, try this instead:
Sales MTD CY =
VAR MyDay = 5
VAR CurrentDate = MAX ( 'Calendar'[Date] )
VAR MyDate = EOMONTH ( CurrentDate, -1 ) + MIN ( MyDay, DAY ( CurrentDate ) )
RETURN
CALCULATE (
TOTALMTD ( SUM ( Sales[Sales] ), 'Calendar'[Date] ),
FILTER ( 'Calendar', 'Calendar'[Date] <= MyDate )
)
The previous year measure can still be done referencing this measure and shifting.
Replace my column names for yours and it should work
The formula remains the same:
MTDSales =
VAR MyDay = 5
RETURN
CALCULATE(
TOTALMTD([total sales], 'Calendar'[DateKey]),
SAMEPERIODLASTYEAR('Calendar'[DateKey]),
FILTER(
ALL(Sales),
Sales[DateKey] >= STARTOFMONTH('Calendar'[DateKey]) && Sales[DateKey] <= DATEADD(STARTOFMONTH(Sales[DateKey]),MyDay-1,DAY)
)
)