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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
codingenthu
Frequent Visitor

Power BI DAX Query Issue: Last X Items Filter Consideration

Hi,

I am currently facing an issue with a DAX query in PowerBI related to the "Last X Items" filter. The challenge is that the filter considers continuous values even if they are missing in the dataset. Let me illustrate with an example.

I have a dataset with the following serial values:
1
2
3
4
7
8
10

Here, Items 5, 6, and 9 are missing. When applying the "Last 3 Items" filter, PowerBI includes items 8, 9, and 10 as the last three, but in reality, Item 9 is not present in the data.

DAX Measures:

I've used the following DAX measures:

CostSumByItem =
VAR item = SELECTEDVALUE(CostCountByItem[Item])
VAR maxItem = CALCULATE(MAX(CostCountByItem[Item]), REMOVEFILTERS(CostCountByItem[Item]))
VAR selectedItemCount = SELECTEDVALUE(SelectedItemsCount[value])
VAR inWindow = IF(item > (maxItem-selectedItemCount ), TRUE(), FALSE())
RETURN IF(inWindow, SUM(CostCountByItem[Count]), BLANK())
-----------------------------------------------------------------------------------

SelectedItemsCount =
VAR firstseries = GENERATESERIES(2,4)
VAR secondseries = GENERATESERIES(5,20,5)
RETURN ADDCOLUMNS(
UNION(UNION(firstseries,secondseries), ROW("value", 10000)),
"Name",
IF([value]>100, "All", "Last " & [value])
)

Issue:

The problem arises when the Last X Items filter includes non-continuous values, leading to incorrect results.


Thanks in Advance for your help!

1 ACCEPTED SOLUTION
VahidDM
Super User
Super User

Hi @codingenthu 

 

The issue you're facing is a common challenge when dealing with gaps in data series in Power BI using DAX. Your current approach calculates the last X items based on the assumption of a continuous series. However, as you have noticed, this does not work well when there are gaps in the data.

To solve this, you need to modify your DAX measure to account for gaps in the series. Instead of relying on the assumption that the last X items are continuous, you should identify the last X distinct items present in your dataset. Here's an approach to modify your CostSumByItem measure:

Determine the Last X Items Considering Gaps: You need a way to dynamically identify the last X items in your data. This can be achieved by creating a rank for each item based on its order in the dataset and then filtering out the top X items.

Calculate the Sum for the Identified Items: Once you have the last X items, you can proceed to calculate the sum for these items.

Here's a revised version of your CostSumByItem measure:

CostSumByItem Revised =
VAR selectedItemCount =
    SELECTEDVALUE ( SelectedItemsCount[value] )
VAR RankedItems =
    ADDCOLUMNS (
        SUMMARIZE ( CostCountByItem, CostCountByItem[Item] ),
        "Rank", RANKX ( ALL ( CostCountByItem ), CostCountByItem[Item],, DESC, DENSE )
    )
VAR FilteredItems =
    FILTER ( RankedItems, [Rank] <= selectedItemCount )
RETURN
    SUMX ( FilteredItems, CALCULATE ( SUM ( CostCountByItem[Count] ) ) )


In this revised measure:

RankedItems creates a rank for each distinct item in descending order.
FilteredItems filters the ranked items to keep only the top X items as per your selected item count.
The SUMX function then calculates the sum of Count for these filtered items

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

Appreciate your Kudos!! 

Badges.jpg

LinkedIn | Twitter | Blog | YouTube 

View solution in original post

1 REPLY 1
VahidDM
Super User
Super User

Hi @codingenthu 

 

The issue you're facing is a common challenge when dealing with gaps in data series in Power BI using DAX. Your current approach calculates the last X items based on the assumption of a continuous series. However, as you have noticed, this does not work well when there are gaps in the data.

To solve this, you need to modify your DAX measure to account for gaps in the series. Instead of relying on the assumption that the last X items are continuous, you should identify the last X distinct items present in your dataset. Here's an approach to modify your CostSumByItem measure:

Determine the Last X Items Considering Gaps: You need a way to dynamically identify the last X items in your data. This can be achieved by creating a rank for each item based on its order in the dataset and then filtering out the top X items.

Calculate the Sum for the Identified Items: Once you have the last X items, you can proceed to calculate the sum for these items.

Here's a revised version of your CostSumByItem measure:

CostSumByItem Revised =
VAR selectedItemCount =
    SELECTEDVALUE ( SelectedItemsCount[value] )
VAR RankedItems =
    ADDCOLUMNS (
        SUMMARIZE ( CostCountByItem, CostCountByItem[Item] ),
        "Rank", RANKX ( ALL ( CostCountByItem ), CostCountByItem[Item],, DESC, DENSE )
    )
VAR FilteredItems =
    FILTER ( RankedItems, [Rank] <= selectedItemCount )
RETURN
    SUMX ( FilteredItems, CALCULATE ( SUM ( CostCountByItem[Count] ) ) )


In this revised measure:

RankedItems creates a rank for each distinct item in descending order.
FilteredItems filters the ranked items to keep only the top X items as per your selected item count.
The SUMX function then calculates the sum of Count for these filtered items

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

Appreciate your Kudos!! 

Badges.jpg

LinkedIn | Twitter | Blog | YouTube 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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