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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
An0n
Frequent Visitor

How to show the sum of two possible combinations of values from two columns in a Matrix?

Dear Power BI Community,

 

I have scanned the forum but could not get any solution to work for the following problem statement:

 

I have one table with multiple columns loaded into Power BI Desktop through Direct Query with a DB2 connection. I am trying to create a matrix which shows a combination of Column 1 and Column 2 in the rows, e.g. AAA_BBB. It should sum up all the rows for the combination AAA_BBB. This is easily done by just dragging Column 3 (Amounts) to Values in the Matrix with a merged column (Column 1 and 2) in the rows. Next I would like to show the reverse combination, e.g. BBB_AAA in the Values area. The goal is to highlight the differences between the total of matching combinations, e.g. AAA_BBB is 10 but BBB_AAA is 8. It should be equal. Would anyone have any suggestions for the measure (or calculated column?) formula to move to the desired implementation?

 

Data example:

 

Column 1Column 2Column 3
AAACCC10
AAABBB5
CCCBBB4
BBBCCC3
CCCAAA10
BBBAAA8
AAABBB2
AAABBB3
BBBCCC1

 

Desired implementation:

 

Column 1 x 2Amount Combination 1Amount Combination 2Difference
AAA_BBB1082
AAA_CCC10100
BBB_CCC440

 

Thanks in advance for your suggestions.

10 REPLIES 10
Anonymous
Not applicable

Hi  @An0n ,

If you want to sort by [Difference], you can do so directly.

Select the ellipsis, select Sort by-[Difference], and sort descending.

vyangliumsft_0-1626416893548.png

 

Best Regards,

Liu Yang

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

Thanks for this suggestion, which would indeed solve the sorting problem.

Could you have a look at how to adapt the "IN AAA_BBB, AAA_CCC, BBB_CCC" part below? In the dataset there are a lot more (around 50) values of AAA, BBB etc. and the amount of different combinations and new values changes from one period to the other.

Amount Combination 1 =
CALCULATE(
    SUM('Table'[Column 3]),
    FILTER(ALL('Table'),'Table'[Column 1 x 2]=MAX('Table'[Column 1 x 2])&&'Table'[Column 1 x 2]IN {"AAA_BBB","AAA_CCC","BBB_CCC"}))

 

Anonymous
Not applicable

Hi  @An0n ,

Here are the steps you can follow:

1. Create calculated column.

Column 1 x 2 = 'Table'[Column 1]&"_"&'Table'[Column 2]

2. Create measure.

Amount Combination 1 =
CALCULATE(
    SUM('Table'[Column 3]),
    FILTER(ALL('Table'),'Table'[Column 1 x 2]=MAX('Table'[Column 1 x 2])&&'Table'[Column 1 x 2]IN {"AAA_BBB","AAA_CCC","BBB_CCC"}))
Amount Combination 2 =
VAR _PIN=MAX('Table'[Column 2])&"_"&MAX('Table'[Column 1])
return
IF([Amount Combination 1]=0,BLANK(),
CALCULATE(SUM('Table'[Column 3]),FILTER(ALL('Table'),[Column 1 x 2]=_PIN)))
Difference = [Amount Combination 1]-[Amount Combination 2]

3. Result:

vyangliumsft_0-1625537714045.png

 

 

Best Regards,

Liu Yang

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

Hi Liu Yang,

 

Thank you for your reply. The AAA BBB and CCC was just as an example, in reality there are around 60 entries and thus a multiple of that as possibilities (not every combination exists, e.g. XXX and TTT). There is also no requirement to order column 1 x 2 in any way, the important thing is that the difference can be sorted from high to low. Hope this helps and look forward to your suggestion.

Jihwan_Kim
Super User
Super User

Picture1.png

 

 

https://www.dropbox.com/s/1p7uhxi1ulzsrvo/anon.pbix?dl=0 

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Thank you for the suggestion. The combination of AAA and BBB should however be shown in one column and as one row entry only (AAA_BBB should be shown, without BBB_AAA). There are hundreds of combinations in the original dataset so it should be easy to see just see the difference of the sum for this possible combination.

Picture2.png

 

https://www.dropbox.com/s/1p7uhxi1ulzsrvo/anon.pbix?dl=0 

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Thank you Jihwan_Kim, the solution is taking shape. Unfortunately I ran into the following bug: the calculated column Rank CC does not work because function 'RANKX' is not allowed as part of calculated column DAX expressions on DirectQuery models and I am using a DirectQuery model.

https://www.dropbox.com/s/1p7uhxi1ulzsrvo/anon.pbix?dl=0 

 

 

Ranking =
RANKX ( ALL(Data[AB Axis CC]), CALCULATE(MAX(Data[AB Axis CC])),, ASC )

 

AB combination total: =
VAR _currentrank = [Ranking]
VAR _currentaxis =
MAX ( Data[AB Axis CC] )
VAR _currentA =
MAX ( Data[A] )
VAR _currentB =
MAX ( Data[B] )
VAR _newtable =
ADDCOLUMNS (
ALL ( Data ),
"@Rank", RANKX ( ALL ( Data[AB Axis CC] ), Data[AB Axis CC],, ASC )
)
RETURN
IF (
COUNTROWS (
FILTER (
_newtable,
Data[AB Axis CC] <> _currentaxis
&& Data[A] = _currentB
&& Data[B] = _currentA
&& [@Rank] < _currentrank
)
) >= 1,
BLANK (),
IF ( ISFILTERED ( Data[AB Axis CC] ), SUM ( Data[Value] ) )
)

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

I appreciate the effort a lot. It seems I'm running into a final bug: "The resultset of a query to external data source has exceeded the maximum allowed size of '1000000' rows."

I was also looking to show this in Matrix format, so that I'll be able to scale the visualisization later by having other columns of the dataset as rows.

 

As I understand it currently, the ranking is not required. The AB Axis CC column can have the combinations ordered in any way, it doesn't have to be alphabetically. The only requirement is to be able to sort the Diff column from high to low.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors