Forum Discussion

CornelisV's avatar
CornelisV
Icon for Helper IV rankHelper IV
1 year ago
Solved

Measure as a column for Bar chart Y axis selection

Dear all,

 

I have made a simple plot based on this view:

When I selelect "490" from the filter, the plot must show barchart with score "A". If selected "495" , then score "B" must be visible in the same plot. The Power BI has two tables,

a) the Score table:

WeekAB
11299
11476
21181
2966
3659
3645
3039
43101
51239
51145
51055
5712

 

b) the Group table :

GroupWeek
4901
4902
4903
4905
4952
4953
4954
495

5

 

 

Column "Week" are connected:

 

I want to create Measure so that the right column will be picked up, depending wicht selection you did make from the filter, 490 = A and 495 = B.

I used this DAX:

_selected group = IF(SELECTEDVALUE('Group'[Group]) = "A",SUMX('Scores','Scores'[A]), SUMX('Scores','Scores'[B]))
However, the plot is not correct, it shows quite different scores, due to the SUMX function from Aggregate. 
Could you please demonstrate a better solution?
 
Regards,
 
Cornelis

 

 

 

 

  • HI CornelisV 

    The problem occurs because there is a many-to-many relationship between 'Group table' and 'Score table'.
    This creates ambiguity when trying to sum values in a bar chart Y-axis selection, which can lead to incorrect results.

    Solution:
    To resolve this, you should introduce a bridge table that contains unique weeks, ensuring a proper relationship between the tables.
    The model should be structured as follows:

    Then you can use DAX FORMULA 

    Sum_selected_group =
    VAR selected_group = SELECTEDVALUE('Group table'[Group])
    VAR A = SUM('Score table'[A])
    VAR B = SUM('Score table'[B])
    RETURN
    IF (selected_group = 490, A, B)

    Result :

    More information about handalling eith many to many relationships here :
    https://www.youtube.com/watch?v=aPLaun-tTgQ

    The pbix is attached

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



4 Replies

  • hey, CornelisV ,

     

    Remove the relationship, it's Many To Many, that will only produce duplicated results:

     

    Add new column to your Group, that represent A/B:


    write measure like this:

    _selected group = 
    var _group = SELECTEDVALUE('Group'[columnToSum])
    var _sum = IF( _group = "A", SUM(Scores[A]), SUM(Scores[B]))
    
    return _sum



    • CornelisV's avatar
      CornelisV
      Icon for Helper IV rankHelper IV

      Hi vojtechsima ,

       

      Thank you for your support. This is also the solution, however the problem is that tables contains over 10,000 rows and 15 columns as result from SQL. The intention is to modify the table as less as possible, prerferably nothing. Nevertheless, thsi is a good learning point for me.

       

      BR,

       

      Cornelis

  • HI CornelisV 

    The problem occurs because there is a many-to-many relationship between 'Group table' and 'Score table'.
    This creates ambiguity when trying to sum values in a bar chart Y-axis selection, which can lead to incorrect results.

    Solution:
    To resolve this, you should introduce a bridge table that contains unique weeks, ensuring a proper relationship between the tables.
    The model should be structured as follows:

    Then you can use DAX FORMULA 

    Sum_selected_group =
    VAR selected_group = SELECTEDVALUE('Group table'[Group])
    VAR A = SUM('Score table'[A])
    VAR B = SUM('Score table'[B])
    RETURN
    IF (selected_group = 490, A, B)

    Result :

    More information about handalling eith many to many relationships here :
    https://www.youtube.com/watch?v=aPLaun-tTgQ

    The pbix is attached

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



    • CornelisV's avatar
      CornelisV
      Icon for Helper IV rankHelper IV

      Hi Ritaf1983 ,

       

      Thank you for your fast response and solution. You are right, I forgot to add a bridge table with only weeknumbers. The provided DAX solution is exact what I'm looking for, it is clear that DAX knowledge is inevitable to solve your programming issue. 

       

      BR,

      Cornelis