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
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
v-yangliu-msft
Community Support
Community Support

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"}))

 

v-yangliu-msft
Community Support
Community Support

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.


Go to My LinkedIn Page


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.


Go to My LinkedIn Page


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.


Go to My LinkedIn Page


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