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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
XuanNguyen
Regular Visitor

Calculate Difference Between Month for Quantity with Filters

XuanNguyen_0-1714148127904.png

Hi, 

I want to calculate the month-over-month difference in quantity while FILTERING based on the same platformID and ShopID.

Example: 
row1 in table: 
P001-S001 - month 1 is 726 

P001-S001 - month 2 = month 2-month 1 = 769-726 = 43

....

Please help me. Thanks

2 ACCEPTED SOLUTIONS
AntrikshSharma
Super User
Super User

@XuanNguyen Try this:

 

Diff w OFFSET = 
VAR CurrentQuantity = 
    'Table'[Quantity]
VAR PreviousQuantity = 
    OFFSET ( 
        -1,
        ALL ( 
            'Table'[ProductID], 
            'Table'[PlatformID], 
            'Table'[ShopID], 
            'Table'[CrawlMonth], 
            'Table'[Quantity] 
        ),
        ORDERBY ( 'Table'[CrawlMonth], ASC ),
        PARTITIONBY ( 'Table'[ProductID], 'Table'[PlatformID], 'Table'[ShopID] )    
    )
VAR PreviousQuantityValue = 
    SELECTCOLUMNS ( PreviousQuantity, 'Table'[Quantity] )
VAR Result = 
    CurrentQuantity - PreviousQuantityValue
RETURN 
    Result

 

Capture.PNG

View solution in original post

Hello @XuanNguyen ,

 

In order to achieve your purpose, you need to create a calculated column with the following DAX (replace the name of the table MoM with your own table name)

VAR CurrentQuantity = 'MoM'[Quantity]
VAR PreviousMonthQuantity = 
    CALCULATE(
        MAX('MoM'[Quantity]),  
        FILTER(
            'MoM',
            'MoM'[ProductID] = EARLIER('MoM'[ProductID]) &&
            'MoM'[PlatformID] = EARLIER('MoM'[PlatformID]) &&
            'MoM'[ShopID] = EARLIER('MoM'[ShopID]) &&
            'MoM'[CrawlMonth] = EARLIER('MoM'[CrawlMonth]) - 1
        )
    )
RETURN
IF(
    ISBLANK(PreviousMonthQuantity),
    CurrentQuantity,
    CurrentQuantity - PreviousMonthQuantity
)

 Best regards,

Alex




Did I answer your question? Mark my post as a solution!
Appreciate your Like/Kudos

Proud to be a Super User!




View solution in original post

4 REPLIES 4
AntrikshSharma
Super User
Super User

@XuanNguyen Try this:

 

Diff w OFFSET = 
VAR CurrentQuantity = 
    'Table'[Quantity]
VAR PreviousQuantity = 
    OFFSET ( 
        -1,
        ALL ( 
            'Table'[ProductID], 
            'Table'[PlatformID], 
            'Table'[ShopID], 
            'Table'[CrawlMonth], 
            'Table'[Quantity] 
        ),
        ORDERBY ( 'Table'[CrawlMonth], ASC ),
        PARTITIONBY ( 'Table'[ProductID], 'Table'[PlatformID], 'Table'[ShopID] )    
    )
VAR PreviousQuantityValue = 
    SELECTCOLUMNS ( PreviousQuantity, 'Table'[Quantity] )
VAR Result = 
    CurrentQuantity - PreviousQuantityValue
RETURN 
    Result

 

Capture.PNG

Alex87
Solution Sage
Solution Sage

can we share the data in a way we can copy it ? 




Did I answer your question? Mark my post as a solution!
Appreciate your Like/Kudos

Proud to be a Super User!




https://docs.google.com/spreadsheets/d/1UuUASPU-JqQ5knkoDtUvdC3m-yYcZXHs/edit?usp=sharing&ouid=10199...

Here you are.

(You can download it, and just need to use data with filter storage = 256gb)

Hello @XuanNguyen ,

 

In order to achieve your purpose, you need to create a calculated column with the following DAX (replace the name of the table MoM with your own table name)

VAR CurrentQuantity = 'MoM'[Quantity]
VAR PreviousMonthQuantity = 
    CALCULATE(
        MAX('MoM'[Quantity]),  
        FILTER(
            'MoM',
            'MoM'[ProductID] = EARLIER('MoM'[ProductID]) &&
            'MoM'[PlatformID] = EARLIER('MoM'[PlatformID]) &&
            'MoM'[ShopID] = EARLIER('MoM'[ShopID]) &&
            'MoM'[CrawlMonth] = EARLIER('MoM'[CrawlMonth]) - 1
        )
    )
RETURN
IF(
    ISBLANK(PreviousMonthQuantity),
    CurrentQuantity,
    CurrentQuantity - PreviousMonthQuantity
)

 Best regards,

Alex




Did I answer your question? Mark my post as a solution!
Appreciate your Like/Kudos

Proud to be a Super User!




Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.