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
chefe
Helper II
Helper II

filter dataset for only last 20 working days in DAX

Hi there,

 

I am trying to to write e measure that returns a simple count. The only catch is that it should filter the underlying dataset to only the last 20 working days. For this I have a proper date table ("Dates") which recognises working days with the field [IS_WORKING_DAY]. 

 

I successfully tested the formula via creating a custom table with it, please see the following screenshot. 

 

T = 
VAR tbl_filtered_is_working_day = FILTER(Dates;Dates[IS_WORKING_DAY]=1)
VAR tbl_filtered_ranked = ADDCOLUMNS(SUMMARIZE(tbl_filtered_is_working_day;Dates[DATE_KEY]);"Rank";RANKX(tbl_filtered_is_working_day;Dates[DATE_KEY])) 
VAR tbl_filtered_top20 = FILTER(tbl_filtered_ranked;[Rank]<=20)
VAR tbl_last_20days = SUMMARIZE(tbl_filtered_top20;Dates[DATE_KEY])

RETURN tbl_last_20days

Only the last 20 working days are returned:

 

2_custom_table.png

 

Success so far. However, when I try to incorporate this into my measure, I does not apply the filter correctly. The measure formula is as follows, also please the the following screenshot which displays the result visually.

 

Count of last 20 days' CDS spreads TEST = 

VAR tbl_filtered_is_working_day = FILTER(Dates;Dates[IS_WORKING_DAY]=1)
VAR tbl_filtered_ranked = ADDCOLUMNS(SUMMARIZE(tbl_filtered_is_working_day;Dates[DATE_KEY]);"Rank";RANKX(tbl_filtered_is_working_day;Dates[DATE_KEY])) 
VAR tbl_filtered_top20 = FILTER(tbl_filtered_ranked;[Rank]<=20)
VAR tbl_last_20days = SUMMARIZE(tbl_filtered_top20;Dates[DATE_KEY])

RETURN IF(HASONEVALUE(Dates[DATE_KEY])
	;CALCULATE([Count of CDS spreads]
		;FILTER(Dates;Dates[DATE_KEY]>=FIRSTDATE(tbl_last_20days))
	)
	;1 //for row total, dummy = 1 for now
)

More results are returned than expected. Does anyone know why? The beginning formula is identical.

1_different counts.png

 

Any help is greatly appreciated!

Cheers

1 ACCEPTED SOLUTION
jahida
Impactful Individual
Impactful Individual

Guessing it's because the very first FILTER is only working on the Dates in the current context, which means it will pull 20 days back from any starting date. Try changing the first line to:

 

FILTER(ALL(Dates); Dates[IS_WORKING_DAY]=1)

 

might not work but worth a shot.

View solution in original post

3 REPLIES 3
v-haibl-msft
Employee
Employee

@chefe

 

As I understand it, you only want to show the last 20 days in the Table chart. It seems that you select Dates[DATE_KEY] for Values of Table chart. If so, all dates will be shown in the Table chart. You can try to select T[DATE_Key] for Values of Table chart.

If you still cannot get your problem resolved, could you please also provide the measure formula of [Count of CDS spreads]?

 

Best Regards,

Herbert

jahida
Impactful Individual
Impactful Individual

Guessing it's because the very first FILTER is only working on the Dates in the current context, which means it will pull 20 days back from any starting date. Try changing the first line to:

 

FILTER(ALL(Dates); Dates[IS_WORKING_DAY]=1)

 

might not work but worth a shot.

@jahida Thanks that worked. Nice.

Though I still need to wrap my head around this (even though you already provided explanation) 😕

I'm still new @ "thinking DAX" 

 

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.