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
PBlearner27
Helper I
Helper I

Dax formula help! filter and multiple items within string

Hi everyone, 

 

  • I have a table (Table 1) with the following data. Please note: The column "Group" has been created by merging (seperated by commas) three different columns (indicated with the three different colours). 

PBlearner27_0-1598180373008.png

 

  • I have another Table (Table 2) as below:

PBlearner27_1-1598181058702.png

  • I need a DAX formula to create the column outlined in red - "Sum of Price". The DAX formula needs to account for the following: 
    • Should include the filters Shop=A and Charges=ALL but excluding Discount (in Table 1).

(Please note: I cannot have the format of the tables changed/ I cannot use other types of visuals to achieve what I'm trying to do; I have checked all these options, I need it to be exactly as I've described above, thank you!!)

Any help would be greatly appreciated - thank you!

1 ACCEPTED SOLUTION

OK, this should work, I tweaked some things to make it act better:

 

Column = 
    VAR __Item = [Shop A]
    VAR __Comma = FIND(",",__Item,,BLANK())
    VAR __First = IF(ISBLANK(__Comma),__Item,LEFT(__Item,__Comma - 1))
    VAR __Second = IF(ISBLANK(__Comma),BLANK(),RIGHT(__Item,LEN(__Item) - __Comma -1))
    VAR __Table = 
        ADDCOLUMNS(
            FILTER('Table (9)',[Charges]<>"Discount" && [Shop] = "Shop A"),
            "__Include",IF(FIND(__First,[Group],,0) > 0 && FIND(__Second,[Group],,0) > 0,1,0)
        )
    VAR __Sum = SUMX(FILTER(__Table,[__Include]=1),[Price])
RETURN
    IF(__Item = "",BLANK(),IF(ISBLANK(__Sum),0,__Sum))

 

CONTAINSSTRING was causing me some grief so I went with FIND instead.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

6 REPLIES 6
lbendlin
Super User
Super User

Acknowledging your note I am going to ignore it and tell you how I would resolve this.

 

Add a transform that splits the group column, and don't specify "into rows". You will end up with new columns Group.1, Group.2, and Group.3  which will immediately be usable for your Table2 desired outcome (through measures that apply your logic.

 

The only tricky part will be to add the blank rows.

@lbendlin Thank you for your reply, however for my purpose I need to use a calculated column, I cannot use a measure. 

I have provided a very simplified example of what I'm trying to achieve - therefore it may not be very clear as to why I specifically need to use a calculated column. 

Is this not possible to do with a calculated column?

@PBlearner27 - Well, I tend to agree with @lbendlin but if you are dead set on what you want, you can do it like this:

 

Column = 
    VAR __Item = [Shop A]
    VAR __Comma = FIND(",",__Item,,BLANK())
    VAR __First = IF(ISBLANK(__Comma),__Item,LEFT(__Item,__Comma - 1))
    VAR __Second = IF(ISBLANK(__Comma),BLANK(),RIGHT(__Item,LEN(__Item) - __Comma -1))
    VAR __Table = FILTER('Table (9)',CONTAINSSTRING([Group],__First) = TRUE() && CONTAINSSTRING([Group],__Second) = TRUE() && [Charges]<>"Discount")
RETURN
    IF(__Item = "",BLANK(),SUMX(__Table,[Price]))

 

I have attached a PBIX below my sig. You want Tables 9 and 10.

 

Next time, please post your data as text in a table so that we don't have to retype everything. Thanks. Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler I simply cannot thank you enough for this!! I am very grateful for your help, works like a charm, thank you so much!

 

I made a minor change by adding in a filter for "Shop A" as highlighted in the formula below.


Before I can mark it as a solution, there is one minor issue I'm unable to figure out:

  • The rows highlighted in yellow should return values of £0 since these records don't exist in the filtered table - I'm not quite sure how to amend the formula for that, please can you help with this?

PBlearner27_0-1598207335040.png

 

Thank you for your time - much appreciated!!

 

OK, this should work, I tweaked some things to make it act better:

 

Column = 
    VAR __Item = [Shop A]
    VAR __Comma = FIND(",",__Item,,BLANK())
    VAR __First = IF(ISBLANK(__Comma),__Item,LEFT(__Item,__Comma - 1))
    VAR __Second = IF(ISBLANK(__Comma),BLANK(),RIGHT(__Item,LEN(__Item) - __Comma -1))
    VAR __Table = 
        ADDCOLUMNS(
            FILTER('Table (9)',[Charges]<>"Discount" && [Shop] = "Shop A"),
            "__Include",IF(FIND(__First,[Group],,0) > 0 && FIND(__Second,[Group],,0) > 0,1,0)
        )
    VAR __Sum = SUMX(FILTER(__Table,[__Include]=1),[Price])
RETURN
    IF(__Item = "",BLANK(),IF(ISBLANK(__Sum),0,__Sum))

 

CONTAINSSTRING was causing me some grief so I went with FIND instead.


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler I am completely blown away!! Thank you so so much for your help Greg - this has saved me a lot of trouble and I've learnt something new & very useful!
Cheers 🙂 

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.