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

Sum based on criteria in other column

Hi,

 

I'm a DAX/BI beginner and I'm struggling with a formula. I currently have measures that sum up my financial data and I then created a simple Difference using Actual - Budget. This works fine for Income transactions (it shows as positive and negative figures correctly), but for Expense transaction I would want it to show Budget - Actual.

 

I was wondering if this could be achieved by basing the calculation on  criteria in another column, something like;

 

Difference =

if ('Main'[I&E] = "Income", 'Main'[Actuals] - 'MainBudget'[Budget],

if ('Main'[I&E] = "Expense",'MainBudget'[Budget] -  'Main'[Actuals] )

 

However this tells me that a single value for column 'I&E' cannot be determined.

 

Could anybody advise how to achieve the effect I'm looking for?

 

Many thanks

5 REPLIES 5
Greg_Deckler
Super User
Super User

Can you provide some sample data?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi,

 

I've simplified the data for ease; 

 

Table: Main

Tran NoDateTypeDetailsTotalI&E
5277601/02/2017FundraisingTea Event400.00Income
5375401/02/2017DonationsCommunity donations100.00Income
5283401/02/2017DonationsCommunity donations30.00Income
5301501/02/2017EventsDeposit returned100.00Expense
5283301/02/2017EventsTea Event98.00Income
5284501/02/2017RetailGoods sold514.00Income
5290001/02/2017EventsRaffle50.00Income
7236701/02/2017EventsFundraising costs500.00Expense
5301601/02/2017StationeryMain office10.00Expense
7236501/02/2017TravelFundraising23.50Expense
5314601/02/2017DonationsRefund10.00Income

 

I then have a Measure to sum it; Actuals = SUM(Main[Total])

I have a separate Budget table with a similar measure to get the Budget totals. The Budget and Main table are linked by a Date table.

I then have a difference calculated by; Difference = 'Main'[Actuals] - 'Budget'[Budget].

 

 

I created a Matrix visualisation showing Budget, Actual and Difference

 

Income ActualsBudgetDifference
 Donations13010030
 Events148150-2
 Fundraising400500-100
 Retail514350164
     
Expense    
 Events400200200
 Stationery1040-30
 Travel23.530-6.5

 

But what I would like to see is;

 

Income ActualBudgetDifference
 Donations13010030
 Events148150-2
 Fundraising400500-100
 Retail514350164
     
Expenses    
 Events400200-200
 Stationery104030
 Travel23.5306.5

 

Essentially I want the signs flipped round on Difference when it is an Expense.

 

I'm unable to share my actual data or model, so hope this has made it clearer!

 

Thank you

Can you change your difference to:

 

Difference = IF('Main'[I&E]="Income",'Main'[Actuals] - 'Budget'[Budget],'Budget'[Budget] - 'Main'[Actuals])

?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi,

 

This brings up the message

 

"A single value for column 'I&E' in table 'Main' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result."

Hi @KSt,

 

Did you use a measure to calculate the difference? If so, please change to create a calculated column using the same formula for a test.

Difference =

if ('Main'[I&E] = "Income", 'Main'[Actuals] - 'MainBudget'[Budget],

if ('Main'[I&E] = "Expense",'MainBudget'[Budget] -  'Main'[Actuals] )

 

If issue persists, please share the sample data of Budget table so that I can reproduce your scenario.

 

Best regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.