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
kta87
Helper I
Helper I

Create Table only with records that met the conditions

Hi everyone,

 

  I have a request that gives me a hard time. I have to create a table with data from 2 tables bringing only the records from Table A that by their sum Amount group are equal or less with the corespondent Amount from Table B. The relationship is Table A (Many) - (One) Table B by the Customer ID.

Here are the 2 tables:

 

kta87_1-1671819672870.png

 

The expected result table:

 

kta87_2-1671819832724.png

 

Thank you in advance,

Kta

 

1 ACCEPTED SOLUTION

@kta87 

Place the following M code in a blank query to see the steps.See it all at work in the attached file.

let
    Source = TableA,
    #"Merged Queries" = Table.NestedJoin(Source, {"Customer ID"}, TableB, {"Customer ID"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Amount"}, {"Table2.Amount"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table2", "Cumul", each List.Sum(Table.SelectRows(#"Expanded Table2", (inner)=> [Customer ID]=inner[Customer ID] and [Document Number]>=inner[Document Number])[Amount]), Int64.Type),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Cumul] <= [Table2.Amount])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Table2.Amount", "Cumul"})
in
    #"Removed Columns"

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

View solution in original post

12 REPLIES 12
jewel_at
Helper II
Helper II

Hi @kta87 

 

You can try this:

 

 

 

Table C = 
FILTER(ADDCOLUMNS('Table A',
    "Compare", 
        VAR CustomerID = 'Table A'[CustomerID]
        var SumOfTableA =
        CALCULATE(SUM('Table A'[Amount]), 'Table A'[CustomerID] = CustomerID)
        VAR SumOfTableB =
        CALCULATE(sum('Table B'[Amount]), 'Table B'[CustomerID] = CustomerID)
        VAR Test =
        IF (SumOfTableA <= SumOfTableB, TRUE(), FALSE())
        return Test
), [Compare] = TRUE())

 

 

 

 

Hope this helps!

 

Jewel

 

AlB
Super User
Super User

Hi @kta87 

This looks like something that should be done in Power Query rather than DAX. What is the relationship for??

Can you please paste tables A and B in text-tabular format here, instead of in screen caps, so that the contents can be copied?

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Hi AIB,

 

 Power Query is fine also, if it does the job.

 

Table A
Customer IDDocument NumberAmount
Customer A912
Customer A1010
Customer A1210
Customer B1310
Customer B1515
Customer B1625
Customer B3510
Customer B12212
Customer C145100
Customer D456200

 

 

Table B
Customer IDDocument NumberAmount
Customer A430
Customer B4580
Customer C34580
Customer D1567200

@kta87 

To clarify, why is the third row for Customer A  (doc number 12) discarded?

It's not discarded, it shoud be in the table.

What do you mean? It is not in your output table TableC

 


SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Because only the records from Table A whose sum is less than or equal to the value from Table B must be in Table C. For the customer A, document 9 and 10 can be in table C or 10 and 12 also is fine.

Then the TableC that you show is incorrect? It does NOT show Customer A, document number 12

What do you mean by "whose sum is less..." ? What sum is that?

 

 

So for each customer from Table B must be brought documents from Table A, but their total amount must not exceed the amount from the Table B.
Customer A has 30 amount in Table B, so the correct records can be either documents 9 & 10 (12+10 = 22 which is <= 30) or documents 9 & 12 (12+10 = 22 which is <= 30) from Table A. Both variants are correct, the logic must bring only one.

@kta87 

The solution I posted above does exactly that. It keeps the first variant that neets the requirement.

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

@kta87 

Place the following M code in a blank query to see the steps.See it all at work in the attached file.

let
    Source = TableA,
    #"Merged Queries" = Table.NestedJoin(Source, {"Customer ID"}, TableB, {"Customer ID"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Amount"}, {"Table2.Amount"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table2", "Cumul", each List.Sum(Table.SelectRows(#"Expanded Table2", (inner)=> [Customer ID]=inner[Customer ID] and [Document Number]>=inner[Document Number])[Amount]), Int64.Type),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Cumul] <= [Table2.Amount])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Table2.Amount", "Cumul"})
in
    #"Removed Columns"

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

This is brilliant. Thank you

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