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
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
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.