Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.
Hello everyone! I have a question regarding data lineage. When I created a table using TOPN function, it didn't work with my time slicer.
I'm trying to calculate the percentage change of stock price over the last 30 days. I have a slicer to let ppl choose which period they want to see. And the last 30 days will be from the last day they choose and 30 days above. And I used the last day price / the last 30 day price -1 to get the percentage change. Since I have multiple stocks with different stock codes, I want to display the top 3 stocks who have the most increase in their price.
Here's my measure I used in the TOPN function:
Last30Days%change =
VAR _lastDate =
CALCULATE ( MAX ( 'Calendar'[Date] ), ALLSELECTED ( 'Calendar'[Date] ) )
VAR _last30days =
DATESINPERIOD ( 'Calendar'[Date], _lastDate, -30, DAY )
VAR _last =
LASTNONBLANK (
TREATAS ( _last30days, hs300data[Date] ),
SUM ( hs300data[Close] )
)
VAR _first =
FIRSTNONBLANK (
TREATAS ( _last30days, hs300data[Date] ),
SUM ( hs300data[Close] )
)
VAR _lastValue =
CALCULATE (
SUM ( hs300data[Close] ),
FILTER ( hs300data, hs300data[Date] = _last )
)
VAR _firstValue =
CALCULATE (
SUM ( hs300data[Close] ),
FILTER ( hs300data, hs300data[Date] = _first )
)
VAR _result =
DIVIDE ( _lastValue, _firstValue ) - 1
RETURN
_result
Here's my table using TOPN to show the top 3:
Last30Days =
TOPN (
3,
ADDCOLUMNS (
VALUES ( 'hs300data'[Stock_Code] ),
"%30change", [Last30Days%change] ),
[%30change]
)
For more details, I created a sample pbix and attached the dataset
Please pls pls, any help will be much appreciated!! I'm stuck here for two days now...
Solved! Go to Solution.
calculated table can not get the filter contexts come from viusals
calculated table can not get the filter contexts come from viusals
ooo good to know! tyty. But do you happen to know what's the best way to create a measure using TOPN? My dax returns multiple columns (stock_code and the percentage change) to return the top n values, which is why I created a table for it.
visual filter can do this work.
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 |
---|---|
54 | |
27 | |
23 | |
14 | |
11 |
User | Count |
---|---|
77 | |
63 | |
44 | |
17 | |
12 |