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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
PBI12345
Frequent Visitor

Dynamic Top N calculation for time series visual

Hello,

 

I have created a dynamic Top N calculation which follows the DAX logic in an article linked here. I have also pasted this DAX at the bottom this message.

 

Whilst this measure works as intended, when used in a time series line graph it evaluates the top N selection at each data point. Instead, I want it to evaluate across the entire data range in the visualisation - the same way the Top N filter works when applied to a graph.

 

Please see example below:

PBI12345_1-1734330095276.png

 

Would be extremely grateful for assistance on this one -- have sunk considerable time in without any result! 

 

DAX from linked article above:

 

Top Orders | Cities = 

VAR N = 'Top N'[Top N Value]

-- Get the top N cities by total orders
VAR Top_Cities = 
    TOPN( 
        N, 
        ALLSELECTED('Cities'[City]), 
        [Total Orders] 
    )

-- Calculate the total orders for the Top N cities
VAR TopN_Orders = 
    CALCULATE(
        [Total Orders],
        KEEPFILTERS(Top_Cities)
    )

-- Calculate total orders for all selected cities
VAR All_Orders = 
    CALCULATE(
        [Total Orders],
        ALLSELECTED(Cities)
    )

-- Calculate the total orders for cities that are not in the Top N
VAR Other_Orders = 
    All_Orders - CALCULATE([Total Orders], Top_Cities)


VAR Result = 
    SWITCH(
        TRUE(),
        NOT ISINSCOPE(Cities[City]), All_Orders,
        SELECTEDVALUE(Cities[City]) = "Others", Other_Orders,
        TopN_Orders
    )



RETURN 
    Result

 

 

 

 

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

I think you need to tweak the Top_Cities variable to use ALLSELECTED('Date')

Top Orders | Cities =
VAR N = 'Top N'[Top N Value] -- Get the top N cities by total orders
VAR Top_Cities =
    CALCULATETABLE (
        TOPN ( N, ALLSELECTED ( 'Cities'[City] ), [Total Orders] ),
        ALLSELECTED ( 'Date' )
    ) -- Calculate the total orders for the Top N cities
VAR TopN_Orders =
    CALCULATE ( [Total Orders], KEEPFILTERS ( Top_Cities ) ) -- Calculate total orders for all selected cities
VAR All_Orders =
    CALCULATE ( [Total Orders], ALLSELECTED ( Cities ) ) -- Calculate the total orders for cities that are not in the Top N
VAR Other_Orders =
    All_Orders - CALCULATE ( [Total Orders], Top_Cities )
VAR Result =
    SWITCH (
        TRUE (),
        NOT ISINSCOPE ( Cities[City] ), All_Orders,
        SELECTEDVALUE ( Cities[City] ) = "Others", Other_Orders,
        TopN_Orders
    )
RETURN
    Result

View solution in original post

2 REPLIES 2
johnt75
Super User
Super User

I think you need to tweak the Top_Cities variable to use ALLSELECTED('Date')

Top Orders | Cities =
VAR N = 'Top N'[Top N Value] -- Get the top N cities by total orders
VAR Top_Cities =
    CALCULATETABLE (
        TOPN ( N, ALLSELECTED ( 'Cities'[City] ), [Total Orders] ),
        ALLSELECTED ( 'Date' )
    ) -- Calculate the total orders for the Top N cities
VAR TopN_Orders =
    CALCULATE ( [Total Orders], KEEPFILTERS ( Top_Cities ) ) -- Calculate total orders for all selected cities
VAR All_Orders =
    CALCULATE ( [Total Orders], ALLSELECTED ( Cities ) ) -- Calculate the total orders for cities that are not in the Top N
VAR Other_Orders =
    All_Orders - CALCULATE ( [Total Orders], Top_Cities )
VAR Result =
    SWITCH (
        TRUE (),
        NOT ISINSCOPE ( Cities[City] ), All_Orders,
        SELECTEDVALUE ( Cities[City] ) = "Others", Other_Orders,
        TopN_Orders
    )
RETURN
    Result

Hi John,

 

This worked exactly as hoped. Thank you so much. 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Feb2025 NL Carousel

Fabric Community Update - February 2025

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