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
Anonymous
Not applicable

Calculated Column to get Flag based on the values for a key

Hi,

 

I have data as below:

CustomerProducts
ABread
BBread
BButter
Bread
CEgg
DBread
DButter
DEgg
EButter
FEgg

 

I want to create a calculated column using DAX as a flag indicating whether a customer got only Bread, Bread and Butter, Bread and Egg, Only Butter, Only Egg, All etc as below.

 

CustomerProductsFlag
ABreadBread only
BBreadBread and Butter
BButterBread and Butter
BreadBread and Egg
CEggBread and Egg
DBreadAll 
DButterAll 
DEggAll 
EButterButter Only
FEggEgg Only


Kindly help with the DAX calculated column for the same.

 

Thanks,

Prajna

2 ACCEPTED SOLUTIONS
daXtreme
Solution Sage
Solution Sage

 

Assuming that your table is named CustProd...

Flag = // calculated column
var CurrentCustomer = CustProd[Customer]
var PartOfTableOfInterest =
    FILTER(
        CustProd,
        // Please keep column names in singular.
        CustProd[Customer] = CurrentCustomer
    )
var TotalNumberOfProducts =
    COUNTROWS(
        SELECTCOLUMNS(
            PartOfTableOfInterest,
            "@Product", CustProd[Product]
        )
    )
var PartialOutput =
    CONCATENATEX(
        PartOfTableOfInterest,
        CustProd[Product],
        IF( TotalNumberOfProducts = 2, " and ", "" ),
        CustProd[Product],
        ASC
    )
var Output = 
    SWITCH( true(),
        TotalNumberOfProducts = 1, PartialOutput & " Only",
        TotalNumberOfProducts > 2, "All",
        PartialOutput
    )
return
    Output

 

 

View solution in original post

@Anonymous 
Please refer to attached sample file with the solution

1.png

Flag = 
VAR FocusProducts = { "Bread", "Butter", "Egg" }
VAR CurrentCustomerProducts = CALCULATETABLE ( VALUES ( Data[Products] ), ALLEXCEPT ( Data, Data[Customer] ) )
VAR MatchingProducts = INTERSECT ( CurrentCustomerProducts, FocusProducts )
VAR Concatenation = CONCATENATEX ( MatchingProducts, [Products], " And ", [Products], ASC )
RETURN
    SWITCH (
        TRUE ( ),
        COUNTROWS ( FocusProducts ) = COUNTROWS ( MatchingProducts ), "All",
        COUNTROWS ( MatchingProducts ) = 1, Concatenation & " Only",
        Concatenation
    )

View solution in original post

4 REPLIES 4
daXtreme
Solution Sage
Solution Sage

 

Assuming that your table is named CustProd...

Flag = // calculated column
var CurrentCustomer = CustProd[Customer]
var PartOfTableOfInterest =
    FILTER(
        CustProd,
        // Please keep column names in singular.
        CustProd[Customer] = CurrentCustomer
    )
var TotalNumberOfProducts =
    COUNTROWS(
        SELECTCOLUMNS(
            PartOfTableOfInterest,
            "@Product", CustProd[Product]
        )
    )
var PartialOutput =
    CONCATENATEX(
        PartOfTableOfInterest,
        CustProd[Product],
        IF( TotalNumberOfProducts = 2, " and ", "" ),
        CustProd[Product],
        ASC
    )
var Output = 
    SWITCH( true(),
        TotalNumberOfProducts = 1, PartialOutput & " Only",
        TotalNumberOfProducts > 2, "All",
        PartialOutput
    )
return
    Output

 

 

tamerj1
Super User
Super User

Hi @Anonymous 

I guess you have more than three products? 

Anonymous
Not applicable

Hi @tamerj1 ,

 

Yes. There are other products. But my focus in only on Bread, Butter and Egg.
Ex:

CustomerProductsFlag
ABreadBread only
BBreadBread and Butter
BButterBread and Butter
BreadBread and Egg
CEggBread and Egg
DBreadAll 
DButterAll 
DEggAll 
EButterButter Only
FEggEgg Only
GBreadBread and Butter
GButterBread and Butter
GJamBread and Butter

@Anonymous 
Please refer to attached sample file with the solution

1.png

Flag = 
VAR FocusProducts = { "Bread", "Butter", "Egg" }
VAR CurrentCustomerProducts = CALCULATETABLE ( VALUES ( Data[Products] ), ALLEXCEPT ( Data, Data[Customer] ) )
VAR MatchingProducts = INTERSECT ( CurrentCustomerProducts, FocusProducts )
VAR Concatenation = CONCATENATEX ( MatchingProducts, [Products], " And ", [Products], ASC )
RETURN
    SWITCH (
        TRUE ( ),
        COUNTROWS ( FocusProducts ) = COUNTROWS ( MatchingProducts ), "All",
        COUNTROWS ( MatchingProducts ) = 1, Concatenation & " Only",
        Concatenation
    )

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