Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
I have a calculated table that returns each purchase and sale transaction by stock ticker by date.
Then I created a helper column that returns whether each row relates to a "Purchase" transaction, a "Sale" transaction, or to both ("Purchase/Sale").
ADDCOLUMNS(
SUMMARIZECOLUMNS(
dAssets[Ticker],
dDates[Date],
"Purchased", [Shares purchased],
"Sold", [Shares sold],
"Balance", [Shares balance]
),
"Transaction", SWITCH(
TRUE(),
[Purchased] <> BLANK() && [Sold] = BLANK(), "Purchase",
[Purchased] = BLANK() && [Sold] <> BLANK(), "Sale",
[Purchased] <> BLANK() && [Sold] <> BLANK(), "Purchase/Sale"
)
)
What I need is a calculated column that indicates how many rows (NEGATIVE) above each "Sale" row the last "Purchase" sale row had taken place.
Below is a screenshot of the table produced by the above code and I hardcoded in red the desired calculated column output.
This seems simple but I just can't get a way to make it work...
Right out of the gate I get the error "the column 'Transaction' cannot be found or may not be used in this expression" when trying to generate a calculated column with the Date of the last "Purchase" transaction prior to each respective "Sale".
VAR Tbl =
ADDCOLUMNS(
SUMMARIZECOLUMNS(
dAssets[Ticker],
dDates[Date],
"Purchased", [Shares purchased],
"Sold", [Shares sold],
"Balance", [Shares balance]
),
"Transaction", SWITCH(
TRUE(),
[Purchased] <> BLANK() && [Sold] = BLANK(), "Purchase",
[Purchased] = BLANK() && [Sold] <> BLANK(), "Sale",
[Purchased] <> BLANK() && [Sold] <> BLANK(), "Purchase/Sale"
)
)
RETURN
ADDCOLUMNS(
Tbl,
"Last Purchase Date",
VAR Ticker_Ref = dAssets[Ticker]
VAR Date_Ref = MAX( dDates[Date] )
RETURN
CALCULATE(
MAXX(
dDates,
dDates[Date]
),
dAssets[Ticker] = Ticker_Ref,
dDates[Date] <= Date_Ref,
[Transaction] = "Purchase" || [Transaction] = "Purchase/Sale"
)
)
That can be done with the windowing functions as long as you can articulate what "before" means from a sorting perspective.
Please provide sample data (with sensitive information removed) that covers your issue or question completely, in a usable format (not as a screenshot). Leave out anything not related to the issue.
https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Please show the expected outcome based on the sample data you provided.
https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
45 | |
26 | |
22 | |
13 | |
8 |
User | Count |
---|---|
73 | |
51 | |
45 | |
16 | |
12 |