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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Michele_I
Frequent Visitor

dynamically exclude records based on user defined conditions

Hi all, and thank you for any suggestion with this little issue.

This is a semplified example, but my real model works the same way.

I have a projects table (every row is a project) and a Localizations table, in which every project has one row for each municipality involved.

Cattura.PNG

The user can select one or more municipalities (Localizations[Municipality]) using a slicer.

 

I'm looking for calculating the n. of projects and total budget of those projects localized only in one or more of the municipalities the user selected, but not in any other municipality not selected by the user.

 

Examples when the user choses municipalities A, B and C:
Project 1 localized in municipality A: included
Project 2 localized in municipality B: included
Project 3 localized in municipalities A and B: included
Project 4 localized in municipalities A, B and D : excluded
Project 5 localized in municipalities D and E: excluded

 

I manage to calculate the n. of projects and their budget localized in at least in one of the municipalities the user selected, but this would include projects 1, 2, 3 and 4 of the example, while i need project D being excluded as it is localized also in municipality D which is not in the list the user selected.

 

thank you very much, any help is really appreciated.

Michele

1 ACCEPTED SOLUTION

@Michele_I Well, if it works, it works. Probably not how I would have done it. I would have probably done something like:

Budget OK = 
  VAR __Table = FILTER( ADDCOLUMNS('Projects', "__Selected", [Selection Measure]), [__Selected] = 1)
  VAR __Result = SUMX(__Table, [budget])
RETURN
  __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Michele_I
Frequent Visitor

@Greg_Deckler thank you very much for helping me.

 

Thanks to your code i could get the table you see on the left, which correctly associates value 1 to projectIDs matching the requirements. Budget total is 100 but if i filter the table for "selection measure" = 1 i correctly get 60. I would never get here without your help!

Michele_I_0-1706646400969.png

 

This all works fine in a table with one record per project, but how can i use your code to build a measure that automatically calculates a total budget of 60 when municipalities A and B are selected?

 

To achieve this i tried adding some more code (it seems fine to me but i'm not sure): 

budget ok = VAR __Municipalities = SELECTCOLUMNS('Municipalities', "municipality", [municipality]) 
    VAR __ProjectID = MAX('Projects'[projectID])
    VAR __Localizations = SELECTCOLUMNS(FILTER(ALL('Localizations'), [projectID] = __ProjectID), "__municipality", [municipality]) 
    VAR __Except = EXCEPT(__Localizations, __Municipalities) 
    VAR __Intersect = INTERSECT(__Localizations, __Municipalities)
    VAR __Result = IF(COUNTROWS(__Except) = BLANK() && COUNTROWS(__Intersect) <> BLANK(), 1, 0)

    var __projecttable = ADDCOLUMNS(Projects,"Check",[Selection Measure])
    var __filteredprojecttable = FILTER(__projecttable, [Check]=1)

return

CALCULATE(SUM(Projects[budget]),__filteredprojecttable)

 

Is this the right way to use your tips?

Thank you very much!

 

@Michele_I Well, if it works, it works. Probably not how I would have done it. I would have probably done something like:

Budget OK = 
  VAR __Table = FILTER( ADDCOLUMNS('Projects', "__Selected", [Selection Measure]), [__Selected] = 1)
  VAR __Result = SUMX(__Table, [budget])
RETURN
  __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler Your way is much better 🙂

Thank you very much!

Greg_Deckler
Super User
Super User

@Michele_I Maybe something like this. See PBIX attached below signature.

Selection Measure = 
    VAR __Municipalities = SELECTCOLUMNS('Municipalities', "municipality", [municipality])
    VAR __ProjectID = MAX('Projects'[projectID])
    VAR __Localizations = SELECTCOLUMNS(FILTER(ALL('Localizations'), [projectID] = __ProjectID), "__municipality", [municipality])
    VAR __Except = EXCEPT(__Localizations, __Municipalities)
    VAR __Intersect = INTERSECT(__Localizations, __Municipalities)
    VAR __Result = IF(COUNTROWS(__Except) = BLANK() && COUNTROWS(__Intersect) <> BLANK(), 1, 0)
RETURN
    __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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