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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
ddalton
Resolver I
Resolver I

CALCULATE SUM using matching columns?

I have two tables that fail to merge and relationships don't behave in the required manner. So, instead, I would like to sum rows in Table B and copy that value into a new column in Table A against a "matching" row. Representations of Table A and Table B are below.

 

Table A

Product IDReference
A1000D5566
A1001D5566
A1002D5687
A1003D5687
A1004D5821
A1005D5821
......

 

Table B

IDXReferenceClassValue
0001D5566A100
0002D5566A125
0003D5566B30
0004D5566C85
0005D5687A100
0006D5687A50
0007D5687B35
0008D5687B50
0009D5687B10
0010D5687C15
0011D5821A80
0012D5821A150
0013D5821B40
0014D5821C50
0015D5821C30
............

 

I would like to create custom columns in Table A that matches the Reference columns and then sums the respective values of each class type. After such a procedure, Table A would look like the below: 

 

Product IDReferenceABC
A1000D55662253085
A1001D55662253085
A1002D56871509515
A1003D56871509515
A1004D58212304080
A1005D58212304080
...............

 

I have tried using CALCULATE(SUM()) expressions but I'm struggling the FILTER part of the CALCULATE expression that allows me to match the Reference columns. For example, CALCULATE requires an EXPRESSION and can then take several FILTER statements, (i.e., CALCULATE(EXPRESSION, FILTER1, FILTER2, ...))

 

I figure my column expression would look something like this:

 

A = CALCULATE(SUM(TableB[Value]), TableB[Class] = "A", TableA[Reference] = TableB[Reference)

 

I've checked the MSFT docs on CALCULATE and believe that it can only filter on a single table so what function could I use instead to achieve this? 

1 ACCEPTED SOLUTION
PaulDBrown
Community Champion
Community Champion

For this you need the TREATAS function, which establishes a virtual relationship between the table in the first expression and the column(s) in the second part of the expression:

Source: https://docs.microsoft.com/en-us/dax/treatas-function 

 

You can  use the following:

 

Sum Value TREATAS =
CALCULATE (
    SUM ( 'Table B'[Value] ),
    TREATAS ( VALUES ( 'Table A'[a Reference] ), 'Table B'[Reference] )
)

 

result.png

 "a" prefixed columns from table A; "b" prefixed column from table B

I've attached the sample PBIX file

 

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

2 REPLIES 2
PaulDBrown
Community Champion
Community Champion

For this you need the TREATAS function, which establishes a virtual relationship between the table in the first expression and the column(s) in the second part of the expression:

Source: https://docs.microsoft.com/en-us/dax/treatas-function 

 

You can  use the following:

 

Sum Value TREATAS =
CALCULATE (
    SUM ( 'Table B'[Value] ),
    TREATAS ( VALUES ( 'Table A'[a Reference] ), 'Table B'[Reference] )
)

 

result.png

 "a" prefixed columns from table A; "b" prefixed column from table B

I've attached the sample PBIX file

 

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Thanks for your reply, Paul. This solution works great!

 

EDIT: Realised I could use a stacked 100% visualisation graph. 

 

Part of the reason I wanted to create custom columns (which I didn't explain previously) was so that I could then create other Measures from those that determine the % of class type from its respective total. Then, I could show a stacked column chart that would show the percent of A, B, and C of a product (totalling 100%). 

 

Is this something that could be done using the Measure already created in your example? 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.