cancel
Showing results 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.

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

 ID Count of Units Code Revenue 1 2 a 20 1 2 b 500 1 2 c 100 1 2 d 20 1 2 e 2000 1 2 f 200 1 2 g 50 2 3 b 60 2 3 b 700 2 3 c 400 2 3 d 300 2 3 b 100 2 3 g 11 2 3 f 212 2 3 g 236 2 3 f 254 3 5 b 20 3 5 g 30 3 5 i 55 3 5 h 60 3 5 m 80

2 REPLIES 2
Super User

Hello @Dubai80,

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
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning
Frequent Visitor

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 .

Announcements

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

Power BI Monthly Update - April 2024

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

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors