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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

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
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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