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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
MichaelBauld
Frequent Visitor

Problem with pulling latest value from a table based on date filter.

Hi there,

 

I'm building a corporate scorecard and am running into an issue where a certain DAX measure which pulls the latest KPI score for my Conservation Performance Indicator (CPI) works perfectly, but behaves completely differently when I use the same approach on a near identical table holding the CPI Targets. 

 

CPI Actuals

Valid FromValid ToCPI Actuals
01/03/202028/02/20210.653
01/03/202128/02/20220.669
01/03/202228/02/20230.679

 

CPI Targets

CPI TargetValid FromValid To
0.6501/03/202028/02/2021
0.6701/03/202128/02/2022
0.6801/03/202228/02/2023
0.7001/03/202329/02/2024
0.7201/03/202428/02/2025
0.7401/03/202528/02/2026
0.7601/03/202628/02/2027
0.7801/03/202729/02/2028
0.8001/03/202828/02/2029
0.8201/03/202928/02/2030
0.8401/03/203028/02/2031
0.8601/03/203129/02/2032
0.8801/03/203228/02/2033

 

Both fact tables are related to a calendar table via a many to one single sided relationship. 

 

To get the current KPI based on the selected date on my dashboard I am using the following DAX measure:

 

 

CPI Actuals = 
VAR _LastDtInRange = MAX( 'Calendar'[Date] )
VAR _AllCurrentAndPreviousKPIs = CALCULATETABLE( 'CPI Annual Scores', 'Calendar'[Date] <= _LastDtInRange )
VAR _LatestKPIrow = TOPN( 1, _AllCurrentAndPreviousKPIs , 'CPI Annual Scores'[Valid from] , DESC )
VAR _LatestKPIval = LASTNONBLANK( CALCULATETABLE( VALUES( 'CPI Annual Scores'[CPI] ), _LatestKPIrow ), 1 )
RETURN
_LatestKPIval

 

 

 

To get the same results but for the CPI target I simply reworked the above DAX to refer to the CPI Target fact table:

 

 

 

CPI Target = 
VAR _LastDtInRange = MAX( 'Calendar'[Date] )
VAR _AllCurrentAndPreviousKPIs = CALCULATETABLE( 'CPI Targets', 'Calendar'[Date] <= _LastDtInRange )
VAR _LatestKPIrow = TOPN( 1, _AllCurrentAndPreviousKPIs , 'CPI Targets'[Valid From] , DESC )
VAR _LatestKPIval = LASTNONBLANK( CALCULATETABLE( VALUES( 'CPI Targets'[CPI Target] ), _LatestKPIrow ), 1 )
RETURN
_LatestKPIval

 

 

 

In my mind this should be straightforward and deliver the same result for both the Actuals and Targets, but when I display the outputs in a matrix the actuals display correctly, but the target data just pulls through the last value in the table (0.88) for every date in the calendar table.

 

YearMonth TextCPI Target CPI Actuals
 88.00% 
2019 Mar88.00% 
2019 Apr88.00% 
2019 May88.00% 
2019 Jun88.00% 
2019 Jul88.00% 
2019 Aug88.00% 
2019 Sep88.00% 
2019 Oct88.00% 
2019 Nov88.00% 
2019 Dec88.00% 
2020 Jan88.00% 
2020 Feb88.00% 
2020 Mar88.00%65.30%
2020 Apr88.00%65.30%
2020 May88.00%65.30%
2020 Jun88.00%65.30%
2020 Jul88.00%65.30%
2020 Aug88.00%65.30%
2020 Sep88.00%65.30%
2020 Oct88.00%65.30%
2020 Nov88.00%65.30%
2020 Dec88.00%65.30%
2021 Jan88.00%65.30%
2021 Feb88.00%65.30%
2021 Mar88.00%66.90%
2021 Apr88.00%66.90%
2021 May88.00%66.90%
2021 Jun88.00%66.90%
2021 Jul88.00%66.90%
2021 Aug88.00%66.90%
2021 Sep88.00%66.90%
2021 Oct88.00%66.90%
2021 Nov88.00%66.90%
2021 Dec88.00%66.90%
2022 Jan88.00%66.90%
2022 Feb88.00%66.90%
2022 Mar88.00%67.90%
2022 Apr88.00%67.90%
2022 May88.00%67.90%
2022 Jun88.00%67.90%
2022 Jul88.00%67.90%
2022 Aug88.00%67.90%

 

I've checked the relationship to the calendar table and it looks correct as well as checking that the 'valid from' columns are correctly formatted as dates (they are). Any other suggestions as to what might be going on here would be greatly appreciated as I can't seem to figure it out! 

 

Thanks in advance for any help you can give.

 

Kind regards

Michael

1 ACCEPTED SOLUTION
onurbmiguel_
Super User
Super User

Hello Michael 

Please take a look at this CPI.pbix 

https://1drv.ms/u/s!AkcWVrMFkXs1h7Req7Ff1DU5YZAloQ?e=w1e46L

 

Any question please ask 

 

Best regards

Bruno Costa | Continued Contributor

 

Did I help you to answer your question? Accepted my post as a solution! Appreciate your Kudos!! 👍

Take a look at the blog: PBI Portugal 

 

wp-1586527108426

 


Best regards


Bruno Costa | Super User


 


Did I help you to answer your question? Accepted my post as a solution! Appreciate your Kudos!! 


Take a look at the blog: PBI Portugal 


 


View solution in original post

3 REPLIES 3
MichaelBauld
Frequent Visitor

Thanks @onurbmiguel_ , that suggestion seems to work!

 

Cheers

Michael

onurbmiguel_
Super User
Super User

Hello Michael 

Please take a look at this CPI.pbix 

https://1drv.ms/u/s!AkcWVrMFkXs1h7Req7Ff1DU5YZAloQ?e=w1e46L

 

Any question please ask 

 

Best regards

Bruno Costa | Continued Contributor

 

Did I help you to answer your question? Accepted my post as a solution! Appreciate your Kudos!! 👍

Take a look at the blog: PBI Portugal 

 

wp-1586527108426

 


Best regards


Bruno Costa | Super User


 


Did I help you to answer your question? Accepted my post as a solution! Appreciate your Kudos!! 


Take a look at the blog: PBI Portugal 


 


Hi @onurbmiguel_ ,

 

I've been working with your solution but have noticed that it is returning only values for the 'Valid From' month, not all months between the valid from and valid to dates. This is fine when the user is just defaulting to all dates in the current year, but when they select a specified month (say July 2022) then the values returned are 'Blank'.

 

YearMonth Text% CPI Actuals% CPI Targets
2022 Mar67.90%68.00%
2022 Apr  
2022 May  
2022 Jun  
2022 Jul  
2022 Aug  

 

 

% CPI Targets = 
 var _max = MAX('calendar'[Date])

 var _value = 
    CALCULATE(
        AVERAGE('CPI Targets'[CPI Target]),
        'CPI Targets'[Valid From]<=_max,
        'CPI Targets'[Valid To]>=_max
    )
return 
_value
 

 

 

Do you have any thoughts on why this is?

 

Cheers

Michael

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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