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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Problem aggregating data at different levels

Hello,

 

I am struggling for a couple of days with a problem that looked simple but I dont manage to solve. I would appreciate your help. 

 

I have a data set that looks like this: 

var1var2error_xerror_yn_events
val1val3aa1000
val2val3aa135
val1val3ab12
val2val4ac1
val2val4ba39
val1val5bb100
val2val4bb85
val1val4cc2

 

var1 and var2 are not involved in the calculations I need to do, but they are relevant because I would like the calculations to take into account the filters that are applied to var1 and var2. Actually, I have managed to calculate what I need using "columns", but I need to use "measures" instead, so that the results take the filters into account. 

 

These are the columns that I have defined: 

error_x_agg = CALCULATE(sum('Table'[n_events]),ALLEXCEPT('Table', 'Table'[error_x]))
error_x_index = CALCULATE(sum('Table'[n_events]),ALLEXCEPT('Table', 'Table'[error_x], 'Table'[error_y]))/ 'Table'[error_x_agg]
error_x_index_norm = 'Table'[error_x_index]/CALCULATE(sum('Table'[error_x_index]),ALLEXCEPT('Table','Table'[error_y]))
 
These are the results that I get (error_x_agg and error_x_index: dont summarize, error_x_index_norm: sum):
 
error_xerror_yerror_x_aggerror_x_indexerror_x_index_norm
aa11480.988675960.91907485
ab11480.010452960.00628848
ac11480.000871080.00087032
ba2240.174107140.08092515
bb2240.825892860.99371152
cc210.99912968

 

What I need is to do this but in such a way that if I filter some values out with var1 and var2, all the calculations are done only on the data that remains. As far as I understand (I am quite new to Power BI) I need to use measures instead of columns to achieve this. For example, if I filter with var1 = val1, I expect:

 

error_xerror_yerror_x_aggerror_x_indexerror_x_index_norm
aa10120.988142291
ab10120.011857710.01171875
bb10010.98828125
cc211

 

Buf if I try to do these calculations with measures instead of columns, I get problems with duplicated values of the aggregates. I have tried many different things without success. Can you please help me?
 

 

 

1 ACCEPTED SOLUTION
sturlaws
Resident Rockstar
Resident Rockstar

Hi, @Anonymous ,

 

not sure what you are trying to do, but here is a possible strategy for you.

First create separate tables for Table[Var1] and Table[Var2], one table for each. Use these two tables for slicers. Then create measures like this:

error_x_agg =
IF (
    ISFILTERED ( Table_Var1[var1] );
    CALCULATE (
        SUM ( 'Table'[n_events] );
        FILTER (
            ALLEXCEPT ( 'Table'; 'Table'[error_x] );
            NOT ( 'Table'[var1] IN VALUES ( Table_Var1[var1] ) ) 
        )
    );
    CALCULATE ( SUM ( 'Table'[n_events] ); ALLEXCEPT ( 'Table'; 'Table'[error_x] ) )
)

 

This example only considers var1, you will have to see how you want to handle situations like: filter on Var1 and not Var2 vs filter on Var2 and not on Var1

 

Cheers,
Sturla

View solution in original post

2 REPLIES 2
Icey
Community Support
Community Support

Hi @Anonymous ,

Not completely solved. I fail to make the third measure "error_x_index_norm" does not work. PBIX file attached.

filter.PNG

 

Best Regards,
Icey

 

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

sturlaws
Resident Rockstar
Resident Rockstar

Hi, @Anonymous ,

 

not sure what you are trying to do, but here is a possible strategy for you.

First create separate tables for Table[Var1] and Table[Var2], one table for each. Use these two tables for slicers. Then create measures like this:

error_x_agg =
IF (
    ISFILTERED ( Table_Var1[var1] );
    CALCULATE (
        SUM ( 'Table'[n_events] );
        FILTER (
            ALLEXCEPT ( 'Table'; 'Table'[error_x] );
            NOT ( 'Table'[var1] IN VALUES ( Table_Var1[var1] ) ) 
        )
    );
    CALCULATE ( SUM ( 'Table'[n_events] ); ALLEXCEPT ( 'Table'; 'Table'[error_x] ) )
)

 

This example only considers var1, you will have to see how you want to handle situations like: filter on Var1 and not Var2 vs filter on Var2 and not on Var1

 

Cheers,
Sturla

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.