<?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: Trying to display the most recent role for a given date range. in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Trying-to-display-the-most-recent-role-for-a-given-date-range/m-p/3780749#M147716</link>
    <description>&lt;P&gt;Thanks 123ABC,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That certainly made a difference and doesn't return the multiple rows error, but using that code I'm now finding that the current role doesn't change as I select months. For example: Employee C returns Team Leader regardless of whether I select April, May or June even though Employee C should be showing as Secondment from May onwards.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And apologies, I would load a PBIX file in the thread but I don't have the ability to do so due to being relatively new to the forum.&lt;/P&gt;</description>
    <pubDate>Wed, 20 Mar 2024 21:46:11 GMT</pubDate>
    <dc:creator>Oberon</dc:creator>
    <dc:date>2024-03-20T21:46:11Z</dc:date>
    <item>
      <title>Trying to display the most recent role for a given date range.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Trying-to-display-the-most-recent-role-for-a-given-date-range/m-p/3776380#M147601</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I've got the following table:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;In a visuilization I'm using an unrelated date table to select a date and want a table that returns the latest 'Role' for each 'Agent Name'. (or 'Employee ID')&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;On the visualisation I'm using date slicers and then measures to try and achieve the result.&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;The measure I'm using to get the latest 'Role' for the selected date is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Current role = 
var _selectedDateRangeEnd=LASTDATE('Date'[Date])
var _selectedDateRangeStart=FIRSTDATE('Date'[Date])
var _EmploymentsBeforeDateRange= FILTER(
    EmploymentHistory,
    EmploymentHistory[Date]&amp;lt;=_selectedDateRangeEnd)
var _LastRoleChangeDate=MAXX(
    _EmploymentsBeforeDateRange,
    EmploymentHistory[date])
var _lastRole=LOOKUPVALUE(
    EmploymentHistory[Role],
    EmploymentHistory[Date],
    _LastRoleChangeDate)
return
if(
    COUNTROWS(VALUES(EmploymentHistory[agent Name]))=1,
    _lastRole
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For some reason it works fine if there is more than 1 result returned for any given agent name however I get&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;"A table of multiple values was supplied where a single value was expected" as soon as there's only a single result returned.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;I've tried debugging and am fairly sure the issue is the lookupvalues part of the formula but can't work out why or how to fix it. (possibly something about returning the first value?) I tried FIRSTNONBLANK after some searching but either can't figure out the syntax or it won't work in this case. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;Happy to try anything which will work even if the method is completely different! &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 22:58:08 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Trying-to-display-the-most-recent-role-for-a-given-date-range/m-p/3776380#M147601</guid>
      <dc:creator>Oberon</dc:creator>
      <dc:date>2024-03-19T22:58:08Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to display the most recent role for a given date range.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Trying-to-display-the-most-recent-role-for-a-given-date-range/m-p/3777082#M147628</link>
      <description>&lt;P&gt;It seems like you're encountering an issue when there's only one result returned for a given agent name. This could be due to the LOOKUPVALUE function expecting a single value but receiving multiple values in certain cases.&lt;/P&gt;&lt;P&gt;To address this issue, you can modify your DAX measure to handle cases where there's only one result differently. One approach could be to check if there's only one result for the agent name and if so, directly return that result, otherwise proceed with your existing logic to find the latest role.&lt;/P&gt;&lt;P&gt;Here's how you can adjust your DAX measure:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Current role =&lt;BR /&gt;VAR _selectedDateRangeEnd = LASTDATE('Date'[Date])&lt;BR /&gt;VAR _selectedDateRangeStart = FIRSTDATE('Date'[Date])&lt;BR /&gt;VAR _EmploymentsBeforeDateRange =&lt;BR /&gt;FILTER(&lt;BR /&gt;EmploymentHistory,&lt;BR /&gt;EmploymentHistory[Date] &amp;lt;= _selectedDateRangeEnd&lt;BR /&gt;)&lt;BR /&gt;VAR _LastRoleChangeDate =&lt;BR /&gt;MAXX(&lt;BR /&gt;_EmploymentsBeforeDateRange,&lt;BR /&gt;EmploymentHistory[Date]&lt;BR /&gt;)&lt;BR /&gt;VAR _lastRole =&lt;BR /&gt;LOOKUPVALUE(&lt;BR /&gt;EmploymentHistory[Role],&lt;BR /&gt;EmploymentHistory[Date],&lt;BR /&gt;_LastRoleChangeDate&lt;BR /&gt;)&lt;BR /&gt;RETURN&lt;BR /&gt;IF(&lt;BR /&gt;COUNTROWS(VALUES(EmploymentHistory[agent Name])) = 1,&lt;BR /&gt;MAX(EmploymentHistory[Role]), // Return the single role directly&lt;BR /&gt;_lastRole // Use the existing logic to find the latest role&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this modification, if there's only one result for the agent name, it directly returns that role using the MAX function. Otherwise, it proceeds with your existing logic to find the latest role.&lt;/P&gt;&lt;P&gt;This adjustment should resolve the issue you're encountering when there's only one result for a given agent name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;If this post&amp;nbsp;helps, then please consider&amp;nbsp;Accepting it as the solution&amp;nbsp;to help the other members find it more quickly.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;In case there is still a problem, please feel free and explain your issue in detail,&amp;nbsp;It will be my pleasure to assist you in any way I can.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 05:18:30 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Trying-to-display-the-most-recent-role-for-a-given-date-range/m-p/3777082#M147628</guid>
      <dc:creator>123abc</dc:creator>
      <dc:date>2024-03-20T05:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to display the most recent role for a given date range.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Trying-to-display-the-most-recent-role-for-a-given-date-range/m-p/3780749#M147716</link>
      <description>&lt;P&gt;Thanks 123ABC,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That certainly made a difference and doesn't return the multiple rows error, but using that code I'm now finding that the current role doesn't change as I select months. For example: Employee C returns Team Leader regardless of whether I select April, May or June even though Employee C should be showing as Secondment from May onwards.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And apologies, I would load a PBIX file in the thread but I don't have the ability to do so due to being relatively new to the forum.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 21:46:11 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Trying-to-display-the-most-recent-role-for-a-given-date-range/m-p/3780749#M147716</guid>
      <dc:creator>Oberon</dc:creator>
      <dc:date>2024-03-20T21:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to display the most recent role for a given date range.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Trying-to-display-the-most-recent-role-for-a-given-date-range/m-p/3790817#M148121</link>
      <description>&lt;P&gt;&lt;FONT&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="693492" data-lia-user-login="Oberon" class="lia-mention lia-mention-user"&gt;Oberon&lt;/a&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I understand correctly, you need to show the most recent post for each Agent name in the corresponding date range. Here is the test I did.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My two slicer tables sample:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Create a measure as follow&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Measure = 
VAR _MaxDate = CALCULATE(MAX([Date]), FILTER('EmploymentHistory', [Date] &amp;lt;= MAX('Date'[Date]) &amp;amp;&amp;amp; [End Date] &amp;gt; MAX('Date'[Date]) || [Date] &amp;lt;= MAX('Date'[Date]) &amp;amp;&amp;amp; [End Date] = BLANK()))
RETURN
IF(SELECTEDVALUE(EmployeeID[Employee ID]) = BLANK() &amp;amp;&amp;amp; MAX([Date]) = _MaxDate, 1, IF(MAX([Date]) = _MaxDate &amp;amp;&amp;amp; MAX([Employee ID]) = SELECTEDVALUE(EmployeeID[Employee ID]), 1, 0))&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Put the measure into the visual-level filters, set up show items when the value is 1.&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Yulia Xu&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this post &lt;EM&gt;&lt;STRONG&gt;helps&lt;/STRONG&gt;&lt;/EM&gt;, then please consider &lt;EM&gt;&lt;STRONG&gt;Accept it as the solution&lt;/STRONG&gt;&lt;/EM&gt; to help the other members find it more quickly.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 06:44:48 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Trying-to-display-the-most-recent-role-for-a-given-date-range/m-p/3790817#M148121</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-03-26T06:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to display the most recent role for a given date range.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Trying-to-display-the-most-recent-role-for-a-given-date-range/m-p/3792359#M148201</link>
      <description>&lt;P&gt;Yes, this works exactly how I wanted! Thank you I knew there had to be a way but it was driving me insane trying to work it out!&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 21:47:18 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Trying-to-display-the-most-recent-role-for-a-given-date-range/m-p/3792359#M148201</guid>
      <dc:creator>Oberon</dc:creator>
      <dc:date>2024-03-26T21:47:18Z</dc:date>
    </item>
  </channel>
</rss>

