How to calculate headcount in PowerBI - powerbi

I have some problem with the HR Dashboard, where I can't get it to work properly.
I have the following visuals:
Visual for HR Dashboard
The headcount code is as follow:
Headcount = CALCULATE (
DISTINCTCOUNT ( FactTable[EmpID]),
FILTER ( ALL(FactTable), FactTable[EmploymentStatus] = "Active" )
)
The problem is, that the visual doesn't slice on the seniority.
Need help.

I have try with some sample data, i guess the issue here is due to you are using filter function, you can try my method if the problem can be solved:
1)I have this original sample table
I have created a measure to count the total employee still with company
Total count = CALCULATE(COUNT(Sheet1[ID]),Sheet1[Status] = "active")
3)My scorecard has displayed the total count correct for with junior slicer

First its bad maner to write code like this:
CALCULATE (
DISTINCTCOUNT ( FactTable[EmpID] ),
FILTER (
ALL ( FactTable ),
AND (
FactTable[DateofHire] <= MIN ( 'Kaldt funktion'[Date] ),
OR (
FactTable[DateofTermination] = BLANK (),
FactTable[DateofTermination] >= MAX ( 'Kaldt funktion'[Date] )
)
)
)
)
A good practice is to use ALL with columns that you really need in your filter. Second instead of comparing column = BLANK() use function ISBLANK(Table[ColumnName]). You also should compare SELECTEDVALUE to get the correct data. (unfortunately, you don't post any samples, and I don't know how your data looks like I can only guess). How is correlated 'Kaldt funktion' with FactTable? Probably problem is in your MIN and MAX. We can also use && insted of AND, and || insted of OR;
CALCULATE (
DISTINCTCOUNT ( FactTable[EmpID] ),
FILTER (
ALL ( FactTable[DateofHire], FactTable[DateofTermination] ),
SELECTEDVALUE ( FactTable[DateofHire] ) <= MIN ( 'Kaldt funktion'[Date] )
&& (
ISBLANK ( FactTable[DateofTermination] )
|| SELECTEDVALUE ( FactTable[DateofTermination] ) >= MAX ( 'Kaldt funktion'[Date] )
)
)
)

Related

DAX - Find and return the category value with the highest summarized measure

In the following example,
Highest Shipping Unit - should be the "ActualArea" where TotalPS has the maximum value.
This is a summarized table grouped by - Customer_ID, ProductDesignation,Package_CD,ActualArea
As you can see above I cannot produce the desired results. The DAX for HighestShippingUnit measure is as follows. I can't seem to figure out what I'm doing wrong.
HighestShippingUnit =
VAR tab =
SUMMARIZE (
ALLEXCEPT (
DeviationReport,
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD]
),
DeviationReport[ActualArea],
"GroupTotalPS", SUM ( DeviationReport[TotalPS] )
)
VAR maxps =
MAXX ( tab, [GroupTotalPS] )
RETURN
CALCULATE (
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), MAX ( [ActualArea] ) )
)
DeviationReport is the name of my table in the dataset.
Please can you tell me what I'm doing wrong in my DAX or how to do it in a more efficient manner?
You were actually very close with your attempt.
If you change the final line from
CALCULATE (
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), MAX ( [ActualArea] ) )
)
to
MAXX ( FILTER ( tab, [GroupTotalPS] = maxps ), [ActualArea] )
then it should work.
The reason is that using CALCULATE re-introduces the ActualArea filter context that you removed with ALLEXCEPT so that you only see the current ActualArea.
Thanks to #Alexis Olson. I managed to fix the problem.
The DAX is as follows -
HighestShippingUnit =
VAR tab =
SUMMARIZE (
ALLEXCEPT (
DeviationReport,
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD]
),
DeviationReport[CUSTOMER_ID],
DeviationReport[ProductDesignation],
DeviationReport[PACKAGE_CD],
DeviationReport[ActualArea],
"GroupTotalPS", SUM ( DeviationReport[TotalPS] )
)
RETURN
MAXX(TOPN(1, tab, [GroupTotalPS]), [ActualArea])
)

DAX subquery measure?

I'm struggling with a DAX query and wondered if you could help?
Consider this table (or visualisation) called 'Builds':
Build....App....Status
Build1...App1...UAT
Build1...App2...Complete
Build2...App1...Complete
Build2...App2...Complete
I would like to add a measure column called 'AppsOutstanding' to show a count of Apps for that Build that aren't 'Complete'. Like so:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........1
Build1...App2...Complete....1
Build2...App1...Complete....0
Build2...App2...Complete....0
I almost need to do a 'subquery' measure!? Something like:
SELECT COUNT(Status) FROM Builds
WHERE Build = [The Build In This Row]
AND Status <> 'Complete'
I'm a bit stumped how to translate this into DAX? Here is my unsuccessful attempt:
AppsUnavailable = CALCULATE (
count(Builds[Build]),
CALCULATETABLE (
SUMMARIZE ( Builds,Builds[Status] ),
Builds[Status] <> "Complete"
))
Thanks in advance!
UPDATE
I've tried this, but the count isn't working, and also this DAX filters "Complete" statuses out of my actual results! Which i don't want. I only want to filter the "Complete" statuses out of my count measure....
AppsUnavailable =
CALCULATE (
COUNT ( Builds[Build] ),
FILTER (
ALL ( Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
),
FILTER (
ALL ( Builds[Status] ),
Builds[Status] <> "Complete"
)
)
UPDATE 2
I think I'm doing something fundamentally wrong. I've really dumbed it down to just find other 'Builds' with the same name, and it still only returns 1's!
AppsUnavailable =
CALCULATE (
COUNT ( Builds[Build] ),
FILTER (
ALL ( Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
)
)
UPDATE 3
This query (when testing on a single table (no joins) sample) produces this:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........1
Build1...App2...Complete....0
Build1...App2...UAT.........1
Build2...App1...Complete....0
Build2...App2...Complete....0
But i actually need this:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........2
Build1...App2...Complete....0
Build1...App2...UAT.........2
Build2...App1...Complete....0
Build2...App2...Complete....0
So Build1 has 2 Apps that are not complete.
I then need to look into why I'm getting all 1's in the 'live' environment. It must be to do with filtering on 2 tables as opposed to 1....
You can replace [The Build In This Row] with SELECTEDVALUE ( Builds[Build] ).
Try this:
AppsUnavailable =
CALCULATE (
COUNT ( Builds[App] ),
FILTER (
ALL ( Builds[Status], Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
&& Builds[Status] <> "Complete"
)
) + 0
powerBI Table visualization internal use SUMMARIZECOLUMNS which is removed row without data from the measure. You have 2 options:
-Right click on GroupBy Column -> "Show item with no data"
-Add +0 to calculation after the last parenthesis
UPDATE:
CALCULATE (
COUNT ( YourTable[App] ),
FILTER (
ALL ( YourTable[App], YourTable[Build], YourTable[Status] ),
YourTable[Build] = SELECTEDVALUE ( YourTable[Build] )
&& YourTable[Status] <> "Complete"
)
) + 0

Previous Day in Power BI

This may be a really simply one but cannot get this to work.
Invitations_Daily =
CALCULATE (
DISTINCTCOUNTNOBLANK ( AppUser[UserId] ),
USERELATIONSHIP ( AppUser[DateInvitedID], 'Date'[DateId] ),
FILTER (
'Date',
'Date'[DateId]
= SELECTEDVALUE ( 'DateSelector'[DateId], MAX ( 'DateSelector'[DateId] ) )
)
) + 0
I have a measure and want the previous days value. When I put in a -1 then ))+0 it gives me 0, instead of the previous day value. Is there something obvious I am missing or is there something else wrong with my data model etc ?

Running total of a measure which use DATESINPERIOD and months from fact table

I am using a measure below to display the months from fact table as described here:
Billings12Months =
CALCULATE (
SUM ( 'Datatable'[Allowable] ),
DATESINPERIOD ( DimDate[Date], MIN ( DimDate[Date] ), +12, MONTH )
)
My attempt to get the running total of above measure is failing:
BillingsRunningTotal =
CALCULATE (
[Billings12Months],
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
BillingsRunningTotal2 =
SUMX (
FILTER (
ALLSELECTED ( DimDate[Date] ),
DimDate[Date] <= MAX ( ( DimDate[Date] ) )
&& YEAR ( DimDate[Date] ) = YEAR ( MIN ( DimDate[Date] ) )
),
[Billings12Months]
)
[BillingsRunningTotal] return same values as [Billings12Months] (please see screen 1 attached) and
[BillingsRunningTotal2] return wrong values and month start from Jan, 17 instead of May, 17 (please see screen-2)
Please help me to calculate the running total. If possible please describe how your solution is working so that I can be better in DAX.
Update:
Please see the screen-3 below for the output when I use the measure suggested by Kosuke:
BillingsRunningTotal =
CALCULATE (
SUM ( Datatable[Allowable] ),
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
The months are from fact table (not from a Date table) and I think DATESINPERIOD plays a role to calculate and display the months. When we use SUM ( Datatable[Allowable] ), there would be a single month as dictated by the slicer. So we need to use DATESINPERIOD with rolling month calculation logic (DimDate[Date] <= MAX ( DimDate[Date] )) or virtually sum the [Billings12Months], It is where I am failing.
Thanks
You are almost there with the first attempt, however what to calculate is not [Billings12Months], but SUM( Datatable[Allowable] ).
BillingsRunningTotal =
CALCULATE (
SUM ( Datatable[Allowable] ),
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
Essentially, [Billings12Months] and [BillingsRunningTotal] are same in calculating the sum of Datatable[Allowable], but the only difference is each measure calculates for different scope of period. Therefore, the right way of thinking is to wrap SUM ( Datatable[Allowable] ) in CALCULATE, with different filter parameters.

Is there a way to dynamically switch which relationship is active in powerpivot (based on slicer selection)?

I have a fact table (MORT) and a dimension table (GEO) in PowerPivot (2016). There are two joins between the tables with two columns in MORT relating to one column in GEO. We can call one join RES and the other REG. I have a large number of measures and I want to allow the user to quickly change between seeing the measures using the RES and REG relationships - essentially I want to be able to switch the active relationship (referred to as switch in the code). This would probably be based on a detached slicer.
I have tried nesting an if statement and various ways of defining a variable to use in a CALCUATE but either have the error that USERELATIONSHIP can only be used in a calculate (in the case of the if) or that USERELATIONSHIP only accepts a column reference (in the case of the var).
IF STATEMENT
MEASURE:= CALCULATE(COUNT(MORT[ID]), GEO,
IF(LASTNONBLANK(SWITCH,1)="RES",
USERELATIONSHIP(MORT[RES], GEO[AREA]),
USERELATIONSHIP(MORT[REG], GEO[AREA]))
VAR
MEASURE:=
VAR switch = IF(LASTNONBLANK(Switch,1)="RES", MORT[RES], MORT[REG])
RETURN
CALCULATE(COUNT(MORT[ID]), GEO, USERELATIONSHIP(switch, GEO[AREA])
I could create every measure with an if statement at the start to check the value of switch but this creates a lot of duplicate code.
I want some way for the end user to change the active relationship in a measure but ideally without a lot of duplicated code.
How about like this?
MEASURE :=
IF (
LASTNONBLANK ( Switch, 1 ) = "RES",
CALCULATE ( COUNT ( MORT[ID] ), GEO, USERELATIONSHIP ( MORT[RES], GEO[AREA] ) ),
CALCULATE ( COUNT ( MORT[ID] ), GEO, USERELATIONSHIP ( MORT[REG], GEO[AREA] ) )
)
Edit:
In your comment, you said you tried this:
M :=
VAR calc =
FILTER ( MORT, MORT[CAUSE] >= "I" && 'Remake Data'[CAUSE] <= "I9" )
VAR select =
IF (
HASONEVALUE ( 'SWITCH'[Switch] ),
LASTNONBLANK ( 'SWITCH'[Switch], 1 ),
"ERROR"
)
RETURN
IF (
select = "RES",
CALCULATE ( COUNTROWS ( calc ), USERELATIONSHIP ( MORT[RES], GEO[Area] ) ),
IF (
selection = "REG",
CALCULATE ( COUNTROWS ( calc ), USERELATIONSHIP ( MORT[REG], GEO[Area] ) ),
"ERROR"
)
)
There are a few problems with this, but mainly, if you define something as a variable, then it's constant and won't be affected by other things within a CALCULATE. Try the following instead.
Define a new measure:
CountRowsMeasure =
COUNTROWS ( FILTER ( MORT, MORT[CAUSE] >= "I" && 'Remake Data'[CAUSE] <= "I9" ) )
Then use that measure within your M measure:
M =
VAR select = SELECTEDVALUE( 'SWITCH'[Switch], "ERROR" )
RETURN
SWITCH (
select,
"RES", CALCULATE ( [CountRowsMeasure], USERELATIONSHIP ( MORT[RES], GEO[Area] ) ),
"REG", CALCULATE ( [CountRowsMeasure], USERELATIONSHIP ( MORT[REG], GEO[Area] ) ),
"ERROR"
)