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

Sum for last days of month per each category - how to simplify

Hello, 

 

I have a quite peculiar problem. I have a column with values that represents the state of Inventory for each Site (category). 
Which means that the most recent one value for each month is always last day per each site per month. 

Example

for site 667 for november its going to be value 5 252 235.74 (31/12/2021) but for site 200 its going to be 79 967 894.18 (30/12/2021)

image_2022-06-14_203943006.png

 

The sum of those values should be 85 220 129.92 which is state of inventory for those two sites per december. 

I was able to calculate this with this measure: 

 

 

 

Inventory Cost = 
VAR _pretable =
    ADDCOLUMNS (
        SUMMARIZE (
            v_factinventorytransactions,
            v_dimdate[DateId],
            v_factinventorytransactions[SiteId]
        ),
        "InventoryCost", CALCULATE ( AVERAGE ( v_factinventorytransactions[RunningCost] ) )
    )
VAR _table =
    FILTER (
        _pretable,
        VAR _MaxDate =
            CALCULATE (
                MAX ( v_factinventorytransactions[InventoryTransactionDateId] ),
                ALLSELECTED ( v_dimdate[DateId] )
            )
        RETURN
            v_dimdate[DateId] = _MaxDate
    )
RETURN
    SUMX ( _table, [InventoryCost] )

 

 

 

 

Which works perfectly but I'm wondering if it can be simplyfied. I want it to simplify, because when I want to use this measure inside another one that sums those Inventory Cost values per month for last 3 months and I have wrong answers. 
Which means that this Inventory Cost measure works but if I call out this measure in the one below it shows wrong numbers (but other measures, more simply ones work). 

 

 

 

Rolling3Months = 
VAR _EndDate = MAX(v_dimdate[Date])
VAR _Dates =  DATESINPERIOD(v_dimdate[Date], _EndDate, -3, MONTH)
VAR _Cost = [Inventory Cost]
VAR _Inventory = SUMX(_Dates, CALCULATE(_Cost, ALL(v_dimdate[YearMonth])))
RETURN 
_Inventory 

 

 

 

 

I'm a little bit stuck and would be super appreciated when someone would pointed out my mistakes/errors here.

I'm also providing sample power BI file with those. 
https://we.tl/t-eQSOYHm1ft

Thank you 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please try the below measure.

I also attached the pbix file.

 

 

Rolling3Months =
VAR _EndDate =
    MAX ( v_dimdate[Date] )
VAR _months =
    SUMMARIZE (
        FILTER (
            ALL ( v_dimdate ),
            v_dimdate[Date] IN DATESINPERIOD ( v_dimdate[Date], _EndDate, -3, MONTH )
        ),
        v_dimdate[YearMonth]
    )
VAR _Inventory =
    SUMX ( _months, [Inventory Cost] )
RETURN
    IF ( [Inventory Cost] <> BLANK (), _Inventory )

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

1 REPLY 1
Jihwan_Kim
Super User
Super User

Hi,

Please try the below measure.

I also attached the pbix file.

 

 

Rolling3Months =
VAR _EndDate =
    MAX ( v_dimdate[Date] )
VAR _months =
    SUMMARIZE (
        FILTER (
            ALL ( v_dimdate ),
            v_dimdate[Date] IN DATESINPERIOD ( v_dimdate[Date], _EndDate, -3, MONTH )
        ),
        v_dimdate[YearMonth]
    )
VAR _Inventory =
    SUMX ( _months, [Inventory Cost] )
RETURN
    IF ( [Inventory Cost] <> BLANK (), _Inventory )

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


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.

Top Solution Authors