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

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 )

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 )

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.

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.

PowerBI DAX get COUNT DISTINCT with GROUP BY , see SQL query below

I have got this following SQL query that gives me the correct value from the database.
SELECT
SUM( DISTINCT_ORDER_NUMBERS )
FROM
(
SELECT STORE_KEY,
COUNT( DISTINCT TRANSACTION_NUM ) AS DISTINCT_ORDER_NUMBERS,
DATE_KEY,
TRANSACTION_TYPE_KEY
FROM Pos_Data
GROUP BY STORE_KEY,
DATE_KEY,
TRANSACTION_TYPE_KEY
)
AS A
I am however facing challenges writing a DAX formula for a measure in Power BI Here is what I have tried so far but I get an error.
Total Number Of Orders
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUM(_TotalOrders[DISTINCT_ORDER_NUMBERS])
Please assist
The SUM function expects a base table rather than a calculated table.
Try this instead:
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUMX(_TotalOrders, [DISTINCT_CHECK_SEQ])
Edit: If the difference you mentioned is related to nulls, then try this in place of DISTINCTCOUNT.
COUNTAX( DISTINCT( 'Pos_Data'[TRANSACTION_NUM] ), 'Pos_Data'[TRANSACTION_NUM] )
The COUNTAX function (as opposed to COUNTX) does not count nulls.