Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Caz_16
Helper II
Helper II

Nested Filter Referencing Two Unrelated Tables.

Hello everyone,

 

I am feeling absolutely beat. I have been trying to get a conditional value to populate based on if two conditions are met, which will then be used in future formulas, but apparently I am at a complete lack of understanding as to how PBI operates when it comes to evaluating expressions. 

 

I have 3 tables with Stock Data pulling form a SQL server:

Stock_main - Columns (Stock ID (Primary), Ticker, Company_name, stock_index)

Owned - Columns (Purchase ID, Stock ID (Foreign), Buy_Date, Shares, Cost, Portfolio)
Price - Columns (Stock ID (Foreign), Date_stamp, Open, High, Low, Close, Volume)

 

What I want is to determine if I have owned a specific stock on that date. I have been trying methods with IF statements and FILTER statements to iterate through each row of the "Price" table to look at the stock ID, match it with a Stock ID from the "Owned" table, then determine if the Date_stamp in the price table is greater than (after) the date i bought it by referencing the "Buy_Date" column in the "Owned" table. 

 

This is one that that works at a hit rate of about 80% of the time. 

 
Owned at Date Conditional =
IF(
AND (FIRSTDATE('price'[Date_stamp])>=
FIRSTDATE(
FILTER(
FILTERS('owned'[buy_date]),
FIRSTNONBLANK('owned'[stock_id],"")=FIRSTNONBLANK('price'[stock_id],"")
)
),
FIRSTNONBLANK('price'[stock_id],"") IN DISTINCT('owned'[stock_id])
),
TRUE(),
FALSE())
 

Like I said, the above formula results in a TRUE or FALSE correctly about 80% of the time. 

 

Please help. There has to be an easier way to do this but I'm just so wrapped up in this incorrect method that I am stuck running in circles. 

 

Cheers,

1 ACCEPTED SOLUTION

You never sell your stock? (Can we assume that if a row has TRUE for one date and stock, it will remain true for the rest of the table for that stock?)
If so, the calculated column (big difference with a measure) is like this (typing on my phone so sorry for mistakes):
VAR curDate= Price[date_stamp]
VAR curStock = Price[Stock_ID]
RETURN
IF ( COUNTROWS( FILTER( ALL(Owned), Owned[Stock_ID]= curStock && Owned[BuyDate] < curDate)) > 0, TRUE, FALSE)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

3 REPLIES 3
JarroVGIT
Resident Rockstar
Resident Rockstar

Tip: start using variables to make your code more readable. It took me a while to read your code. Now my biggest question is; is this a calculated column and if so, to what table? You say "I want to determine if I owned a stock on a date", where does that date come from? What has that to do with table Price? (Or does that table only contains rows for OLHC when you owned a stock?) 

Please explain a bit more what you are trying to achieve (and where) and wat the expected result looks like 🙂

 

Kind regards

Djerro123

-------------------------------

If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.

Keep those thumbs up coming! 🙂





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Djerro,

 

Sorry for the unclear question, allow me to clarify. 

 

This will be a calculated column, or measure (Im unsure which to be using), within the Price table. The price table holds the the HLOC and volume for each day, for each stock. 

 

Date_StampStock_IDOpenHighLowCloseVolume
1/1/20201234$5$5.50$4.90$5.12

1M

1/1/20205678$100$101$98$1005M
1/1/2020789$40$45$40$454M

1/2/2020

1234$5.12$5.60$4.98$5.45

2M

1/2/20205678$100$105$98$1026M
1/2/2020789$45$45$38$3910M

 

What I need is a column (either this table, or a new table) where a TRUE or FALSE is populated if I owned that stock on that date. See below, I did not own stock 789 on 1/1 or 1/2. The reason I need this is because I will use this to determine if I will include that stock's value, multiplied by the number of shares I own, in my overall portfolio. What I want is for it to look at the "Date_stamp" column and determine if that date is after the "Buy_date" column, which comes from a different table. 

 

Date_StampStock_IDOwned at Date
1/1/20201234TRUE
1/1/20205678TRUE
1/1/2020789FALSE

1/2/2020

1234TRUE
1/2/20205678TRUE
1/2/2020789FALSE

 

Also I found some tips yesterday where people are recommending using a seperate calendar table in order to perform date/time analysis in PBI. 

You never sell your stock? (Can we assume that if a row has TRUE for one date and stock, it will remain true for the rest of the table for that stock?)
If so, the calculated column (big difference with a measure) is like this (typing on my phone so sorry for mistakes):
VAR curDate= Price[date_stamp]
VAR curStock = Price[Stock_ID]
RETURN
IF ( COUNTROWS( FILTER( ALL(Owned), Owned[Stock_ID]= curStock && Owned[BuyDate] < curDate)) > 0, TRUE, FALSE)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.