I'm trying to get count of active employees by date that can be filtered by department.
The following is what my Employees data more or less looks like. Additionally, there's a dim_Department table connected to the main Employees table and a TERMINATED(Y/N) column. I found a similar case here (without the departments and terminated(y/n) though):
I used the following DAX expression to get a table of total number of employees (organization-wide), but I'm not sure how to filter this metric using the slicer showing departments.
CountOfActive =
var _selectedDate = MAX('Calendar'[Date])
return
CALCULATE(COUNTROWS('employee'), filter(ALL(employee), employee[Hire Date] <= VALUE(_selectedDate) && (employee[Termination Date] >= VALUE(_selectedDate) || ISBLANK(employee[Termination Date]))))
CountOfTerminated =
var _selectedDate = MAX('Calendar'[Date])
return
CALCULATE(COUNTROWS('employee'), filter(ALL(employee), employee[Hire Date] <= VALUE(_selectedDate) && (employee[Termination Date] < VALUE(_selectedDate) )))
How do I get my department slicer to filter the results of this column? Or should I use a different expression, or make a column/table for each department?
we have answered that before i guess...
Unique Count
Unique Count =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
)
Unique Count (Active)
Unique Count (Active) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Active"
)
Unique Count (Terminated)
Unique Count (Terminated) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Terminated"
)
also you can insert your data into that sample file and see if it works...
Related
I am trying to create a List for New Customers Added during the year and List of Lost Customers, I have written a DAX which works fine in summary count but doesn't work in table matrix.
NTB =
VAR currentCustomers =
VALUES ( Deposits[CIF ID] )
VAR currentDate =
MAX ( Deposits[Source.Date] )
VAR pastCustomers =
CALCULATETABLE (
VALUES ( Deposits[CIF ID] ),
ALL (
Deposits[Source.Date].[Month],
Deposits[Source.Date].[MonthNo],
Deposits[Source.Date].[Year]
),
Deposits[Source.Date] < currentDate
)
VAR newCustomers =
EXCEPT ( currentCustomers, pastCustomers )
RETURN
COUNTROWS ( newCustomers )
Total Row is correct, even if I remove one function the table remains same..
Appreciate your help
try this :
Modelling --> Add Table
Table =
VAR _max =
MAX ( Deposits[Source.Date] )
RETURN
ADDCOLUMNS (
SUMMARIZE ( Deposits, Deposits[CIF ID] ),
"NTB",
CALCULATE (
COUNT ( Deposits[CIF ID] ),
FILTER (
ALLEXCEPT ( Deposits, Deposits[CIF ID] ),
Deposits[Source.Date] >= _max
)
),
"Lost Customers",
CALCULATE (
COUNT ( Deposits[CIF ID] ),
FILTER (
ALLEXCEPT ( Deposits, Deposits[CIF ID] ),
Deposits[Source.Date] < _max
)
)
)
My database schema looks like down below
ID
Date
Status
ID1
2022/01/01
Active
ID1
2022/02/01
Active
ID1
2022/03/01
Active
ID1
2022/04/01
Terminated
ID2
2022/01/01
Active
ID2
2022/02/01
Terminated
I'd like to calculate unique occurrences from start of selected date year, till the selected date. My formula is:
CountOfUnique = CALCULATE( DISTINCTCOUNT( 'Table'[ID] ) , 'Table'[STATUS] = "Active", DATESBETWEEN('CALENDAR'[DATE], STARTOFYEAR('CALENDAR'[DATE]), MAX('CALENDAR'[DATE]) ))
In SQL I'd need something like
SELECT COUNT ( DISTINCT ID) FROM Table
WHERE STATUS = "ACTIVE"
AND DATE BETWEEN 2022/01/01 AND 2022/04/01
Try this:
CountOfUnique =
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID] ),
'Table'[STATUS] = "Active",
DATESBETWEEN (
'CALENDAR'[DATE],
STARTOFYEAR ( 'CALENDAR'[DATE] ),
SELECTEDVALUE ( 'CALENDAR'[DATE] )
)
)
when you have a slicer on the visual, the start of selected date year doesnt mean much as you select the dates on the slicer. I created a Calendar Table = CALENDARAUTO() so it started from the 2022/01/01...
use one of these as you like...
sample PBix File
Unique Count =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
)
or only Active if you need
Unique Count (Active) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Active"
)
Source Data:
DAX:
Total Derived =
VAR selected_min_date =
MIN ( 'Date'[Date] )
VAR selected_max_date =
MAX ( 'Date'[Date] )
VAR vTable =
SUMMARIZE (
FILTER (
MyTable,
MyTable[PDate] >= selected_min_date
&& MyTable[PDate] <= selected_max_date
),
MyTable[Gcode],
MyTable[DerTax]
)
var result = sumx(vTable, MyTable[DerTax])
RETURN
Result
I was expecting result as 2644.48, but is outputting 1322.24
What am doing wrong.
I was same result same as with below sql.
Select GCode,
sum(DerTax)
from MyTable
where PDate >= #datemin and PDate <=#datemax
group by GCode
I don't think you've grasped how SUMMARIZE works. Since your Gcode and DerTax entries are identical for each of the two rows, your SUMMARIZE statement will generate a table comprising just a single row, viz:
Gcode
DerTax
Grp01
1322.24
Instead of passing DerTax as a GroupBy column, you should be passing an aggregation of that field:
Total Derived: =
VAR selected_min_date =
MIN( 'Date'[Date] )
VAR selected_max_date =
MAX( 'Date'[Date] )
VAR vTable =
SUMMARIZE(
FILTER(
MyTable,
MyTable[PDate] >= selected_min_date
&& MyTable[PDate] <= selected_max_date
),
MyTable[Gcode],
"Tax", SUM( MyTable[DerTax] )
)
VAR result =
SUMX(
vTable,
[Tax]
)
RETURN
Result
How can I map or count new items that were also in my data the previous month?
For example:
ID / date / item
123 / 01.12.20 / one
123 / 01.11.20 / one
143 / 01.11.20 / two
153 / 01.12.20 / three
Will get:
123 / one
It looks like the Item is the description of the ID.
We can write a measure that shows the Item if it was also present in the preious month or blank otherwise. This way, creating a Table visual in Power BI with the ID and this measure we will just see the IDs and Items that where present in the previous month.
Item if in previous month =
IF(
HASONEVALUE( T[Item] )
&& HASONEVALUE( T[ID] ),
VAR currentMonth =
MONTH(
MAX( T[Date] )
)
VAR currentItem =
SELECTEDVALUE( T[Item] )
RETURN
IF(
NOT ISEMPTY(
FILTER(
ALL( T ),
MONTH( T[Date] ) = CurrentMonth - 1
&& T[Item] = currentItem
)
),
currentItem
)
)
This works if we have two months of data, like in your question. If we have more than two months, then maybe that what we really want is to check the previous month of the last one.
Item if in previous to last month =
VAR LastMonth =
MONTH(
MAXX(
ALL( T ),
T[Date]
)
)
RETURN
IF(
HASONEVALUE( T[Item] )
&& HASONEVALUE( T[ID] )
&& MONTH(
MAX( T[Date] )
) = LastMonth,
VAR currentItem =
SELECTEDVALUE( T[Item] )
RETURN
IF(
NOT ISEMPTY(
FILTER(
ALL( T ),
MONTH( T[Date] ) = LastMonth - 1
&& T[Item] = currentItem
)
),
currentItem
)
)
I am using a matrix table in powerbi to show previous week totals for different areas (categories). I have the majority of it working but I am not able to correctly get the subtotals on the table working.
I believe it has to do with the filtering that I am using - i have been unable to correct it.
screen capture
As you can see my Total for week 24 previous is missing
Dax code is:
VAR Area =
MAX ( 'SumTable'[Area Name] )
VAR CurrentWeek =
SELECTEDVALUE ( SumTable[WeekofYear] )
VAR CurrentYear =
SELECTEDVALUE ( SumTable[Year] )
VAR MaxWeekNumber =
CALCULATE ( MAX ( SumTable[WeekofYear] ), ALL ( SumTable ) )
RETURN
IF (
HASONEVALUE ( SumTable[Area Name] ),
SUMX (
FILTER (
ALL ( SumTable ),
IF (
CurrentWeek = 1,
SumTable[WeekofYear] = MaxWeekNumber
&& SumTable[Year] = CurrentYear - 1
&& SumTable[Area Name] = Area,
SumTable[WeekofYear] = CurrentWeek - 1
&& SumTable[Year] = CurrentYear
&& SumTable[Area Name] = Area
)
),
SumTable[BOE]
),
SUMX (
FILTER (
ALLSELECTED ( SumTable ),
IF (
CurrentWeek = 1,
SumTable[WeekofYear] = MaxWeekNumber
&& SumTable[Year] = CurrentYear - 1,
SumTable[WeekofYear] = CurrentWeek - 1
&& SumTable[Year] = CurrentYear
)
),
SumTable[BOE]
)
)
Data Table:
Example Table Format
Thank you, first time poster!
B
I would start by spliting my data table from my date table.
And I guess you don't need to ALL the whole table, just the columns for year and weeknumber and keep the Area in context, that way you don't have to bother if HASONEVALUE it will just work.
SELECTEDVALUE only returns if only a single value for that column is in context, not the case for totals and subtotals.
MyMeasure =
VAR CurrentWeek =
MAX( SumTable[WeekofYear] )
VAR CurrentYear =
MAX( SumTable[Year] )
VAR MaxWeekNumber =
CALCULATE ( MAX ( SumTable[WeekofYear] ), SumTable[Year] = CurrentYear-1 )
RETURN
IF(
CurrentWeek = 1,
CALCULATE(
SUM(SumTable[BOE]),
FILTER (
ALL ( SumTable[Year],SumTable[WeekofYear]),
SumTable[WeekofYear] = MaxWeekNumber
&& SumTable[Year] = CurrentYear - 1
)
),
CALCULATE (
SUM ( SumTable[BOE] ),
FILTER (
ALL ( SumTable[WeekofYear] ),
SumTable[WeekofYear] = CurrentWeek-1
)
)
)
I did not have a chance to confirm this code.