In Power BI, I have table with following data, columns "Company", "Document", "Link", "Price", "Document Category"
Example:
I need to add new column (for example Company Category), where will be most accured value of "Document Category" depend on "Company" and with rule, that "Link" column value is not empty.
So the new table would look like this:
Company A, most common value of "Document Category" (where link is
not empty) is "Car"
Company B has "Airplane"
Company C has "Other"
It's possible to add new column for this case in Power BI via DAX?
This is similar to the question I linked in the comments. For each Company, you want to count how many times each Document Category appears and then pick the top one.
Company Category =
VAR CurrRowCompany = Table1[Company]
VAR DocumentCategories =
SUMMARIZE (
FILTER ( Table1, Table1[Company] = CurrRowCompany ),
Table1[Document Category],
"DocCount", COUNT ( Table1[Document Category] )
)
RETURN
SELECTCOLUMNS (
TOPN ( 1, DocumentCategories, [DocCount] ),
"Category", Table1[Document Category]
)
Related
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 )
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 )
I have created this table in Power BI:
DimProduct = UNION(
ROW("ProductId", 1, "Product", "AB","ProductType","A"),
ROW("ProductId", 2, "Product", "AC","ProductType","A"),
ROW("ProductId", 3, "Product", "AD","ProductType","A"),
ROW("ProductId", 4, "Product", "BB","ProductType","B"),
ROW("ProductId", 5, "Product", "BC","ProductType","B")
)
Then I created this table:
DimProductSelectedType =
VAR vSelectedProduct=
SELECTEDVALUE('DimProduct'[Product])
RETURN
ROW("Col1",vSelectedProduct)
I dropped DimProduct[Product] into a slicer
I dropped DimProductSelectedType into a list
I expect that when I pick one product from the product slicer, it will appear in the list. But the list always remains blank.
I had thought that SELECTEDVALUE would reflect any single value picked in a slicer
That's my immediate problem, and this can be summarized as
Table columns that use DAX calcs are evaluated at import time. Measure are evaluated at runtime
What I'm actually working towards is:
Pick a product in a slicer
Identify that products product type
Show visualisations comparing selected product compared to all other products within that product type
I actually started with this and now I'm working my way back.
DimProductTypeSelected =
VAR vSelectedProduct=
SELECTEDVALUE('DimProduct'[Product])
VAR vSelectedProductType=
SUMMARIZE(
FILTER(
ALLSELECTED(DimProduct),
DimProduct[Product]=vSelectedProduct
),
DimProduct[ProductType]
)
RETURN
FILTER(
ALLSELECTED(DimProduct),
DimProduct[ProductType]=vSelectedProductType
)
When you select item in a slicer it already creates fitler context on other visuals, so there is no need to overwrite it manually. What you would need to do is to create a fitler context for all products, e.g. by using ALL or ALLSELECTED for the comparators. But in order for this to be dynamic you need to use measures, tables are static and do not recalculate with UI changes.
To explain why you get your results with your current approach - tables are refreshed once and do not change afterwards. Therefore vSelectedProduct always returns blank, as at the moment of refreshing this table there is no filter context on the DimProduct[Product], so it defaults to blank
If you create a measure Measure = SELECTEDVALUE('DimProduct'[Product]) and put DimProduct[Product] into a slicer it will show the selected product for a single select and blank for multiselect (or all values)
EDIT
Regarding the second part of your question - selecting a product will create a filter context only for that product in the other visuals. Then you could use e.g.:
CountRowsPerType =
VAR vSelectedType =
SELECTEDVALUE('DimProduct'[ProductType])
RETURN
CALCULATE( COUNTROWS(DimProduct), ALL(DimProduct[Product]), 'DimProduct'[ProductType] = vSelectedType )
which will return count for all the products of that type even though the visual will only show the selected product
Say I have Page 'A' and I want to drill through to Page 'B'. Ordinarily I would set up the drill through by specifying the field in page B that "joins" the same field from page A.
I want to do something slightly different. I would like to somehow specify that the User Name column from a table on page A should be used to filter the Manager Name field, from the same table, in page B.
For example, assume on page A, the current User Name is John Smith. On page B I would like the value of John Smith to be used to filter the Manager Name field from the same table.
Is this possible?
This is an interesting prompt. I think one way to do this is to have your [User Name] and [Manager Name] fields in a table in your model with ONLY an inactive relationship on the [Manager Name]. Then, when you filter on the [User Name], you would use DAX calculations that look something like this:
YourMeasure =
IF (
ISFILTERED ( 'NewTable'[User Name] ) = TRUE (),
CALCULATE (
CALCULATE (
[ExistingMeasure],
ALLEXCEPT ( 'NewTable', [Manager Name] ),
USERELATIONSHIP ( 'Table1'[Manager Name], 'NewTable'[Manager Name] )
)
),
BLANK ()
)
The main consideration here is that you'd have to do this for each measure you want to display on Page B.
I have a big data table with a column called Account Name. In another table (Accounts) I have a column of Account Keywords that contains parts of full account names. I want to filter the big data by column 'Account Name' using the list of keywords in the 'Acount Keywords' list.
I've created a new measure in the big data table called 'Filter Accounts' with the following DAX:
FILTER ACCOUNTS = contains(Accounts,Accounts[Account Keyword],Big_Data[Account Name])
But the "contains" function works on exact matches. I want it to return true if the 'Account Keyword' is found within any part of the 'Account Name' field. I also want it to ignore case.
The DAX statement results in TRUE only for exact matches. How do I modify my DAX to achieve the desired non-exact matches?
DAX has two functions for text contains matching, CONTAINSSTRING and CONTAINSSTRINGEXACT, where the latter is case-sensitive but the former is not.
You can find how many keywords match an Account Name by writing a calculated column like this on the Big_Data table:
Keyword Matches =
COUNTROWS (
FILTER (
Accounts,
CONTAINSSTRING ( Big_Data[Account Name], Accounts[Account Keyword] )
)
)
To get a TRUE or FALSE output instead of a count, simply append > 0 to see if the count is a positive value.
Match Exists =
COUNTROWS (
FILTER (
Accounts,
CONTAINSSTRING ( Big_Data[Account Name], Accounts[Account Keyword] )
)
) > 0