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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
stella1215
Frequent Visitor

Using TOPN to create a table that did not work with time slicer

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...

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

calculated table can not get the filter contexts come from viusals

View solution in original post

3 REPLIES 3
wdx223_Daniel
Super User
Super User

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.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors