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
jimbob2285
Advocate II
Advocate II

SEARCH function only working when the field is displayed

I need the user to be able to select a date period for a sales table: Year-To-Date (YTD), Month-To-Date (MTD) & Week-To-Date (WTD) - But as MTD includes WTD and YTD includes MTD this can't simply filter on a column (see example below assuming we're currently in Week 3 of July):

MonthWeekPeriod
JulyWeek 1MTD
JulyWeek 2MTD
JulyWeek 3WTD

as MTD (weeks 1 & 2) would exclude WTD (week 3) and as you can see MTD needs to include week 3

 

To overcome this I have:

  • A calculated column in my sales table that assigns the appropriate period to each record by comparing the sales date with the current date and assigns "MTD/WTD" to week 3 (as opposed to just "WTD" in the example above)
  • Created a Slicer table to hold the values for the Chiclet Slicer and (YTD, MTD & WTD)
  • A measure (SelectedPeriod) in my sales table to search for the SELECTEDVALUE from the Slicer table in the Period calculated column in the Sales table

This seems to be working OK, but only when the SalesTable[Period] column is shown in the table visual - which is causing duplication, so where the table is showing sales by sales team for instance, adding the period column is duplicating sales teams for "MTD" & "MTD/WTD"

 

SelectedPeriod = VAR searchvalue=SEARCH(SELECTEDVALUE(SlicerTable[Period]), SELECTEDVALUE(SalesTable[Period]),,Blank())
RETURN
If(searchvalue>0,"Found")

 

Why is it only working when the Period column is shown in the table visual?

 

Any help would be appreciated as this is driving me crazy and I can't think of another way to achieve this... I don't want users to have to select both MTD & WTD to get MTD sales values

 

Thanks

Jim

1 ACCEPTED SOLUTION

Of course! Normally what you would do is make sure that you have a calendar table attached to your fact table.

This will mean that any filter you apply to your calendar will affect your fact table. 

So you can try something along the lines of this:

MTD = 
VAR Year_ = YEAR(TODAY())
VAR Month_ = MONTH(TODAY()) 
Return
Calculate( [Sales] , ALL(Calendar) , Calendar[Year] = Year_ , Calendar[Month] = Month_ , Calendar[Date] <= TODAY())


This should always return the current MTD Sales Value. 

Br, 
J


Connect on LinkedIn

View solution in original post

4 REPLIES 4
v-shex-msft
Community Support
Community Support

Hi @jimbob2285,

Did tex628 's suggestions help with your scenario? if that is the case, you can consider Kudo or accept the helpful suggestions to help others who faced similar requirements to find it more quickly.

If these also don't help, please share more detailed information to help us clarify your scenario to test.

How to Get Your Question Answered Quickly 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
tex628
Community Champion
Community Champion

Hi @jimbob2285 , 

I'd advice you to create 3 different measures instead. 

[WTD], [MTD] & [YTD] 

Then use a switch statement to swap between them depending on the slicer selection: 

Switch measure = 
SWITCH(
SELECTEDVALUE( SlicerTable[Period] )
"WTD" , [WTD] ,
"MTD" , [MTD] , 
"YTD" , [YTD] 
)


Br, 
J


Connect on LinkedIn

Hi, thanks for this, forgive my ignorance, I'm quite new to DAX, but I'm struggling to understand what the three measures would look like - For instance, for MTD I've tried an if statement to produce a 1 or 0, but that's not working 

 

Could you give me an example please

 

Here's a link to my example file: Example File 

 

Thanks

Jim

Of course! Normally what you would do is make sure that you have a calendar table attached to your fact table.

This will mean that any filter you apply to your calendar will affect your fact table. 

So you can try something along the lines of this:

MTD = 
VAR Year_ = YEAR(TODAY())
VAR Month_ = MONTH(TODAY()) 
Return
Calculate( [Sales] , ALL(Calendar) , Calendar[Year] = Year_ , Calendar[Month] = Month_ , Calendar[Date] <= TODAY())


This should always return the current MTD Sales Value. 

Br, 
J


Connect on LinkedIn

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