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
GKs
Frequent Visitor

Sum Unique Quantity for Total Correct

I have worked with this for a couple of days and I really can't find the solution. I appreciate the support you can give me.
In a data that has the color, id number and quantity I am trying to unify a single row.
Primary (Yellow, Black, Soft, Red) and Secondary (Gray and blue), so I want to group the secondary into the primary.

 

I have used all these combinations of formulas but none gives me the expected result. the ones that are closer to expected are these:

SUMX(DISTINCT(Data[Id]),FIRSTNONBLANK(Data[Second],CALCULATE(SUM(Data[Qty],FILTER(Data,Data[Id] = EARLIER(Data[Id])))))

SUMX(DISTINCT(Data[Id]),LASTNONBLANK(Data[Third],SUM(Data[Third])))
 
This are other combination
SUMX(GROUPBY(Data,Data[Color],Data[Qty]),SUM(Data[Qty]))
SUMX( SUMMARIZE( VALUES(Data[Id]), "Total Second",SUM(Data[Third]) ), MIN(Data[Second]))
 
I enclose an image of how I would expect it to show the results. I took this data to Excel and made it manually for the purpose of the example.
 
Capture.JPG
1 ACCEPTED SOLUTION

Hi @GKs 

 

Maybe there was a problem with the previous link. Can you try this one?

 

Let me know if it works

 

LC

View solution in original post

11 REPLIES 11
lc_finance
Solution Sage
Solution Sage

Hi @GKs ,

 

 

You can find my proposed solution here.

 

I created calculated columns for First and Second:

First = IF([Category] IN {"Yellow", "Black", "Red", "White"},[Qty],0)
Second = IF(NOT [Category] IN {"Yellow", "Black", "Red", "White"},[Qty],0)

 

And a calculated measure for Third:

Third = 

var categoryIsTotal = NOT ISFILTERED(Colors[Category])
var categoryIsPrimary = SELECTEDVALUE(Colors[Category]) IN {"Yellow", "Black", "Red", "White"}

RETURN SWITCH(TRUE,
 categoryIsPrimary, CALCULATE(SUM('Colors'[Second]),ALLEXCEPT('Colors','Colors'[Id]))
 ,categoryIsTotal, SUMX(VALUES('Colors'[Id]), CALCULATE(SUM('Colors'[Second])) )
)

 

Finally, here is a screenshot:

Filter by color.png

Let me know if this is what you are looking for.

 

LC

Interested in Power BI and DAX tutorials? Check out my blog at www.finance-bi.com

Hi @lc_finance 

I appreciate your help the first and second columns work great.
For the third measure, I replicated it, however, it is totaling based on all the IDs in the case that I want to see it as a summary of all the records by categories.

The following image is how it is being displayed. Try to extract an part for the previous example, but taking it to the full data you can see the amount of 637,628.
Expected Column: Third, Blue and Gray should be 0, and should show only the quantities, where the ID has First.
Looking for the quantity that were sold with colors First.

 

Capture 2.JPG

 

 

Hi @GKs ,

 

 

can you share your Power BI file?

That will help me to understand why you get those results and how to fix the formula.

 

You can use One Drive, Google Drive or another similar tool to share your Power BI file.

 

Regards,

 

LC

Hi @GKs ,

 

 

I just checked and your Google Drive file is private. 

Let me know when you make it public so I can access it,

 

LC

Please try again, thanks you for your help

Hi @GKs ,

 

 

I apologize for the late reply.

 

I tried to modify the formula on the old example, you can download it from here.

It's quite a complicated formula, so I used the old example as it has fewer rows so it's easier for testing and understanding the formula.

 

Can you let me know if that is what you are looking for?

 

LC

 

 

id used as filter the formula does what is expected but if you take the id from the box or filter the value is equal to the second.
In essence, what is expected is to see a total real value without the need to filter id by id.
The formula is the ideal I keep using it to see if I can perform the calculation as expected.
I think the topic would be SUMX since it is adding a whole or maybe doing a SUM with FILTER.

Hi @GKs 

 

Based on the Power BI I sent yesterday, Third is no longer equal to Second even if Id is taken away from the table.

 

In the example, the total for Third is 84 and the total for Second is 241.

84 is the sum of 68 for transaction 8326 and 16 for 8468.

 

A paste a screenshot below.

Totals by color tables.png

 

Is this what you are looking for?

 

LC

Yes, you did it.
But you won't believe me for some reason, my Power BI doesn't SUM well, download the file I did't make any modification just remove the id from the table. I don't know why this happens, If it's an update that I don't have or maybe I did.Untitled3.png

Hi @GKs 

 

Maybe there was a problem with the previous link. Can you try this one?

 

Let me know if it works

 

LC

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.