{"id":1550,"date":"2026-01-08T12:28:13","date_gmt":"2026-01-08T10:28:13","guid":{"rendered":"https:\/\/www.flip-design.de\/?p=1550"},"modified":"2026-01-08T12:28:13","modified_gmt":"2026-01-08T10:28:13","slug":"how-do-i-display-the-second-to-last-status-of-a-row-within-power-bi-using-dax","status":"publish","type":"post","link":"https:\/\/www.flip-design.de\/?p=1550","title":{"rendered":"How do I display the second-to-last status of a row within Power BI using DAX?"},"content":{"rendered":"\n<p>The initial situation was that the data was being read live from a streaming database. Each record had a current status, and the records could be grouped by product. Now I only want to see the latest record, but also have the previous status displayed in the row. Calculated columns are not an option because the data is being retrieved live. Therefore, this has to be solved using measures.<\/p>\n\n\n\n<p>I&#8217;d like to show you how this works in this post.<\/p>\n\n\n\n<p>Here is my example data:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image.png\"><img decoding=\"async\" loading=\"lazy\" width=\"453\" height=\"362\" src=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image.png\" alt=\"\" class=\"wp-image-1551\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image.png 453w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-300x240.png 300w\" sizes=\"(max-width: 453px) 100vw, 453px\" \/><\/a><\/figure>\n\n\n\n<p>If I now visualize the data and sort it by date, it looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-1.png\"><img decoding=\"async\" loading=\"lazy\" width=\"464\" height=\"337\" src=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-1.png\" alt=\"\" class=\"wp-image-1552\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-1.png 464w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-1-300x218.png 300w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/a><\/figure>\n\n\n\n<p>For example, for ID C003, this would mean that the previous status in the first line should be displayed as invalid.<\/p>\n\n\n\n<p>As a first step, I created a measure that displays the row number for the respective data record:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RowNumberPerID = \r\nVAR CurrentDateTime =\r\n    SELECTEDVALUE ( data&#91;datetime] )\r\nVAR CurrentID =\r\n    SELECTEDVALUE ( data&#91;id] )\r\nRETURN\r\nCALCULATE (\r\n    COUNTROWS ( data ),\r\n    FILTER (\r\n        ALL ( data ),          \r\n        data&#91;id] = CurrentID\r\n            &amp;&amp; data&#91;datetime] >= CurrentDateTime\r\n    )\r\n)\r\n<\/code><\/pre>\n\n\n\n<p>The line number is sorted in such a way that the last record is assigned a 1. This is necessary for later display. <\/p>\n\n\n\n<p>This metric generates an additional ID in the window for each ID:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-2.png\"><img decoding=\"async\" loading=\"lazy\" width=\"691\" height=\"364\" src=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-2.png\" alt=\"\" class=\"wp-image-1553\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-2.png 691w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-2-300x158.png 300w\" sizes=\"(max-width: 691px) 100vw, 691px\" \/><\/a><\/figure>\n\n\n\n<p>So, essentially, this is classic windowing, similar to what you might know from SQL Server with the PARTITION_BY command.<\/p>\n\n\n\n<p>Next, the actual metric is needed. While it doesn&#8217;t require the previous measure to determine the status, doing so makes it easier to visualize the actual process and filter the results.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Previous Status = \r\nVAR CurrentId =\r\n    SELECTEDVALUE ( data&#91;id] )\r\nVAR CurrentDateTime =\r\n    SELECTEDVALUE ( data&#91;datetime] )\r\nVAR PrevDateTime =\r\n    CALCULATE (\r\n        MAX ( data&#91;datetime] ),\r\n        FILTER (\r\n            ALL ( data ),\r\n            data&#91;id] = CurrentId\r\n                &amp;&amp; data&#91;datetime] &lt; CurrentDateTime\r\n        )\r\n    )\r\nRETURN\r\nCALCULATE (\r\n    MAX ( data&#91;status] ),\r\n    FILTER (\r\n        ALL ( data ),\r\n        data&#91;id] = CurrentId\r\n            &amp;&amp; data&#91;datetime] = PrevDateTime\r\n    )\r\n)\r\n<\/code><\/pre>\n\n\n\n<p>Using this metric, the previous data record is searched for based on the date, and the status is extracted. This then leads to the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-3.png\"><img decoding=\"async\" loading=\"lazy\" width=\"814\" height=\"362\" src=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-3.png\" alt=\"\" class=\"wp-image-1554\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-3.png 814w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-3-300x133.png 300w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-3-768x342.png 768w\" sizes=\"(max-width: 814px) 100vw, 814px\" \/><\/a><\/figure>\n\n\n\n<p><strong>Now a filter can be applied to the visual to display only the last record in a table:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-4.png\"><img decoding=\"async\" loading=\"lazy\" width=\"940\" height=\"279\" src=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-4.png\" alt=\"\" class=\"wp-image-1555\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-4.png 940w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-4-300x89.png 300w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/01\/image-4-768x228.png 768w\" sizes=\"(max-width: 940px) 100vw, 940px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The initial situation was that the data was being read live from a streaming database. Each record had a current status, and the records could be grouped by product. Now I only want to see the latest record, but also &hellip; <a href=\"https:\/\/www.flip-design.de\/?p=1550\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/1550"}],"collection":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1550"}],"version-history":[{"count":1,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/1550\/revisions"}],"predecessor-version":[{"id":1556,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/1550\/revisions\/1556"}],"wp:attachment":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}