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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

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
AlB
Community Champion
Community Champion

@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
Resolver I
Resolver I

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
Community Champion
Community Champion

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
AlB
Community Champion
Community Champion

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

AlB
Community Champion
Community Champion

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.

AlB
Community Champion
Community Champion

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.

AlB
Community Champion
Community Champion

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

 

AlB
Community Champion
Community Champion

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