Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Summing with filter changing each row

My data does not have a correct value for the highest level in the hierachy. The value should be calculated based on all the values belonging to the same main project.

 

I can calculate this in Excel quite easily with the following formula

=IF(A2=B2; SUMIF($A$2:$A$17;"="&B2;$C$2:$C$17); C2)

to get the desired output in the table below.

 

Main projectProject numberValueCALC
AA0-237700
AA1-134100-134100
AA2-103600-103600
BB15000-1864449,97
BB1-338250-338250
BB2-451699,98-451699,98
BB3-418999,74-418999,74
BB4-670500,25-670500,25
CC0-2013599,94
CC1-338250-338250
CC2-451699,98-451699,98
CC3-418999,98-418999,98
CC4-804649,98-804649,98
DD0-789949,98
DD1-338250-338250
DD2-451699,98-451699,98

 

I cannot figure out how to do this same calculation with Power BI. Either with the query or DAX is fine.

 

I tried using the row by row SUMX with FILTER but I am not getting a correct output.

CALC = SUMX(FILTER('Table'; 'Table'[Main project] == 'Table'[Project number]); 'Table'[Value])
Main projectProject numberValueCALC
AA015000
AA1-13410015000
AA2-10360015000
BB1500015000
BB1-33825015000
BB2-451699,9815000
BB3-418999,7415000
BB4-670500,2515000
CC015000
CC1-33825015000
CC2-451699,9815000
CC3-418999,9815000
CC4-804649,9815000
DD015000
DD1-33825015000
DD2-451699,9815000

 

How do I achieve this in Power BI?

 

  • Anonymous's avatar
    Anonymous
    6 years ago

    That did not work correctly. It gives the same value for each row. But luckily I managed to figure out a way to do it. Below table contains both calculations. Correct one in CALC and your suggestion in Measure.

     

     

    CALC = SUMX(
        FILTER(
            'Table'; 
            'Table'[Main project] == EARLIER('Table'[Project number]) || 
            'Table'[Project number] == EARLIER('Table'[Project number])
            ); 
        'Table'[Value]
    )

     

     

    Main projectProject numberValueCALCMeasure
    AA0-237700-59819198,68
    AA1-134100-134100-59819198,68
    AA2-103600-103600-59819198,68
    BB15000-1864449,97-59819198,68
    BB1-338250-338250-59819198,68
    BB2-451699,98-451699,98-59819198,68
    BB3-418999,74-418999,74-59819198,68
    BB4-670500,25-670500,25-59819198,68
    CC0-2013599,94-59819198,68
    CC1-338250-338250-59819198,68
    CC2-451699,98-451699,98-59819198,68
    CC3-418999,98-418999,98-59819198,68
    CC4-804649,98-804649,98-59819198,68
    DD0-789949,98-59819198,68
    DD1-338250-338250-59819198,68
    DD2-451699,98-451699,98-59819198,68

3 Replies

  • sturlaws's avatar
    sturlaws
    Resident Rockstar

    Hi Anonymous,

     

    you can try this code:

    Measure =
    SUMX (
        'Table';
        IF (
            'Table'[Main project] = 'Table'[Project number];
            CALCULATE (
                SUM ( 'Table'[Value] );
                FILTER (
                    ALL ( 'Table' );
                    'Table'[Main project] = MIN ( 'Table'[Main project] )
                )
            );
            SUM ( 'Table'[Value] )
        )
    )

     

    Cheers,
    Sturla

    If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

    • Anonymous's avatar
      Anonymous
      Not applicable

      That did not work correctly. It gives the same value for each row. But luckily I managed to figure out a way to do it. Below table contains both calculations. Correct one in CALC and your suggestion in Measure.

       

       

      CALC = SUMX(
          FILTER(
              'Table'; 
              'Table'[Main project] == EARLIER('Table'[Project number]) || 
              'Table'[Project number] == EARLIER('Table'[Project number])
              ); 
          'Table'[Value]
      )

       

       

      Main projectProject numberValueCALCMeasure
      AA0-237700-59819198,68
      AA1-134100-134100-59819198,68
      AA2-103600-103600-59819198,68
      BB15000-1864449,97-59819198,68
      BB1-338250-338250-59819198,68
      BB2-451699,98-451699,98-59819198,68
      BB3-418999,74-418999,74-59819198,68
      BB4-670500,25-670500,25-59819198,68
      CC0-2013599,94-59819198,68
      CC1-338250-338250-59819198,68
      CC2-451699,98-451699,98-59819198,68
      CC3-418999,98-418999,98-59819198,68
      CC4-804649,98-804649,98-59819198,68
      DD0-789949,98-59819198,68
      DD1-338250-338250-59819198,68
      DD2-451699,98-451699,98-59819198,68
      • sturlaws's avatar
        sturlaws
        Resident Rockstar

        Then I misunderstood, I thought you wanted a measure, not a calculated column