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
YZORI
New Member

Inventory Last Year

Hi all,
 
I need some help in order to calculate the inventory of any given period for previous year. Currently I am using the current DAX measure to calculate the latest inventory of any given month (sample data: sample_data.pbix😞
 
    CY INV = CALCULATE( SUM ( PowerQuery_inventory[RetailAmt]), LASTDATE(PowerQuery_inventory[FileCreationDate]))
 
Initially I used this measure to calculate the LY inventory's value:
 
INV PY ($) =
CALCULATE(SUM(PowerQuery_inventory[RetailAmt]),
        DATESBETWEEN(Dates_table[Dates],                    
            FIRSTDATE(DATEADD(Dates_table[Dates],-12,MONTH)),  
            IF(MONTH(MIN(Dates_table[Dates])) = MONTH(TODAY()), EDATE(TODAY(),-12) , LASTDATE(DATEADD(Dates_table[Dates],-12,MONTH)))
         )
     )
 
However this doesn't work as it adds up the inventory value for each week's entry.... I only need to return the inventory value for whatever the latest available date is for that particular month (does not necessarily mean the last date of the month). See theoritical example below, in this case it should only return the value 300 and not the total of 550 (which is currently the case).
YZORI_0-1679555597697.png
 
Any help is appreciated.
1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @YZORI ,

 

My solution don't need the calendar table.

Create a Year column and a Month column in your main table.

vstephenmsft_0-1679899243675.png

Create a measure to return the latest date in the same month of last year.

The latest date in the same month of last year =
CALCULATE (
    MAX ( 'PowerQuery_Inventory'[FileCreationDate] ),
    FILTER (
        ALLSELECTED ( PowerQuery_Inventory ),
        [Year]
            = MAX ( 'PowerQuery_Inventory'[Year] ) - 1
            && [Month] = MAX ( 'PowerQuery_Inventory'[Month] )
    )
)

vstephenmsft_2-1679899376572.png

You can create another metric or, like me, modify the metric to get the sum of the values of the latest date of the same month last year.

Inventory Last Year =
VAR latestdate =
    CALCULATE (
        MAX ( 'PowerQuery_Inventory'[FileCreationDate] ),
        FILTER (
            ALLSELECTED ( PowerQuery_Inventory ),
            [Year]
                = MAX ( 'PowerQuery_Inventory'[Year] ) - 1
                && [Month] = MAX ( 'PowerQuery_Inventory'[Month] )
        )
    )
RETURN
    CALCULATE (
        SUM ( PowerQuery_Inventory[RetailAmt] ),
        FILTER ( ALLSELECTED ( PowerQuery_Inventory ), [FileCreationDate] = latestdate )
    )

vstephenmsft_3-1679899724925.png

   

                                                                                                                                                         

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.           

 

View solution in original post

5 REPLIES 5
v-stephen-msft
Community Support
Community Support

Hi @YZORI ,

 

My solution don't need the calendar table.

Create a Year column and a Month column in your main table.

vstephenmsft_0-1679899243675.png

Create a measure to return the latest date in the same month of last year.

The latest date in the same month of last year =
CALCULATE (
    MAX ( 'PowerQuery_Inventory'[FileCreationDate] ),
    FILTER (
        ALLSELECTED ( PowerQuery_Inventory ),
        [Year]
            = MAX ( 'PowerQuery_Inventory'[Year] ) - 1
            && [Month] = MAX ( 'PowerQuery_Inventory'[Month] )
    )
)

vstephenmsft_2-1679899376572.png

You can create another metric or, like me, modify the metric to get the sum of the values of the latest date of the same month last year.

Inventory Last Year =
VAR latestdate =
    CALCULATE (
        MAX ( 'PowerQuery_Inventory'[FileCreationDate] ),
        FILTER (
            ALLSELECTED ( PowerQuery_Inventory ),
            [Year]
                = MAX ( 'PowerQuery_Inventory'[Year] ) - 1
                && [Month] = MAX ( 'PowerQuery_Inventory'[Month] )
        )
    )
RETURN
    CALCULATE (
        SUM ( PowerQuery_Inventory[RetailAmt] ),
        FILTER ( ALLSELECTED ( PowerQuery_Inventory ), [FileCreationDate] = latestdate )
    )

vstephenmsft_3-1679899724925.png

   

                                                                                                                                                         

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.           

 

Thank you @v-stephen-msft - I have modified it slightly to make it work for my working file - but this put me in the right direction. 

bhelou
Responsive Resident
Responsive Resident

INV PY ($) = 
CALCULATE(
    SUM(PowerQuery_inventory[RetailAmt]),
    FILTER(
        ALL(PowerQuery_inventory),
        YEAR(PowerQuery_inventory[FileCreationDate]) = YEAR(TODAY())-1 &&
        MONTH(PowerQuery_inventory[FileCreationDate]) = MONTH(DATEADD(DATESYTD(TODAY()), -12, MONTH)) &&
        PowerQuery_inventory[FileCreationDate] = CALCULATE(
            MAX(PowerQuery_inventory[FileCreationDate]),
            ALL(PowerQuery_inventory),
            YEAR(PowerQuery_inventory[FileCreationDate]) = YEAR(TODAY())-1,
            MONTH(PowerQuery_inventory[FileCreationDate]) = MONTH(DATEADD(DATESYTD(TODAY()), -12, MONTH))
        )
    )
)


//////You can copy and paste this code into your DAX editor and replace "PowerQuery_inventory" with the name of your inventory table.

Hey @bhelou unfortanutely this didn't work for me. I've added a sample of the data here for your reference: sample_data.pbix 

bhelou
Responsive Resident
Responsive Resident

Hello , please can you share some sample of the PBIX file ? , its a bit hard to guess what is happening here for the last year calculation . 
you can share a link via dropbox or drive . 


Regards , 

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.