<?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 Re: Calculated columns for last and second last values for a measurement in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculated-columns-for-last-and-second-last-values-for-a/m-p/2893033#M93959</link>
    <description>&lt;P&gt;Here are some &lt;STRONG&gt;measures&lt;/STRONG&gt; you can try... Measures, not calculated colums.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;// Hidden helper measure; easier to debug
[_LatestDate] =
var Output =
    CALCULATE(
        MAX( Readings[taken_date] ),
        keepfilters(
            not isblank( Readings[value] )
    )
return
    Output

// Hidden helper measure
[_SecondLatestDate] =
var LatestDate = [_LatestDate]
var Output =
    CALCULATE (
        MAX ( Readings[taken_date] ),
        keepfilters(
            Readings[taken_date] &amp;lt; LatestDate
        )
    )
return
    Output

// This works on the assumption that
// there will only be at most 1 value
// for the LatestDate. If there are
// many, BLANK will be returned since
// this is the behaviour of SELECTEDVALUE.
// Same applies to the second measure.
[LatestValue] =
VAR LastestDate = [_LatestDate]
var Output = 
    CALCULATE(
        SELECTEDVALUE( Readings[value] ),
        keepfilters( 
            Readings[taken_date] = LatestDate
        )
    )
return
    Output
    
[SecondLatestValue] =
VAR SecondLastestDate = [_SecondLatestDate]
var Output =
    CALCULATE (
        SELECTEDVALUE ( Readings[value] ),
        keepfilters(
            Readings[taken_date] = SecondLatestDate
        )
    )
return
    Output
    
[Difference] = [LatestValue] - [SecondLatestValue]&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Nov 2022 16:53:36 GMT</pubDate>
    <dc:creator>daXtreme</dc:creator>
    <dc:date>2022-11-08T16:53:36Z</dc:date>
    <item>
      <title>Calculated columns for last and second last values for a measurement</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculated-columns-for-last-and-second-last-values-for-a/m-p/2892621#M93930</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am working on a project where is needed the last and second last readings for an asset and get the difference between values, so we can make some decisions afteerwards based on the difference. I created two calculated columns, but they are very expensive, especially the secondlastvalue formula. I shared a screenshot with the readings (value), dates (taken_date), where(taken_at_location), reading type (UT Reading Minimum) and asset. Also, the formulas for my last and second last values.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;latestvalue&lt;/STRONG&gt; =&lt;BR /&gt;VAR last_date =&lt;BR /&gt;CALCULATE ( MAX ( Readings[taken_date] ), Readings[value] &amp;lt;&amp;gt; BLANK () )&lt;BR /&gt;RETURN&lt;BR /&gt;CALCULATE (&lt;BR /&gt;SELECTEDVALUE ( Readings[value] ),&lt;BR /&gt;Readings[taken_date] = last_date&lt;BR /&gt;)&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;STRONG&gt;secondlastvalue&lt;/STRONG&gt; = &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;VAR&lt;/SPAN&gt; &lt;SPAN&gt;second_last_date&lt;/SPAN&gt;&lt;SPAN&gt; =&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;CALCULATE&lt;/SPAN&gt;&lt;SPAN&gt; (&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;MAX&lt;/SPAN&gt;&lt;SPAN&gt; ( &lt;/SPAN&gt;&lt;SPAN&gt;Readings&lt;/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lt;/SPAN&gt;&lt;SPAN&gt;Readings&lt;/SPAN&gt;&lt;SPAN&gt;[value]&lt;/SPAN&gt;&lt;SPAN&gt; ),&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;Readings&lt;/SPAN&gt;&lt;SPAN&gt;[taken_date]&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;second_last_date&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; )&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;You can see that for each location there are two readings (now they are the same, but is not the standard) for two dates. Can somebody take a look at the formulas, maybe there is another way to get those values?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Much appreciated!&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 14:46:51 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculated-columns-for-last-and-second-last-values-for-a/m-p/2892621#M93930</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-11-08T14:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated columns for last and second last values for a measurement</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculated-columns-for-last-and-second-last-values-for-a/m-p/2893033#M93959</link>
      <description>&lt;P&gt;Here are some &lt;STRONG&gt;measures&lt;/STRONG&gt; you can try... Measures, not calculated colums.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;// Hidden helper measure; easier to debug
[_LatestDate] =
var Output =
    CALCULATE(
        MAX( Readings[taken_date] ),
        keepfilters(
            not isblank( Readings[value] )
    )
return
    Output

// Hidden helper measure
[_SecondLatestDate] =
var LatestDate = [_LatestDate]
var Output =
    CALCULATE (
        MAX ( Readings[taken_date] ),
        keepfilters(
            Readings[taken_date] &amp;lt; LatestDate
        )
    )
return
    Output

// This works on the assumption that
// there will only be at most 1 value
// for the LatestDate. If there are
// many, BLANK will be returned since
// this is the behaviour of SELECTEDVALUE.
// Same applies to the second measure.
[LatestValue] =
VAR LastestDate = [_LatestDate]
var Output = 
    CALCULATE(
        SELECTEDVALUE( Readings[value] ),
        keepfilters( 
            Readings[taken_date] = LatestDate
        )
    )
return
    Output
    
[SecondLatestValue] =
VAR SecondLastestDate = [_SecondLatestDate]
var Output =
    CALCULATE (
        SELECTEDVALUE ( Readings[value] ),
        keepfilters(
            Readings[taken_date] = SecondLatestDate
        )
    )
return
    Output
    
[Difference] = [LatestValue] - [SecondLatestValue]&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 16:53:36 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculated-columns-for-last-and-second-last-values-for-a/m-p/2893033#M93959</guid>
      <dc:creator>daXtreme</dc:creator>
      <dc:date>2022-11-08T16:53:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated columns for last and second last values for a measurement</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculated-columns-for-last-and-second-last-values-for-a/m-p/2893143#M93973</link>
      <description>&lt;P&gt;Hi daXtreme, for some reason I get an error regarding the Return syntax!?&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;_LatestDate] =
var Output =
    CALCULATE(
        MAX( Readings[taken_date] ),
        keepfilters(
            not isblank( Readings[value] )
    )
return
    Output&lt;/PRE&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 17:43:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculated-columns-for-last-and-second-last-values-for-a/m-p/2893143#M93973</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-11-08T17:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated columns for last and second last values for a measurement</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculated-columns-for-last-and-second-last-values-for-a/m-p/2893158#M93975</link>
      <description>&lt;P&gt;Tried to format it in DAX Formatter and get the error at Return&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 17:46:24 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculated-columns-for-last-and-second-last-values-for-a/m-p/2893158#M93975</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-11-08T17:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: Calculated columns for last and second last values for a measurement</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculated-columns-for-last-and-second-last-values-for-a/m-p/2895597#M94111</link>
      <description>&lt;P&gt;Hi daXtreme, I manipulated the formulas to work, it is a bit faster. I like using the lastdate and secondlastdate measures. Thank you for your input.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2022 14:40:22 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculated-columns-for-last-and-second-last-values-for-a/m-p/2895597#M94111</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-11-09T14:40:22Z</dc:date>
    </item>
  </channel>
</rss>

