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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Anonymous
Not applicable

sum only when date is present in both tables

hello everyone!

I have the following problem, I can't figure out how to solve it.

I have 2 tables Cost and Rent, both linked via a calendar table

 

I need to sum the values from Cost and Rent only when they are present in the Cost table, otherwise 0,

a simple calculation works fine for single month selected,

Total Cost =
CALCULATE
(
        [cost],
        Acc[Code] = "160")
    +
    CALCULATE(
        [rent],
        Acc[Code] = "160")

but when I select YTD (or deselect the all months, only the year is selected), it returns me an incorrect value (summing the total cost and rent),
is there a way to include the values in the total cost only when [cost] is present from the beginning of the year
 
Example:
 Cost table
DateCost
01-01-2023100
01-03-2023150

Rent table
DateRent
01-01-202350
01-02-2023100
01-03-2023150

right now it returns me YTD = 550, 
(Jan, 23 = Cost + Rent => 100+50 = 150
Feb, 23 = Cost + Rent => 0+100 = 100 (Cost is not present)
Mar, 23 = Cost + Rent => 150+150 = 300)

but it should return me:
Total Cost = 450
(Jan, 23 = Cost + Rent => 100+50 = 150
Feb, 23 = 0 (Cost is not present)
Mar, 23 = Cost + Rent => 150+150 = 300)

and it should work with a single month as well
 
is that possible to achieve ?
1 ACCEPTED SOLUTION

Try this version

Total costs =
CALCULATE (
    SUMX (
        DISTINCT ( Costs[Date] ),
        VAR CurrentDate = Costs[Date]
        RETURN
            CALCULATE ( [Costs] + [Rents], TREATAS ( { CurrentDate }, 'Calendar'[Date] ) )
    ),
    'Acc'[Code] = "160"
)

View solution in original post

26 REPLIES 26

From the behaviour we have seen I would guess that there is some rogue data in Costs which is causing a date from that table to be picked up when it shouldn't be.

If you identify a particular period when the data is wrong then examine all the Costs data for that period, regardless of other filters. That might shed some light on where the issue is.

Anonymous
Not applicable

Good news,
I managed to get the correct value using SUMX(ValuesGEO[City], your code).

 

Also thanks to Alberto Ferrari and his video, I figured out that I need to iterate on city level to get what I wanted 😁
https://www.youtube.com/watch?v=6rgAkejrup8&ab_channel=SQLBI 

Their videos are always must-watch.

Glad you solved it.

Anonymous
Not applicable

it worked!
thanks a lot, do you by any chance know what was the issue ? why simple filter in calculate didn't work ?

I'm nowhere near sure on this, but I think its to do with the way that queries are executed to produce a matrix, particularly a matrix with a hierarchy on the roles. Several queries are produced and then combined, and my guess is that at some level the filter we were generating resulted in an empty table, or a blank value, and that led to its being ignored for some parts of the calculation.

When you had the filter on the visual then that would be applied to all of the different queries and that's why it worked.

Anonymous
Not applicable

i tried it previously, for some reason it doesn't help

Ali_Shakh_0-1688479954921.png

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.