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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

How to compare two Rows value in same table and assign a value in a separate column based on it

Hi Team,

 

I need help in below logic:

 

I have three tables:
Orders table (Contains 1 row per orders placed with customer and revenue data as whole)

Order line items table (Contains brief Order data with line items bought within the order)

Product Stock Keeping Unit Table (It contains the Stock Keeping Unit and brand data)

 

Table data follows:

Order Table
Order NoCustomerRevenue
67894677Customer A580
67894600Customer B950
67890000Customer C800
67890012Customer A540

 

Order Line Items Table
Order NoProduct SKUProduct SKU CodeBrand
67894677Shampoo - 340 ml13Pantene
67894677Hair Conditioner - 340 ml11Sunsilk
67894677Hair Serum - 400ml12Matrix
67894600Shampoo - 340 ml14Sunsilk
67894600Hair Conditioner - 340 ml11Sunsilk
67894600Hair Serum - 400ml20Sunsilk
67890000Shampoo - 340 ml13Pantene
67890000Hair Conditioner - 340 ml10Pantene
67890012Hair Conditioner - 340 ml15Matrix
67890012Hair Serum - 400ml12Matrix

 

Product Stock Keeping Unit
Product SKUProduct SKU CodeBrand
Hair Serum - 400ml12Matrix
Hair Conditioner - 340 ml15Matrix
Shampoo - 340 ml13Pantene
Hair Conditioner - 340 ml10Pantene
Hair Conditioner - 340 ml11Sunsilk
Shampoo - 340 ml14Sunsilk
Hair Serum - 400ml20Sunsilk

 

What help I need is that, I want to categories my orders on the basis of Brand Name

Expected, Assign brand name to orders and if there are more than 1 brand in one order mark it as "Mix Order". Please see the expected table below:

Order Table
Order NoCustomerRevenueCalculated Brand Column
67894677Customer A580Mix Brand
67894600Customer B950Sunsilk Brand
67890000Customer C800Pantene Brand
67890012Customer A540Matrix Brand

 

Can someone please help me with the logic to create this calculated Column? I tried Compare function but it is not helping here.

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

@Anonymous 
Please use

Column = 
VAR Brand = CALCULATE ( SELECTEDVALUE ( 'Order Line Items'[Brand] ) )
RETURN
    IF ( 
        ISBLANK ( Brand ),
        "Mixed Order",
        Brand
    )

1.png

View solution in original post

10 REPLIES 10
tamerj1
Super User
Super User

@Anonymous 
Please use

Column = 
VAR Brand = CALCULATE ( SELECTEDVALUE ( 'Order Line Items'[Brand] ) )
RETURN
    IF ( 
        ISBLANK ( Brand ),
        "Mixed Order",
        Brand
    )

1.png

Anonymous
Not applicable

Thank you @tamerj1 , this solution worked.

tamerj1
Super User
Super User

Hi @Anonymous 
Suppose you have a one way relationhip (One to Many) between 'Orders' and 'Order Line Items' then you my try

Calculated Brand Column =
CONCATENATEX (
    RELATEDTABLE ( 'Order Line Items' ),
    IF (
        HASONEVALUE ( 'Order Line Items'[Brand] ),
        'Order Line Items'[Brand],
        "Mixed Order"
    )
)
Anonymous
Not applicable

Hi @tamerj1 

I tried the solution provided by you, but in my case it is not returning the expected value. When I am applying the DAX provide by you above, it is returning the values as in the screenshot below:

 

amritpalsingh_0-1652384411639.png

 

We need a little modification in the logic. 

Hi @Anonymous 
Please try

Calculated Brand Column =
VAR Check =
    SUMX (
        RELATEDTABLE ( 'Order Line Items' ),
        IF ( HASONEVALUE ( 'Order Line Items'[Brand] ), 1 )
    )
RETURN
    IF ( Check >= 1, VALUES ( 'Order Line Items'[Brand] ), "Mixed Order" )
Anonymous
Not applicable

Hi @tamerj1 ,

 

It is still only giving all values as "Mixed Order". Please see the screenshot below:

amritpalsingh_0-1652436814125.png


Can we compare orders column with Brand column, like if One Order is having multiple brand purchased together it returns "Mixed Order" else "Brand"?

@Anonymous 
Can you please create a column

COUNTROWS ( RELATEDTABLE ( 'Order Line Items' ) )

and let me know what results you get

 

Anonymous
Not applicable

Hi @tamerj1 

 

This is the result of above formula in 'Orders' Table.

amritpalsingh_0-1652439213149.png

 

Great @Anonymous 

Please use

 

 

Calculated Brand Column =
IF (
    COUNTROWS ( RELATEDTABLE ( 'Order Line Items' ) ) = 1,
    CALCULATE ( VALUES ( 'Order Line Items'[Brand] ) ),
    "Mixed Order"
)

 

 
Anonymous
Not applicable

Hi @tamerj1 ,

 

Thanks, above logic is working correctly to some extent but the result is still not accurate.

 

currently it is giving result as (if the order line item count is 1, then it is giving brand name) but in my case order can have multiple productsof brands in it but we need to see if the products in one order are of same Brand or not

 

For eg:

Scenario 1: 

Order Line Items Table
Order NoProduct SKUProduct SKU CodeBrand
67890012Hair Conditioner - 340 ml15Matrix
67890012Hair Serum - 400ml12Matrix

 

Expected Brand Column result:

Order Table
Order NoCustomerRevenueCalculated Brand Column
67890012Customer A540Matrix Brand

 

Scenario 2:

Order Line Items Table
Order NoProduct SKUProduct SKU CodeBrand
67894677Shampoo - 340 ml13Pantene
67894677Hair Conditioner - 340 ml11Sunsilk
67894677Hair Serum - 400ml12Matrix

 

Expected Brand Column result:

Order Table
Order NoCustomerRevenueCalculated Brand Column
67894677Customer A580Mix Brand

 

This is what I am looking for to create. Please help me.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Top Kudoed Authors