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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
lancea
Helper I
Helper I

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:

lancea_0-1624856696108.png

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:

lancea_1-1624856783490.png

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?

1 ACCEPTED SOLUTION
v-xulin-mstf
Community Support
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:

vxulinmstf_0-1625127622528.png

 

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!

View solution in original post

7 REPLIES 7
v-xulin-mstf
Community Support
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:

vxulinmstf_0-1625127622528.png

 

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!

Jihwan_Kim
Super User
Super User

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 )


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

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

amitchandak
Super User
Super User

@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 )

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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

lancea_0-1624859944186.png

 

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 *:*)

@lancea , change topN like and try

 

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

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

It works now but unfortunately, I still get the same results:

 

lancea_0-1624868538588.png

 

Here is my formula for 8wk_separate:

 

8wk_separate = VAR LastWeek = MAX ( Query1[Week] )
VAR Last8Weeks = TOPN(10,filter(all(Week_Table[Week]), Week_Table[Week] <=max(Week_Table[Week])),Week_Table[Week],DESC)
RETURN
CALCULATE ( SUM ( Query1[Total PO lines due this week (CRD)] ), Last8Weeks )

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.