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
Renan-rdo
Frequent Visitor

Issue with SUM outside filters

I'm trying to replicate a business calculation that exists in my environment on PowerBI, but I'm not getting what I expect as a result.

I have 2 tables that I made as an example and replicate the real life problem.

Table Sales:

dateSold      Sector    Idsale  NPS
01/12/2021    A         1       good
01/11/2021    A         2       bad
01/11/2021    A         3       good
01/11/2021    B         4       bad
01/10/2021    A         5       bad
01/10/2021    B         6       good
01/10/2021    B         7       good
01/09/2021    A         8       good
01/09/2021    A         9       good
01/09/2021    A         10      good

And Weights table:

SECTOR  Weight
A       0,7
B       0,3

I created some fields to do my math and they do what I expect, but the last field doesnt. I did a COUNT for GOOD and BAD and total values on the last 3 months based on field NPS using this method:

QuantityGOOD = 
CALCULATE(
    COUNTA(Sales[Idsale])
    , DATESINPERIOD(Sales[dateSold], SELECTEDVALUE(Sales[dateSold]), -3, MONTH)
    , Sales[NPS] == "good"
)

Then Created a measure with the math using my fields:

NPSCalculated = 
[QuantityGOOD] / [QuantityTotal] - [QuantityBad] / [QuantityTotal] 

Then in the last step I have to multiply this value by the weights in the other table, and I tryed this:

FinalValue = 
CALCULATE(
SUMX(
    SUMMARIZE(Sales ,Sales[dateSold], weights[Sector]),
    [NPSCalculated] * 
    CALCULATE(
        AVERAGE(weights[weight]))))

But this code cuts out the values of the sector A that exists in the previous months and doesnt exist in the target month. It returns the sum without the part missing.

SaleDate    Sector  bad   good  total   NPS     weight  FinalValue
2021-12     A       2     2     4       0       0,7     0
2021-12     B       1     2     3       0,333   0,3     0,1

I need to get the results of the column FinalValue as above (did it in excel for 2021-12 only), but instead I get the image below, can you guys help me?

Renanrdo_0-1659476314694.png

 

1 ACCEPTED SOLUTION

Hi @Renan-rdo ,

 

Is this right?

FinalValue =
VAR _s =
    ADDCOLUMNS (
        CROSSJOIN ( VALUES ( 'Calendar'[Month] ), 'Weight' ),
        "final", [Weight] * [NPSCalculated]
    )
RETURN
    SUMX ( _s, [final] )

 

Result:

ChenwuZhu_Gmail_0-1660185256244.png

 

Best Regards

 

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

 

 

View solution in original post

6 REPLIES 6
v-chenwuz-msft
Community Support
Community Support

Hi @Renan-rdo ,

 

You need create a new date table for this visual via Calendar = CALENDAR(MIN('Sales'[dateSold]),MAX('Sales'[dateSold]))

Then add a calculate column named month like the below.

vchenwuzmsft_0-1659692420353.png

 

In the table visual, replace [dateSold] with [Month], which will let the table visual get a Cartesian product of [month] and [SECTOR].

 

And change the parameter in your measure DATESINPERIOD(). Replace the dateSold column with the new date column.

DATESINPERIOD('Sales'[dateSold],MIN('Calendar'[Date]),-3,MONTH))
 

Result:

vchenwuzmsft_1-1659692497393.png

 

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

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

 

Hi @v-chenwuz-msft, thanks for the reply!

Well, creating this table really solved the issue of computing all values, now we are close to what I need, but the final value is still not correct. 

Basically this is the operation:
FinalValue = NPSCalculated * Weight

And so, it should be:
FinalValue = 0,33 * 0,3
FinalValue = 0,1

I think we are closer to the solution, but I still cant find whats wrong with it. Looks like the value is duplicated.

Do you have any idea on how to solve this final step? Again, all the rest looks just perfect, thank you for that!

Hi @Renan-rdo ,

 

ChenwuZhu_Gmail_1-1660113390848.png

Maybe you can modify the code like above screenshot shows.

 

Best Regards

 

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

Hi @ChenwuZhu_Gmail,

Well, it is partially right. While ussing the table with the weight on it the result is the expected as you shown.

The thing is: I'm actually not planning to use the complete table, just the Month and the Final Value and put then in a line graph. So the results should consider the respective weight when it is not being used as a category.

The previous solution is ok for 2021-12, but is wrong for 2021-11 when not using the weight on the table:

Renanrdo_0-1660157076859.png

On 2021-11 the final value should be 0,33. Is it possible we get these results while in this conditions @v-chenwuz-msft ?


 

Hi @Renan-rdo ,

 

Is this right?

FinalValue =
VAR _s =
    ADDCOLUMNS (
        CROSSJOIN ( VALUES ( 'Calendar'[Month] ), 'Weight' ),
        "final", [Weight] * [NPSCalculated]
    )
RETURN
    SUMX ( _s, [final] )

 

Result:

ChenwuZhu_Gmail_0-1660185256244.png

 

Best Regards

 

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

 

 

@ChenwuZhu_Gmail thanks a lot for this reply, it really solves the case showed and helped in my case. I will have to open a new one due to my issue having another specific cross between tables, but you already helped, thanks for the effort!

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.