Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
I have the following calculated table from this code:
Tbl =
SUMMARIZECOLUMNS(
dAssets[Ticker],
dDates[Date],
"Purchases", [Shares purchased],
"Sales", [Shares sold]
)
Then I created the following measure that retrieves the last purchase date prior to each sale row...
Last purchase date before respective sale =
VAR Ticker_Ref = MAX( Tbl[Ticker] )
VAR Date_Ref = MAX( Tbl[Date] )
RETURN
MAXX(
FILTER(
ALLSELECTED( Tbl ),
[Ticker] = Ticker_Ref &&
[Date] <= Date_Ref &&
[Purchases] <> BLANK()
),
[Date]
)
... which works just fine when dropped to a table visual:
I tried to produce those same results above as a calculated column but I keep getting wrong results. How can I get that to work?
Since I'm a DAX newbie, I still have a hard time reproducing a measure code as a calculated column or vice-versa...
The closest I got was this...
Last purchase date before respective sale =
VAR Date_Ref = MAX( Tbl[Date] )
RETURN
CALCULATE(
MAX( Tbl[Date] ),
ALLEXCEPT( Tbl, Tbl[Ticker] ),
Tbl[Date] <= Date_Ref,
Tbl[Purchases] <> BLANK()
)
... but it returns wrong results (max purchase row date per group of tickers, instead of max purchase row date within each group of tickers prior to or up to each respective sale row date):
And before one suggests I should think long and hard before resorting to calculated columns as opposed to measures: I've already done such philosophical conundrum and realized that for this particular application, which is producing that piece of calculation, and others that follow, in order to reach a particular (and very complex) calculation goal, I have no other choice but go with calculated columns.
Solved! Go to Solution.
Managed to get to the bottom of this thing...
Last purchase date before respective sale =
VAR Ticker_Ref = Tbl[Ticker]
VAR Date_Ref = Tbl[Date]
RETURN
MAXX(
FILTER(
ALL( Tbl ),
Tbl[Ticker] = Ticker_Ref &&
Tbl[Date] <= Date_Ref &&
Tbl[Purchases] <> BLANK()
),
Tbl[Date]
)
Managed to get to the bottom of this thing...
Last purchase date before respective sale =
VAR Ticker_Ref = Tbl[Ticker]
VAR Date_Ref = Tbl[Date]
RETURN
MAXX(
FILTER(
ALL( Tbl ),
Tbl[Ticker] = Ticker_Ref &&
Tbl[Date] <= Date_Ref &&
Tbl[Purchases] <> BLANK()
),
Tbl[Date]
)
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
10 | |
9 | |
8 | |
8 | |
7 |
User | Count |
---|---|
13 | |
12 | |
11 | |
11 | |
8 |