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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

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? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ 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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Users online (1,491)