<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Power BI DAX Query Issue: Last X Items Filter Consideration in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Power-BI-DAX-Query-Issue-Last-X-Items-Filter-Consideration/m-p/3566981#M137506</link>
    <description>&lt;P&gt;&lt;SPAN&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;I have a dataset with the following serial values:&lt;BR /&gt;1&lt;BR /&gt;2&lt;BR /&gt;3&lt;BR /&gt;4&lt;BR /&gt;7&lt;BR /&gt;8&lt;BR /&gt;10&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;DAX Measures:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I've used the following DAX measures:&lt;BR /&gt;&lt;BR /&gt;CostSumByItem =&lt;BR /&gt;VAR item = SELECTEDVALUE(CostCountByItem[Item])&lt;BR /&gt;VAR maxItem = CALCULATE(MAX(CostCountByItem[Item]), REMOVEFILTERS(CostCountByItem[Item]))&lt;BR /&gt;VAR selectedItemCount = SELECTEDVALUE(&lt;SPAN&gt;SelectedItemsCount[value]&lt;/SPAN&gt;)&lt;BR /&gt;VAR inWindow = IF(item &amp;gt; (maxItem-selectedItemCount ), TRUE(), FALSE())&lt;BR /&gt;RETURN IF(inWindow, SUM(CostCountByItem[Count]), BLANK())&lt;BR /&gt;-----------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;SelectedItemsCount =&lt;BR /&gt;VAR firstseries = GENERATESERIES(2,4)&lt;BR /&gt;VAR secondseries = GENERATESERIES(5,20,5)&lt;BR /&gt;RETURN ADDCOLUMNS(&lt;BR /&gt;UNION(UNION(firstseries,secondseries), ROW("value", 10000)),&lt;BR /&gt;"Name",&lt;BR /&gt;IF([value]&amp;gt;100, "All", "Last " &amp;amp; [value])&lt;BR /&gt;)&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Issue:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The problem arises when the Last X Items filter includes non-continuous values, leading to incorrect results.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;Thanks in Advance for your help!&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 04 Dec 2023 19:53:43 GMT</pubDate>
    <dc:creator>codingenthu</dc:creator>
    <dc:date>2023-12-04T19:53:43Z</dc:date>
    <item>
      <title>Power BI DAX Query Issue: Last X Items Filter Consideration</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Power-BI-DAX-Query-Issue-Last-X-Items-Filter-Consideration/m-p/3566981#M137506</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;I have a dataset with the following serial values:&lt;BR /&gt;1&lt;BR /&gt;2&lt;BR /&gt;3&lt;BR /&gt;4&lt;BR /&gt;7&lt;BR /&gt;8&lt;BR /&gt;10&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;DAX Measures:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I've used the following DAX measures:&lt;BR /&gt;&lt;BR /&gt;CostSumByItem =&lt;BR /&gt;VAR item = SELECTEDVALUE(CostCountByItem[Item])&lt;BR /&gt;VAR maxItem = CALCULATE(MAX(CostCountByItem[Item]), REMOVEFILTERS(CostCountByItem[Item]))&lt;BR /&gt;VAR selectedItemCount = SELECTEDVALUE(&lt;SPAN&gt;SelectedItemsCount[value]&lt;/SPAN&gt;)&lt;BR /&gt;VAR inWindow = IF(item &amp;gt; (maxItem-selectedItemCount ), TRUE(), FALSE())&lt;BR /&gt;RETURN IF(inWindow, SUM(CostCountByItem[Count]), BLANK())&lt;BR /&gt;-----------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;SelectedItemsCount =&lt;BR /&gt;VAR firstseries = GENERATESERIES(2,4)&lt;BR /&gt;VAR secondseries = GENERATESERIES(5,20,5)&lt;BR /&gt;RETURN ADDCOLUMNS(&lt;BR /&gt;UNION(UNION(firstseries,secondseries), ROW("value", 10000)),&lt;BR /&gt;"Name",&lt;BR /&gt;IF([value]&amp;gt;100, "All", "Last " &amp;amp; [value])&lt;BR /&gt;)&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Issue:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The problem arises when the Last X Items filter includes non-continuous values, leading to incorrect results.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;Thanks in Advance for your help!&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 19:53:43 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Power-BI-DAX-Query-Issue-Last-X-Items-Filter-Consideration/m-p/3566981#M137506</guid>
      <dc:creator>codingenthu</dc:creator>
      <dc:date>2023-12-04T19:53:43Z</dc:date>
    </item>
    <item>
      <title>Re: Power BI DAX Query Issue: Last X Items Filter Consideration</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Power-BI-DAX-Query-Issue-Last-X-Items-Filter-Consideration/m-p/3567612#M137535</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="647695" data-lia-user-login="codingenthu" class="lia-mention lia-mention-user"&gt;codingenthu&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Calculate the Sum for the Identified Items: Once you have the last X items, you can proceed to calculate the sum for these items.&lt;/P&gt;&lt;P&gt;Here's a revised version of your CostSumByItem measure:&lt;/P&gt;&lt;LI-CODE lang="php"&gt;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] &amp;lt;= selectedItemCount )
RETURN
    SUMX ( FilteredItems, CALCULATE ( SUM ( CostCountByItem[Count] ) ) )&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;In this revised measure:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RankedItems&lt;/STRONG&gt; creates a rank for each distinct item in descending order.&lt;BR /&gt;&lt;STRONG&gt;FilteredItems&lt;/STRONG&gt; filters the ranked items to keep only the top X items as per your selected item count.&lt;BR /&gt;The SUMX function then calculates the sum of Count for these filtered items&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If this post &lt;STRONG&gt;helps&lt;/STRONG&gt;, please consider &lt;STRONG&gt;accepting&lt;/STRONG&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;it as the solution&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;to help the other members find it more quickly.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Appreciate your Kudos!!&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.credly.com/users/vahid-doustimajd/badges" target="_blank"&gt;&lt;img /&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.linkedin.com/in/vahid-dm/" target="_blank" rel="noopener noreferrer"&gt;LinkedIn&lt;/A&gt; | &lt;A href="https://twitter.com/VahidDMcom" target="_blank" rel="noopener noreferrer"&gt;Twitter&lt;/A&gt; | &lt;A href="https://www.vahiddm.com/" target="_blank" rel="noopener noreferrer"&gt;Blog&amp;nbsp;&lt;/A&gt;| &lt;A href="https://www.youtube.com/channel/UCF-uQfGF8de-EWnR9b2UQhQ" target="_blank" rel="noopener noreferrer"&gt;YouTube&lt;/A&gt;&lt;A href="https://www.youtube.com/channel/UCF-uQfGF8de-EWnR9b2UQhQ" target="_blank" rel="noopener noreferrer"&gt;&amp;nbsp;&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2023 05:01:41 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Power-BI-DAX-Query-Issue-Last-X-Items-Filter-Consideration/m-p/3567612#M137535</guid>
      <dc:creator>VahidDM</dc:creator>
      <dc:date>2023-12-05T05:01:41Z</dc:date>
    </item>
  </channel>
</rss>

