DAX code to countrows when comparing values in two columns - compare

How can I compare values in two related tables to do a count in a measure?
Here is the measure formula I wrote - which does not work:
PoliciesPurchasedAsStudent =
CALCULATE(Countrows(Policies),People[Date Graduated]>=Policies[Date Purchased]))
The People table contains one record per customer (including a column for Date Graduated)
The Policies table contains all the policies owned by all the customers (including a column for Date Purchased)
The tables are linked on the Customer ID number.
I just want to know how many policies were purchased by customers before they graduated.
What am I doing wrong?

Try using the RELATED() function which lets you get the related value in Policies table.
PoliciesPurchasedAsStudent =
CALCULATE (
COUNTROWS ( Policies ),
FILTER (
People,
People[Date Graduated] >= RELATED ( Policies[Date Purchased] )
)
)
Let me know if this helps.

Related

Find and return a new column with unique values comparing two table columns in Power BI

I have 2 tables, one table for projects and other with services performed. This services always use a project number, but some projects aren't used yet.
Example table projects:
Project Number
Object
12345
describe one
45146
describe two
10015
describe three
Example table services performed
Location
Price
Project Number
City 1
100,000
80002
City 2
200,000
12345
City 3
300,000
56874
I want to know (create a column) with the projects that weren't used yet. At this example, I need a column with 2 lines, project 45146 and 10015. How can I do it?
I'm still learning dax and I thought in SEARCH/FIND, but I didn't get what I need.
Thanks for your help.
The measure Returns True/False values. You can use it as a measure for matrix or as a calculated column.
=
ISEMPTY(
EXCEPT(
CALCULATETABLE(VALUES(Projects[Project Number]))
,VALUES(performed[Project Number])
)
)
Can’t you just add a new table and use EXCEPT for this?
Table =
EXCEPT (
DISTINCT ( 'projects'[Project Number] ) ,
DISTINCT ( 'services'[Project Number] )
)
Not sure what this is solving, but if you specifically want to calculate this as a new column (/table) then by all means!

Power BI count rows with same ID

I'm trying to count my rows but I've got a little issue. some rows have the same ID and I don't want to count those again, it's like each row represents a product but some products have the same ID. I'm trying to count the products from Their ids.
is there any measure to apply?
I've used the measure "Countrows" but as I said I've got a lot of Duplicated IDS .
You can use DISTINCTCOUNT:
MyMeasure = DISTINCTCOUNT ( MyTable[ProductID] )

Get first department join date for each client from a different table using DAX

In my scenario, many clients moving between departments and the system is tracking this using a table with columns like: ClientID/ DepartmentID/ Start date.
What if I need to get a column with first department start-date for each client? I am trying to read this to a Client profile table from the above Department association table using DAX:
FIRSTDATE('ClientDepartment'[StartDate]) will give only a single date for all clients. I am looking to get something like:
For Example (see client, department movement and result needed tables:
This is for a tabular model and I am trying to add as a column (not as measure or calculated table).
I tried:
CALCULATE (
FIRSTDATE ( ClientDepartments[StartDate] ),
ClientDepartments[ClientID] = Clients[ClientID]
)
but throwing an error
The expression contains multiple columns, but only a single column can
be used in a True/False expression that is used as a table filter
expression.
If the two tables have an active relationship, such that the Clients table is a dimension table for the ClientDepartments table, then you can simply use the following expression for the calculated column:
CALCULATE ( MIN ( 'ClientDepartments'[StartDate] ) )
If you don't have a relationship in place, use a variable:
VAR currentClient = 'Clients'[ClientID]
RETURN
CALCULATE (
MIN ( 'ClientDepartments'[StartDate] ) ,
'ClientDepartments'[ClientID] = currentClient
)

Power BI Rank within Matrix Visual

I've spent many weeks trying to get this to work including a posting at another site. No luck. Not sure what I'm missing.
The following code gets me the table of data that I need. However, I want just the "rank" value and I want it for each row within a matrix visual.
First some background, the PowerBI page has slicers on dates and "base" product. Any given product may be sold in multiple countries. All carry the same base product suffix with a country-specific prefix.
The matrix in question displays several aspects of all the product variants for the selected base product. I want to show where each variant ranks (unit sales) within its country.
VAR tblCountrySales =
SUMMARIZE (
FILTER(
tblSalesUnits,
RELATED(tblProducts[Country])="US"
),
tblProducts[ProdID],
"Total Sales", SUM ( tblSalesUnits[Units] )
)
RETURN
ADDCOLUMNS (
tblCountrySales,
"ProductRank", RANKX ( tblCountrySales, [Total Sales] )
)
Is there a way to either pull just the rank for a single product from the above code, or, better yet, a DAX pattern that will get me the rank by unit sales of each product variant within its respective country?
I've use these statements to successfully get the product ID and country for each row within the visual.
VAR ProdSales = SUM(tblSales[Units])
VAR ProdCountry = SELECTEDVALUE(tblProducts[Country])
Any help is greatly appreciated. I assume I'm missing something with row context within the visual as a simple COUNTROWS on the table expressions above always returns "1". Being new to PowerBI and DAX I'm still trying to grasp the nuances of filter and row contexts.
The products table contains data like...
ProdID.....BaseID....Name...Country
7190...........7190.....xxxx.....US
150207190......7190.....XXXX....Panama
241807190......7190.....xxxx.....Spain
The sales table contains data like...
ProdID......SalesMonth......Units.....USD
7190........July 2010.......4563....$23491.00
150207190...July 2010.......2543....$16235.00
241807190...July 2010.......1263....$8125.00
There is a dates table as well that links the SalesMonth to the usual selection of date display formats. The two tables above are linked via ProdID, though the visuals are sliced by the BaseID. I'd attach a picture, but don't have permissions to do so.
With some more tinkering I found this solution.
Country Rank =
VAR ProdSales = SUM(tblSales[Units])
VAR ProdCountry = SELECTEDVALUE(tblProducts[Country])
VAR tblCountrySales =
SUMMARIZE (
FILTER(
ALLEXCEPT(tblProducts,tblProducts[BaseID]),
tblProducts[Country]=ProdCountry
),
tblProducts[ProdID],
"Total Sales", SUM ( tblSales[Units] )
)
RETURN
IF(ProdSales>0,COUNTROWS(FILTER(tblCountrySales,[Total Sales]>ProdSales))+1,BLANK())

Power BI: How to dynamically change currency

We have requirement to allow user to choose which currency he wants to see in the dashboard, like below example:
By default, it's GBP, if user changes to USD, we need to show the spend by USD. Under the hood we already have table InvoiceDetail which contains columns doing currency conversion beforehand:
SpendInGBP
SpendInUSD
SpendInEUR
I am not sure how I can map when user chooses different currency using ChicletSlicer to different Columns.
If you have a table containing all formats available to apply this can be achieved.
I've created these tables as example:
MyTable
CurrencyFormat
In the MyTable table I've created two measures called Format and Total Sales.
Format = LASTNONBLANK ( CurrencyFormat[Format], 1 )
Total Sales = Total Sales = FORMAT(SUM(MyTable[Sales]),[Format])
Note Total Sales measures uses the [Format] measure to get the selected format from the slicer.
Adding the ChicletSlicer and setting FormatName column from CurrencyFormat table in Category pane, you should get the expected result.
Also note the formats I've used can vary from what you need, so you have to add some changes to the format strings, do a quick read of the documentation about it.
Format Region
$#,##0;($#,##0) SpendInUSD
£#,##0;(£#,##0) SpendInGBP
€#,##0;(€#,##0) SpendInEUR
UPDATE: OP wants to get the sum of the right column based on the slicer.
Fortunately your table has a column for every currency, as you found with my proposal to map the slicer value to your measure, this is the final expression:
Spend =
IF (
LASTNONBLANK ( 'Currency'[Code], 1 ) = "GBP",
SUM ( Invoice[SpendGBP] ),
IF (
LASTNONBLANK ( 'Currency'[Code], 1 ) = "USD",
SUM ( Invoice[SpendUSD] ),
SUM ( Invoice[SpendEUR] )
)
)
Let me know if you need further help.