Use slicer to filter PATHCONTAINS - powerbi

So I have a table with employees. Each employee has a manager, and using the PATH function, I can create another column, that traces the management of each employee to the CEO (using employee ID and manager's ID.) For example, if my boss reports to the ceo, my employee path would be "CEO ID | My boss ID | My ID" or "10001234|10002345|1000456."
Once I have this path selected, I need to be able to filter my data based on a list of people (business leaders.) If I select business leader A, then I want my data to only include people who have business leader A in their employee path, i.e., people who eventually report to him. I have a separate table of business leaders. Here's what Im doing:
Create a path for each employee: empPath = PATH(EmployeeTable[Employee ID], EmployeeTable[Manager ID])
Use business leader table as a slicer (using their IDs)
Create a variable, selectedLeader, that records the value of whatever selection has been made from the slicer: selectedLeader = IF(HASONEVALUE('LeaderTable'[Id]),VALUES('LeaderTable'[Id]),BLANK())
Put it all together using PATHCONTAINS to filter assign a binary indicator that will allow us to filter the data:
Filter =
var selectedLeader = IF(HASONEVALUE('LeaderTable'[Id]),VALUES('LeaderTable'[Id]),BLANK())
var empPath = PATH(EmployeeTable[Employee ID], EmployeeTable[Manager ID])
return IF(PATHCONTAINS(empPath, selectedLeader ),1,0)
This however, gives the error: Calculation error in measure: A table of multiple values was supplied where a single value was expected.
I've been trying to play around with the formula a lot, using VALUES, FIRSTNONBLANK and other aggregating functions on employeePath, but none are working as desired. Any help appreciated!

The issue is that you need row context for your PATH function to work as you're expecting. As you have it written, it doesn't know which row of the EmployeeTable you're referring to with empPath.
Try this instead:
Filter =
VAR selectedLeader = SELECTEDVALUE ( 'LeaderTable'[Id] )
VAR FilteredTable =
FILTER (
EmployeeTable,
PATHCONTAINS (
PATH ( EmployeeTable[Employee ID], EmployeeTable[Manager ID] ),
selectedLeader
)
)
RETURN
IF ( ISEMPTY ( FilteredTable ), 0, 1 )
You can make this more efficient if you define a calculated path column on the EmployeeTable first.
Path = PATH ( EmployeeTable[Employee ID], EmployeeTable[Manager ID] )
Then the measure simplifies to:
Filter =
VAR selectedLeader = SELECTEDVALUE ( 'LeaderTable'[Id] )
VAR FilteredTable =
FILTER ( EmployeeTable, PATHCONTAINS ( EmployeeTable[Path], selectedLeader ) )
RETURN
IF ( ISEMPTY ( FilteredTable ), 0, 1 )

Related

How to ignore slicer for particular measure?

My data is
I'd like to block measure (sum of amount) with one filter Choose = "Home"
When I change slicer on "Not Home" my data change.
I don't want it. When I click "Not home" I want to have same values like I click "Home"
I can't use Edit interactions because I want to add new columns that they will be depended to slicer. I'd like to apply it to only one maesure.
I tried calculate(sum(amount), filter(choose = "Home").
It doesn't work.
Data still change.
What should I do?
Wouldn't this work?
Sum of Amount =
CALCULATE (
SUM ( 'Table'[Amount] ),
'Table'[Choose] = "Home"
)
I think in your original measure you forgot ALL ( 'Table'[Choose] ). The above measure is semantically identical to:
Sum of Amount =
CALCULATE (
SUM ( 'Table'[Amount] ),
FILTER (
ALL ( 'Table'[Choose] ) ,
'Table'[Choose] = "Home"
)
)
I don't have a Windows box to test this simple model, but perhaps you also need to invoke KEEPFILTERS:
Sum of Amount =
CALCULATE (
SUM ( 'Table'[Amount] ),
KEEPFILTERS ( 'Table'[Choose] = "Home" )
)

PowerBI report cannot recognize If statement when string are compared

I'm trying to replicate the example presented in this youtube tutorial
https://www.youtube.com/watch?v=z9ttZAZkEhs
However, even if I use the same DAX code the controls do not recognize the values properly.
Selected = calculate (DimProduct[uniqueCustomer], treatas( Values(Products[Name]), DimProduct[EnglishProductName] ) )
I tried different ways to recognize the values coming from the slicer, but they simply do not work.
CheckColumn = if (trim(DimProduct[EnglishProductName]) = trim(DimProduct[SelectedNumber2]),true,false)
I have attached the example file that I'm using.
https://github.com/gabrielacosta/TestPowerBiSlicer/blob/main/testslicer.pbix
Does anyone know what could be the issue.
I guess your calculation should look like the formula below but it doesn't make much sense doing that calculation.
Selected =
CALCULATE (
[uniqueCustomer],
TREATAS ( VALUES ( Products[Name] ), DimProduct[EnglishProductName] ),
VALUES ( DimProduct[EnglishProductName] )
)
For this specific scenario just using SELECTEDVALUE is better
Selected2 =
VAR SelectedProduct =
SELECTEDVALUE ( Products[Name] )
VAR CountCalc =
CALCULATE (
[uniqueCustomer],
FILTER ( DimProduct, [EnglishProductName] = SelectedProduct )
)
RETURN
CountCalc

Power BI - DAX - table variable - use column for further computation

I am using DAX language in Power BI Desktop.
I have a tricky situation where I am trying to use the column name generated from a table variable.
Table 1: SourceTable
Table 2: ReferenceTable
I need to develop a calculated column in SourceTable called EmploymentStatus, based on the corresponding column in ReferenceTable. But I need only the EmploymentStatus value from ReferenceTable, for the maximum InternalID for a given EmployeeEmail.
For example, for the email xyz.gmail.com in SourceTable, I need the EmploymentStatus (calculated column) as 'Active' from ReferenceTable, since 'Active' has the maximum of the two available InternalID values (17, 15).
I tried the following code (Calculated Column in SourceTable):
EmploymentStatus_SourceTable_CalculatedColumn =
VAR tabl1 =
SUMMARIZE (
ReferenceTable,
ReferenceTable[EmployeeEmail],
"MaxInteralID", MAX ( ReferenceTable[InternalID] )
)
VAR tabl2 =
FILTER (
ReferenceTable,
ReferenceTable[InternalID] IN VALUES ( tabl1[MaxInteralID] )
)
VAR NewCol =
LOOKUPVALUE (
tabl2[EmploymentStatus],
tabl2[EmployeeEmail], SourceTable[EmployeeEmail]
)
RETURN
NewCol
I realize that I cannot use the column generated from the table variable.
For example, tabl1[MaxInteralID], tabl2[EmployeeStatus], tabl2[EmployeeEmail] - are all invalid.
Any idea on how to handle this? You can even provide me with a solution that does not use variables at all. Am okay with any solution.
Similar to here, you can find the maximal ID for each email and look up the status for that ID.
Table and column names shortened for readability:
CalcCol =
VAR Email = Source[Email]
VAR MaxID = CALCULATE ( MAX ( Ref[ID] ), Ref[Email] = Email )
RETURN
LOOKUPVALUE ( Ref[Status], Ref[Email], Email, Ref[ID], MaxID )

DAX Count Max Value By Group

I have a survey that is sent to companies periodically and each new survey is given a collection #. I'm trying to create a column that tells me the maximum collection # for each company code. This will vary by company because not every company responds to every survey.
LastCollection = (FILTER('Dynamic', 'Dynamic'[Collection]
=CALCULATE(max('Dynamic'[Collection]),ALLEXCEPT('Dynamic','Dynamic'[Company Name])))
The error I'm getting is that the expression refers to multiple columns. I attempted to wrap it in AVERAGE but that didn't help.
You should just be able to do something along the lines of the below.
LastCollection =
VAR companyName = 'Dynamic'[Company Name]
RETURN CALCULATE( MAX ('Dynamic'[Collection] ),
FILTER ( 'Dynamic', 'Dynamic'[Company Name] = companyName ))
I'm not sure why you have the CALCULATE inside a FILTER since a FILTER returns a table rather than a single value.
This should be simpler:
LastCollection =
CALCULATE (
MAX ( 'Dynamic'[Collection] ),
ALLEXCEPT ( 'Dynamic', 'Dynamic'[Company Name] )
)

Calculated Column: Distinct id# should return first date and ignore other dates

I have the requirement in which id# has duplicate records and also has duplicate received_date, where I need to show only unique received date for each id#. Could you please help me on how to resolve this?
Data sample shown below:
I have tried the following in the calculated column
expected_date_or_result =
VAR selected_id = test[id#]
VAR distinct_received_date =
CALCULATE (
FIRSTDATE ( test[received_date] ),
FILTER ( test, test[id#] = selected_id )
)
RETURN
distinct_received_date
I am not sure now to add blanks in case of duplicate received_date.
Please help me with this.
Note: I cannot use remove duplicate option since it is affecting my column group
There are likely many ways to approach this but here's the first one that comes to my mind:
expected_date_or_result =
VAR TopRow =
TOPN (
1,
FILTER ( test, test[id#] = EARLIER ( test[id#] ) ),
test[received_date], ASC,
test[group], ASC
)
RETURN
MAXX (
FILTER ( TopRow, test[group] = EARLIER ( test[group] ) ),
test[received_date]
)
This picks the top row of the table filtered by id# and sorted by received_date and group and then filters that row so that it's only non-empty if the group is the top one and extracts the received_date column using MAXX.