March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hi,
I'm new at Power BI and got asked to solve a task my co-worker has.
The data model is for a restaurant chain that wants to be able to compare different locations, years and months with a flag to be able to remove locations that weren't open yet for the time of the comparison.
The data model has three tables:
The task is to create a filter that can be used when comparing years.
So for an example:
When April 2021 is selected in the Date Table, and 2018 is selected in the CompareYears table. Only restaurants that opened BEFORE April 2018 should be shown when this filter is active.
I tried scripting this with creating a measure in the Venures table looking something like this:
FlagOpeningDate =
VAR CY =
CALCULATETABLE(
VALUES('Venues'[Restaurant]),
FILTER('Venues',YEAR('Venues'[openingDate]) < (MAX('CompareYear'[CompareYear]))),
FILTER('Venues',MONTH(Venues[openingDate]) < (MAX('Date Table'[Month])))
)
RETURN
IF(
COUNTROWS(INTERSECT(CY,VALUES('Venues'[Restaurant]))) > 0, 1,0
)
But it didn't work. How is it best to solve this?
Thanks in advance!
Sincerely
Ash
Solved! Go to Solution.
// Create a filtering measure that
// will return 1 for the restaurants
// you want to see and 0 otherwise.
// Then, when you display your restaurants
// in a table or matrix, just put this
// measure in the Filter Pane of the visual
// and set the "is" condition to 1.
[Show Restaurant?] =
IF( ISINSCOPE( Venue[Restaurant] ),
var OpenDate = SELECTEDVALUE( Venue[OpeningDate] )
var ComparisonYear = SELECTEDVALUE( CompareYear[CompareYear] )
var BaseMonthNumber = MONTH( MIN( 'Date Table'[Date] ) )
var CutoffDate = DATE( ComparisonYear, BaseMonthNumber, 1 )
var ShouldShow = OpenDate < CutoffDate
return
INT( ShouldShow )
)
Hi,
I got it working with daxer's solution.
Thanks for the help!
Sincerely
Ash
// Create a filtering measure that
// will return 1 for the restaurants
// you want to see and 0 otherwise.
// Then, when you display your restaurants
// in a table or matrix, just put this
// measure in the Filter Pane of the visual
// and set the "is" condition to 1.
[Show Restaurant?] =
IF( ISINSCOPE( Venue[Restaurant] ),
var OpenDate = SELECTEDVALUE( Venue[OpeningDate] )
var ComparisonYear = SELECTEDVALUE( CompareYear[CompareYear] )
var BaseMonthNumber = MONTH( MIN( 'Date Table'[Date] ) )
var CutoffDate = DATE( ComparisonYear, BaseMonthNumber, 1 )
var ShouldShow = OpenDate < CutoffDate
return
INT( ShouldShow )
)
Hi daxer,
This seems to be quite close to the solution. But I can still see 1's on the rows before the restaurant opened when I add the fields to a straight table as seen below. The restaurant in Berlin opened in May 2019 (0 on the far right on that row).
And is it possible to add this as an optional filter? So that it is triggered when a button in a box is pressed?
Sincerely,
Ash
Hi @Anonymous,
Can you please share the sample pbix file to test? It should help us clarify your scenario and test to coding formula.
How to Get Your Question Answered Quickly
Regards,
Xiaoxin Sheng
Venue[Restaurant] is assumed in my code to be the unique identifier of a restaurant. This field can't have the same value across different restaurants. What's more, a restaurant must have exactly one opening date and if this is not the case in the code I've given you, SELECTEDVALUE will return BLANK and hence the comparison OpenDate < CutoffDate will return TRUE, which means such a row will be displayed.
You have to adjust the code to your model for this to work correctly.
Hi,
You can use the following formula to count restaurants opened
Proud to be a Super User!
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
125 | |
85 | |
69 | |
54 | |
45 |
User | Count |
---|---|
204 | |
106 | |
98 | |
65 | |
54 |