DAX Count Max Value By Group - powerbi

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] )
)

Related

Use slicer to filter PATHCONTAINS

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 )

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 )

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.

Power BI - Getting the most recent value from a related table

I know this must be extremely simple, but every example I can find online only works within a single table. I've simplified my situation to these two tables:
I want to add a calculated column to the first table, showing the most recent value for that id. It also needs to work with text.
There are a variety of ways to do this kind of thing as I've explained before and all of the solutions there can be adjusted to work in this case.
Doing this as a calculated column and with a second table, you need to make sure you are using row context and filter context appropriately.
Here's are a couple different possibilities I think may work:
MostRecentValue =
MAXX ( TOPN ( 1, RELATEDTABLE ( Table2 ), Table2[date] ), Table2[value] )
In this one, RELATEDTABLE is doing the work of filtering Table2 to only the rows where id matches Table1.
MostRecentValue =
VAR PrevDate = CALCULATE ( MAX ( Table2[date] ) )
RETURN CALCULATE ( MAX ( Table2[value] ), Table2[date] = PrevDate )
The relationship is more subtle here. Wrapping the MAX in CALCULATE forces a context transition so that the row context (which includes id) is applied to Table2 as filter context.

DAX - Stuck on dynamic MAXX column formula

I'm working in Power BI.
I have a table with member card usage data called NonSameDayUses:
https://www.screencast.com/t/yeSjoqonZ
I have another table with member card add data called AddsOnly:
https://www.screencast.com/t/zlPBRWaDqC
The tables are related by the GUID_TranDate2 field. I am trying to add a column to NonSameDayUses that provides the date just before the use date (to calculate when the amount used was added to their card). I have tried a million things, but this is my current formula and I can't figure out what is wrong with it:
DateAdded =
MAXX (
FILTER (
AddsOnly,
AND (
AddsOnly[member_guid] = [member_guid],
AddsOnly[ValueAddDate] < [TransactionDate]
)
),
AddsOnly[TransactionDate]
)
Neither filter is working for me. If I try it with just the first argument (member_guid), I get blanks. If I try with the second (dates) I get the max date for the whole table with no filtering.
Any help would be sooooooooooo appreciated, as I am currently banging my head against the wall! :)
Try qualifying all the column names, it should work:
DateAdded =
MAXX(
FILTER(
AddsOnly
, AND(
AddsOnly[member_guid]
= NonSameDayUses[member_guid]
, AddsOnly[ValueAddDate]
< NonSameDayUses[TransactionDate]
)
)
, AddsOnly[TransactionDate]
)