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
harringtonc
Regular Visitor

DAX formula to show latest client change row

Hi all,

I have a DAX formula in Analysis Services:

 

=MAXX( FILTER('Client Changes',

                          'Client Changes'[dclc_dcli_id]=EARLIER('Client Changes'[dclc_dcli_id])

                         ),

               'Client Changes'[dclc_id]

             )

 

I use this formula to get the latest change record ID by grouping by each distinct client. This works fine.

(this ID can then be used to compare the latest value of any client attribute with previous values to report on changes)

I also have a relationship between my Dates table and 'Client Changes'[Valid To Date], which is the date on which each change was made.

 

In PowerBI I have a date selection filter which filters the client change records based on their Valid To Date. This filter works fine.

 

The issue I have is that I am having trouble getting the Latest Change ID formula above to "see" the date filter, i.e. I only want to show tyhe latest change ID using the client change records filter by the date selection filter not all records.

 

So what I want to do is to add a date criteria to the filter which will take into account the maximum Valid To Date as specified by the Power BI date filter.

 

Thanks,

Carl

7 REPLIES 7
Anonymous
Not applicable

Why are you using EARLIER in your measure even if there is no OUTER row context to reach? What is this filter actually doing:

 

    FILTER (
        'Client Changes',
        'Client Changes'[dclc_dcli_id] = EARLIER ( 'Client Changes'[dclc_dcli_id] )
    )

I think it's doing nothing since this 'Client Changes'[dclc_dcli_id] = EARLIER ( 'Client Changes'[dclc_dcli_id] ) is ALWAYS TRUE.

 

Would this measure not do the same thing as yours?

 

MAXX (
    'Client Changes',
    'Client Changes'[dclc_id]
)

// or even this one:
MAX ( 'Client Changes'[dclc_id] )

By the way, this max should honor any filters that you put on 'Client Changes'. So, if your Date table is tied to a date column in 'Client Changes', then the above should return the max dclc_id that's visible in the current context.

 

Best

Darek

Hi Darek,

Thanks so much for the quick reply.

I'm using EARLIER in the filter to act as a PARTITION BY clause. There are loads of different client records, so without the EARLIER clause then it will return the MAX dclc_id for the entire table, not the MAX dclc_id for each client:

 

dclc_iddclc_dcli_idValid FromValid ToCTYLatest Client Record IdCurrent CTYCalculated Valid To DateMAX Client Record Id
203747516146601/01/199022/01/2015ITA2037477ITA22/01/20152118948
203747616146623/01/201528/07/2016GBR2037477ITA28/07/20162118948
203747716146629/07/2016 ITA2037477ITA09/07/20192118948

 

The above data only shows the record for Client 161466 and the change record 2037477 is the latest change record for this client. What I want to achive in my report is when the user selects a To Date of say 28/07/2016 then the Latest Client Record ID would change to 2037476.

 

The Latest Client Record ID column is calculated using:

 

=MAXX(FILTER('Client Changes'
,'Client Changes'[dclc_dcli_id]=EARLIER('Client Changes'[dclc_dcli_id]
)
,'Client Changes'[dclc_id]
)

Without the EARLIER clause I get the maximum DCLC_ID in the entire table, Client Record Id:

 

=MAXX(FILTER('Client Changes'
,'Client Changes'[dclc_dcli_id]='Client Changes'[dclc_dcli_id]
)
,'Client Changes'[dclc_id]
)

I get the same result for your suggestion:

=MAXX('Client Changes'
     ,'Client Changes'[dclc_id]
     )

I'm pretty new to DAX and Power BI so I could be using completely the wrong technique.

But what I'm trying to achive is to change my Latest Client Id formula to expand the filter to compare something (parameter) to the Calculated Valid To Date field in order to get the latest client change record on or before the date slected.

 

Thanks,

Carl

Anonymous
Not applicable

Carl, are you talking about DAX measures or calculated columns? I have a feeling the latter is the case...

 

Best

Darek

Hi Darek,

Yes, these are calculated columns, sorry should have been more specific (complete n00b, only started using PowerBI and Analysis Services last week!).

Is it because they're not setup as Measures that the columns are not date aware?

 

Thanks,

Carl

Anonymous
Not applicable

OK.

 

First of all, for what you want you cannot have a calculated column, only a measure. Here's the first variation of the measure (since I don't know what version you need as it's not clear from your description):

 

[Latest Client Record ID] =
var __onlyOneClientVisible = HASONEVALUE( 'Client Changes'[dclc_dcli_id] )
var __dateSelected = SELECTEDVALUE( ValidToDateSlicer[Valid To] )
var __latestClientRecordID =
	CALCULATE(
		MAX( 'Client Changes'[dclc_id] ),
		'Client Changes'[Calculated Valid To Date] <= __dateSelected,
		ALLEXCEPT( 'Client Changes', Client Changes'[dclc_dcli_id] )
	)
return
	if( __onlyOneClientVisible, __latestClientRecordID, "Many Clients Visible")

Here's another variation:

[Latest Client Record ID] =
var __onlyOneClientVisible = HASONEVALUE( 'Client Changes'[dclc_dcli_id] )
var __dateSelected = SELECTEDVALUE( ValidToDateSlicer[Valid To] )
var __latestClientRecordID =
	CALCULATE(
		MAX( 'Client Changes'[dclc_id] ),
		'Client Changes'[Calculated Valid To Date] <= __dateSelected
	)
return
	if( __onlyOneClientVisible, __latestClientRecordID, "Many Clients Visible")

The difference between them is that the first does not honour any filters on the 'Client Changes' table (apart from the one on dclc_dcli_id). The other one does honour all filters on the table apart from [Calculated Valid To Date].

 

There's still another version that does honour all filters, even on [Calculated Valid To Date]:

[Latest Client Record ID] =
var __onlyOneClientVisible = HASONEVALUE( 'Client Changes'[dclc_dcli_id] )
var __dateSelected = SELECTEDVALUE( ValidToDateSlicer[Valid To] )
var __latestClientRecordID =
	CALCULATE(
		MAX( 'Client Changes'[dclc_id] ),
		KEEPFILTERS( 'Client Changes'[Calculated Valid To Date] <= __dateSelected )
	)
return
	if( __onlyOneClientVisible, __latestClientRecordID, "Many Clients Visible")

Please note that if more than one client is visible in the current context, "Many Clients Visible" will be returned. This is because it makes no sense to calculate latest record id for many clients at the same time.

 

For the above to work you need a table ValidToDateSlicer that will store all the dates you'd like to use as the Valid To date that your users would select. Please note that this table should NOT be linked to 'Client Changes' in any way. It's a parameter table.

 

Best

Darek

Hi,

Thanks for taking the time to look into this problem, I appreciate it.

Thanks,

Carl

Anonymous
Not applicable

No, that's not why I was asking. Bear with me, please. I'll show you the right formula.

 

Best

Darek

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.

Top Solution Authors