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
abchandru
Regular Visitor

Creating a measure using two different column from different table

Need to write  DAX to calculate Qty  , IF Table1.Flag=1 and Table2.Date>=Table1.Date then sum(Qty) ELSEIF Table1.Flag=0 and Table2.Date<Table1.Date then sum(Qty) ELSE sum(Qty)

Joins based on CODE

 

 

Table1:

CodeNameGroupOrgSKUmethodFlagDate
561KUMKUM1080MB5SO Portal  
363SynSyn1020MB5Manual0 
363SynSyn1020MB5EDI_RPA19/13/2022
259THTH1010MB5EDI_RPA  
126PBRPBR1020MB5SO Portal  
127BRBR1020MB5EDI_RPA0 
127BRBR1020MB5SO Portal110/3/2022

 

Table2:

CodeNameGroupOrgSKUDateQty
561KUMKUM1080MB510/3/202250
363SynSyn1020MB57/23/202225
363SynSyn1020MB510/24/202275
259THTH1010MB57/25/202260
126PBRPBR1020MB510/5/2022200
127BRBR1020MB59/17/202265
127BRBR1020MB510/6/202250
127BRBR1020MB510/21/202250

 

Result:

CodeNameGroupOrgSKUmethodQty
561KUMKUM1080MB5SO Portal50
363SynSyn1020MB5Manual25
363SynSyn1020MB5EDI_RPA75
259THTH1010MB5EDI_RPA60
126PBRPBR1020MB5SO Portal200
127BRBR1020MB5EDI_RPA65
127BRBR1020MB5SO Portal100

 

1 ACCEPTED SOLUTION

Hi @abchandru ,

 

According to your description, I made a sample and here is my solution.

Create a calculated column.

Column =
VAR _a =
    MAXX ( FILTER ( 'Table1', [Code] = EARLIER ( Table1[Code] ) ), [Date] )
VAR _b =
    SUMX (
        FILTER (
            'Table2',
            'Table2'[Code] = EARLIER ( Table1[Code] )
                && 'Table2'[Date] <= _a
        ),
        [Qty]
    )
VAR _c =
    SUMX (
        FILTER (
            'Table2',
            'Table2'[Code] = EARLIER ( Table1[Code] )
                && 'Table2'[Date] > _a
        ),
        [Qty]
    )
VAR _d =
    SUMX ( FILTER ( 'Table2', 'Table2'[Code] = EARLIER ( Table1[Code] ) ), [Qty] )
RETURN
    SWITCH ( Table1[Flag], 0, _b, 1, _c, BLANK (), _d )

Final output:

vxiaosunmsft_0-1669171265845.png

 

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ xiaosun

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
amitchandak
Super User
Super User

@abchandru , If possible merge these two tables in the power query, you can select multiple columns while doing the merge

 

 https://radacad.com/append-vs-merge-in-power-bi-and-power-query

then you can easily create that as column or use in the filter in measure

Hi Amit,

Thanks for reply,I am new to PowerBI tried to merge getting more records and how to write dax to achive 

Hi @abchandru ,

 

According to your description, I made a sample and here is my solution.

Create a calculated column.

Column =
VAR _a =
    MAXX ( FILTER ( 'Table1', [Code] = EARLIER ( Table1[Code] ) ), [Date] )
VAR _b =
    SUMX (
        FILTER (
            'Table2',
            'Table2'[Code] = EARLIER ( Table1[Code] )
                && 'Table2'[Date] <= _a
        ),
        [Qty]
    )
VAR _c =
    SUMX (
        FILTER (
            'Table2',
            'Table2'[Code] = EARLIER ( Table1[Code] )
                && 'Table2'[Date] > _a
        ),
        [Qty]
    )
VAR _d =
    SUMX ( FILTER ( 'Table2', 'Table2'[Code] = EARLIER ( Table1[Code] ) ), [Qty] )
RETURN
    SWITCH ( Table1[Flag], 0, _b, 1, _c, BLANK (), _d )

Final output:

vxiaosunmsft_0-1669171265845.png

 

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ xiaosun

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.