Forum Discussion

lancea's avatar
lancea
Helper I
5 years ago
Solved

TopN returns different results when adding additional column

I am generating the following table below which just displays week number, total for the week (sum of sales), and a measure for calculating 8-week Running Total:

My problem is that, when I add the Week Description column (which is just text column from data source), the 8-week running total is no longer returning the correct results and is just the same as Total for the Week:

My formula for the 8-week Running Total is below:

8wk_Total =
VAR LastWeek = MAX ( Query1[Week] )
VAR Last8Weeks = TOPN (8, FILTER ( ALL ( Query1[Week] ), Query1[Week] <= LastWeek ), Query1[Week], DESC)
RETURN
CALCULATE ( SUM ( Query1[Sales] ), Last8Weeks )

 

Why does this happen and how can I fix this?

  • Hi lancea,

     

    Try measure as:

    8wk_Total =
    VAR LastWeek = MAX ( Query1[Week] )
    VAR Last8Weeks = TOPN (8, FILTER ( ALL ( Query1[Week] ), Query1[Week] <= LastWeek ), Query1[Week], DESC)
    RETURN
    CALCULATE ( SUM ( Query1[Sales] ), FILTER(ALL('Query1'),Query1[Week] IN Last8Weeks )

    Here is the output:

     

    If you still have some question, please don't hesitate to let me known.‌‌

     

    Best Regards,

    Link

     

    Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution. Really appreciate!

7 Replies

  • lancea , In such case better to have separate week or date table, That will allow you to all or allselected of table then column

     

    8wk_Total =
    VAR LastWeek = MAX ( Query1[Week] )
    VAR Last8Weeks = TOPN (8, FILTER ( ALL ( Week[Week] ), Query1[Week] <= LastWeek ), Query1[Week], DESC)
    RETURN
    CALCULATE ( SUM ( Query1[Sales] ), Last8Weeks )

    • lancea's avatar
      lancea
      Helper I

      Sorry, this does not work and returns error in the formula:

       

      For reference, I created a Week_Table using the formula:

      Week_Table = UNION(VALUES(Query1[Week]),{""})
       
      Then I created a 1:* relationship between Query1 and Week_Table (the only other alternative is *:*)
      • amitchandak's avatar
        amitchandak
        Super User

        lancea , change topN like and try

         

        TOPN(10,filter(all(WeekTable[Week]), WeekTable[Week] <=max(WeekTable[Week])),WeekTable[Week],DESC)

  • 8wk_Total =
    VAR LastWeek =
    MAX ( Query1[Week] )
    VAR Last8Weeks =
    TOPN (
    8,
    FILTER ( ALL ( Query1 ), Query1[Week] <= LastWeek ),
    Query1[Week], DESC
    )
    RETURN
    CALCULATE ( SUM ( Query1[Sales] ), Last8Weeks )

    • lancea's avatar
      lancea
      Helper I

      Unfortuantely, this returns same result as the 2nd image.

  • v-xulin-mstf's avatar
    v-xulin-mstf
    Community Support

    Hi lancea,

     

    Try measure as:

    8wk_Total =
    VAR LastWeek = MAX ( Query1[Week] )
    VAR Last8Weeks = TOPN (8, FILTER ( ALL ( Query1[Week] ), Query1[Week] <= LastWeek ), Query1[Week], DESC)
    RETURN
    CALCULATE ( SUM ( Query1[Sales] ), FILTER(ALL('Query1'),Query1[Week] IN Last8Weeks )

    Here is the output:

     

    If you still have some question, please don't hesitate to let me known.‌‌

     

    Best Regards,

    Link

     

    Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution. Really appreciate!