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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
KW123
Helper V
Helper V

Subtracting two rows

Hi, 

I am hoping someone can help.  I have a column with sales values and I need to create a DAX where I minus A from B to get C but then take C and minus it from B to get D.  Then D-B=E etc for the entire month. I have a dates table and an index. 

A-B=C
C-B=D
D-B=E
E-B=F
etc

Is there a way to do this? 

Thanks, 

2 ACCEPTED SOLUTIONS
tamerj1
Super User
Super User

Hi @KW123 
Sorry it was too late yesterday I couldn't reply to you. Here is a sample file with the solution jnowing that you will retun back to me with more information that you've been hiding as usual 😉https://www.dropbox.com/t/lzKxh0DxLA056sy7

Result = 
VAR A = 1000
VAR B = 10
VAR CurrentIndex = SELECTEDVALUE ( Data[INDEX] )
VAR MaxIndex = CALCULATE ( MAX ( Data[INDEX] ), ALLEXCEPT ( Data, Data[Day].[Month] ) )
RETURN
    IF ( 
        CurrentIndex <> BLANK ( ),
        A - ( MaxIndex - CurrentIndex ) * B
    )



View solution in original post

@KW123 

What does YTD total need to be =
VAR A = [Accounting goal calc-c]
VAR B = [Daily Goal]
VAR CurrentIndex =
    SELECTEDVALUE ( Dates[FD2] )
VAR MaxIndex =
    CALCULATE ( MIN ( Dates[FD2] ), ALLEXCEPT ( Dates, Dates[Date] ) )
RETURN
    IF (
        DAY ( SELECTEDVALUE ( Dates[Day] ) ) = 1
            && MONTH ( SELECTEDVALUE ( Dates[Day] ) ) = 1,
        0,
        IF ( CurrentIndex <> BLANK (), A + ( MaxIndex - CurrentIndex ) * B )
    )

View solution in original post

26 REPLIES 26
tamerj1
Super User
Super User

Hi @KW123 
Sorry it was too late yesterday I couldn't reply to you. Here is a sample file with the solution jnowing that you will retun back to me with more information that you've been hiding as usual 😉https://www.dropbox.com/t/lzKxh0DxLA056sy7

Result = 
VAR A = 1000
VAR B = 10
VAR CurrentIndex = SELECTEDVALUE ( Data[INDEX] )
VAR MaxIndex = CALCULATE ( MAX ( Data[INDEX] ), ALLEXCEPT ( Data, Data[Day].[Month] ) )
RETURN
    IF ( 
        CurrentIndex <> BLANK ( ),
        A - ( MaxIndex - CurrentIndex ) * B
    )



@tamerj1 
Thank you very much for your help! I think this is what I am looking for.  The thing is, when I change the month, it isn't starting from the months goal ($1000 in the example case) How would we get it to start at the current months goal? It looks as though February is starting at the VAR B amount for that month 

@KW123 

please provide sample of the expected results 

JANUARY 
VAR A= $1000 
VAR B= $10 

DayINDEXWhat I am trying to calculate
1 Saturday1$810
2 Sunday  
3 Monday (HOLIDAY)1$810
4 Tuesday2$820
5 Wednesday3$830
6 Thursday4$840
7 Friday5$850
8 Saturday5$850
9 Sunday  
10 Monday6$860
11 Tuesday7$870
12 Wednesday8$880
13 Thursday9$890
14 Friday10$900
15 Saturday10$900
16 Sunday  
17 Monday HOLIDAY 10$900
18 Tuesday11$910
18 Wednesday12$920
20 Thursday13$930
21 Friday14$940
22 Saturday14$940
23 Sunday  
24 Monday15$950
25 Tuesday16$960
26 Wednesday17$970
27 Thursday18$980
28 Friday19$990
29 Saturday19$990
30 Sunday  
31 Monday20$1000 (I only know this number, I need to work in reverse from here)

 

FEBRUARY
VAR A=$2500
VAR B= $50

DayINDEXWhat I am trying to calculate
1 Tuesday1$650
2 Wednesday2$700
3 Thursday 3$750
4 Friday4$800
5 Saturday4$800
6 Sunday  
7 Monday5$850
8 Tuesday6$900
9 Wednesday7$950
10 Thursday8$1000
11 Friday9$1050
12 Saturday9$1050
13 Sunday  
14 Monday10$2000
15 Tuesday11$2050
16 Wednesday12$2100
17 Thursday 13$2150
18 Friday14$2200
18 Saturday14$2200
20 Sunday  
21 Monday HOLIDAY14$2200
22 Tuesday15$2250
23 Wednesday16$2300
24 Thursday17$2350
25 Friday18$2400
26 Saturday18$2450
27 Sunday  
28 Monday19$2500


In my report, instead of starting at "$650" it's starting at "$50" Does that make sense? 

@KW123 

yes all clear. Best approach is create a year month number column. I assume you have a date column so the new column would be 

YEAR ( Data[Date] ) * 100 + MONTH ( Data[Date] )

then use this column inside ALLEXCEPT instead of Data[Date].[Month]

@tamerj1 
I think I found the issue.  The [Accounting goal calculation] is a column imported direct from excel.  I wasn't able to plug it into the formula so I created a measure for it (see screen shot) but it doesn't appear to show any numbers.  This could be why the Dax you gave me isn't working? AccountinggoalC.png

@KW123 
I think we need to connect to clarify the pivture. Please let me know when we can do that

@tamerj1 I got it!!! I had to slightly adjust the DAX but it's working.  

My only other question is that the January 1 (Saturday) and January 3 (Monday) have the same Index number which needs to stay the same.  Is there a way however to have the value we just calculated for January 1 be $0? Since it's the first day of the year there should be no values.  But of course Monday has a value since we had sales that day.  Maybe an IF clause???

@KW123 
Yes just add IF ( DAY ( SELECTEDVALUE ( Data[Day] ) = 1, 0, IF ( CurrentIndex <> BLANK ( ), A - ( MaxIndex - CurrentIndex ) * B ) )

What does YTD total need to be.png

@KW123 
Delete the closing bracket after the 0

What does YTD total need to be.png

@KW123 
Please copy paste the code in a reply so I can edit it for you.

@tamerj1 

What does YTD total need to be =
VAR A = [Accounting goal calc-c]
VAR B = [Daily Goal]
VAR CurrentIndex = SELECTEDVALUE ( Dates[FD2] )
VAR MaxIndex = CALCULATE ( MIN ( Dates[FD2] ), ALLEXCEPT ( Dates, Dates[Date] ) )
RETURN
IF ( DAY ( SELECTEDVALUE ( Dates[Day] ) = 1, 0,
IF ( CurrentIndex <> BLANK ( ), A + ( MaxIndex - CurrentIndex ) * B )

@KW123 

Here you go

What does YTD total need to be =
VAR A = [Accounting goal calc-c]
VAR B = [Daily Goal]
VAR CurrentIndex =
    SELECTEDVALUE ( Dates[FD2] )
VAR MaxIndex =
    CALCULATE ( MIN ( Dates[FD2] ), ALLEXCEPT ( Dates, Dates[Date] ) )
RETURN
    IF (
        DAY ( SELECTEDVALUE ( Dates[Day] ) ) = 1,
        0,
        IF ( CurrentIndex <> BLANK (), A + ( MaxIndex - CurrentIndex ) * B )
    )

@tamerj1 This works thank you!!! Now it's making each first of each month $0.  I just need it to make January 1 = $0

@KW123 

What does YTD total need to be =
VAR A = [Accounting goal calc-c]
VAR B = [Daily Goal]
VAR CurrentIndex =
    SELECTEDVALUE ( Dates[FD2] )
VAR MaxIndex =
    CALCULATE ( MIN ( Dates[FD2] ), ALLEXCEPT ( Dates, Dates[Date] ) )
RETURN
    IF (
        DAY ( SELECTEDVALUE ( Dates[Day] ) ) = 1
            && MONTH ( SELECTEDVALUE ( Dates[Day] ) ) = 1,
        0,
        IF ( CurrentIndex <> BLANK (), A + ( MaxIndex - CurrentIndex ) * B )
    )

@tamerj1 
That was it!! Thank you so so much for all of your help, I really appreciate it. 

Yearmonthno.png

@tamerj1 
Thank you! It still doesn't seem to be returning the correct values......

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!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.