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

Need Help in calculating the sum of two columns values based on different conditions and scenarios

Hello friends ,
I need to calculate the sum of units and sum of revenue in the below table based on the following condition:
if Code = a and code = any other then calculate the sum of units and revenue under Category1

If code = b and (code = c or code =d) and (code not a ) and code = any other calculate the sum under Category2

If code = b and ( code not c ) and (code not d ) and (code not a ) and code = any other calculate the sum under Category3

Please note that the sum of units should calculate only the average sum ( meaning for ID =1 , the sum of units of this particualar ID is 2

the sum of revenue is the sum of amounts in all lines ,for instanse :sume of revenue for ID=1 is 2890

 

IDCount of Units CodeRevenue 
12a20
12b500
12c100
12d20
12e2000
12f200
12g50
23b60
23b700
23c400
23d300
23b100
23g11
23f212
23g236
23f254
35b20
35g30
35i55
35h60
35m80

 

 

 

 

 

 

 

2 REPLIES 2
Sahir_Maharaj
Super User
Super User

Hello @Dubai80,

 

Can you please try:

 

1. Create a New Table

Categories = 
UNION(
    ROW("Code", "a", "Category", "Category1"),
    ROW("Code", "b", "Category", "Category2"),
    ROW("Code", "c", "Category", "Category2"),
    ROW("Code", "d", "Category", "Category2"),
    ROW("Code", "e", "Category", "Category1"),
    ROW("Code", "f", "Category", "Category1"),
    ROW("Code", "g", "Category", "Category1"),
    ROW("Code", "h", "Category", "Category3"),
    ROW("Code", "i", "Category", "Category3"),
    ROW("Code", "m", "Category", "Category3")
)

 

2. Create Measures

Total Units = SUM('YourTableName'[Count of Units])

Total Revenue = SUM('YourTableName'[Revenue])

Category1 Units = 
SUMX(
    FILTER(Categories, Categories[Category] = "Category1"),
    [Total Units]
)

Category1 Revenue = 
SUMX(
    FILTER(Categories, Categories[Category] = "Category1"),
    [Total Revenue]
)

Category2 Units = 
SUMX(
    FILTER(Categories, Categories[Category] = "Category2"),
    [Total Units]
)

Category2 Revenue = 
SUMX(
    FILTER(Categories, Categories[Category] = "Category2"),
    [Total Revenue]
)

Category3 Units = 
SUMX(
    FILTER(Categories, Categories[Category] = "Category3"),
    [Total Units]
)

Category3 Revenue = 
SUMX(
    FILTER(Categories, Categories[Category] = "Category3"),
    [Total Revenue]
)

Avg Units Per ID = 
AVERAGEX(
    SUMMARIZE('YourTableName', 'YourTableName'[ID], "Total Units", [Total Units]),
    [Total Units]
)

 

Should you require further assistance, please do not hesitate to reach out to me. 


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution?
➤ Lets connect on LinkedIn: Join my network of 13K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Thanks Sahir for the reply .

However , this is not the required .

I have explained the logic in the post .

Take ID=1 as an example .

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.

Top Solution Authors