Hello @Cedric,
Thank You for posting your query onto the Forum.
In order to achieve the solution based on the scenario that you’ve mentioned. Below are the measures alongwith the screenshot of the final results provided for the reference -
1. Opening Inventaire =
----- Calculation of Opening Inventory Ever -----
VAR _First_Inventory_Date_Ever =
CALCULATE( MIN( Tout[Date calculé pour rapport inventaire] ) ,
REMOVEFILTERS( ) )
VAR _Min_Date =
MIN( Dates[Date] )
VAR _Max_Balance_Date =
CALCULATETABLE(
ADDCOLUMNS(
SUMMARIZE(
Tout ,
Tout[Type du produit] ) ,
"@Max_Balance_Date" ,
CALCULATE( MAX( Tout[Date calculé pour rapport inventaire] ) ) ) ,
Dates[Date] <= _Min_Date ,
Tout[Type du produit] = "Inventaire" )
VAR _Max_Balance_Dates_With_Lineage =
TREATAS(
_Max_Balance_Date ,
Tout[Type du produit] ,
Dates[Date] )
VAR _Opening_Inventory_Ever =
CALCULATE( SUM( Tout[Quantité calculé pour inventaire] ) ,
_Max_Balance_Dates_With_Lineage )
----- Calculation of Previous Month Cumulative Entrant -----
VAR _Previous_Month_Cumulative_Entrant =
CALCULATE(
CALCULATE( SUM( Tout[Quantité calculé pour inventaire] ) ,
DATEADD( Dates[Date] , -1 , MONTH ) ,
Tout[Type du produit] = "Entrant" ) ,
FILTER( ALLSELECTED( Dates[Date] ) ,
Dates[Date] <= MAX( Dates[Date] ) ) )
----- Calculation of Previous Month Cumulative Entrant -----
VAR _Previous_Month_Cumulative_Sortant =
CALCULATE(
CALCULATE( SUM( Tout[Quantité calculé pour inventaire] ) ,
DATEADD( Dates[Date] , -1 , MONTH ) ,
Tout[Type du produit] = "Sortant" ) ,
FILTER( ALLSELECTED( Dates[Date] ) ,
Dates[Date] <= MAX( Dates[Date] ) ) )
----- Calculation of Final Results -----
VAR _Results =
SWITCH( TRUE() ,
ISINSCOPE( Dates[MM-YYYY] ) && _First_Inventory_Date_Ever = _Min_Date , _Opening_Inventory_Ever ,
ISINSCOPE( Dates[MM-YYYY] ) , _Opening_Inventory_Ever + _Previous_Month_Cumulative_Entrant + _Previous_Month_Cumulative_Sortant )
RETURN
_Results
2. Entrant =
IF(
ISINSCOPE( Dates[MM-YYYY] ) ,
CALCULATE( SUM( Tout[Quantité calculé pour inventaire] ) ,
KEEPFILTERS( Tout[Type du produit] = "Entrant" ) ) )
3. Sortant =
IF(
ISINSCOPE( Dates[MM-YYYY] ) ,
CALCULATE( SUM( Tout[Quantité calculé pour inventaire] ) ,
KEEPFILTERS( Tout[Type du produit] = "Sortant" ) ) )
4. Closing Inventaire =
----- Calculation of Opening Inventory Ever -----
VAR _Min_Date =
MIN( Dates[Date] )
VAR _Max_Balance_Date =
CALCULATETABLE(
ADDCOLUMNS(
SUMMARIZE(
Tout ,
Tout[Type du produit] ) ,
"@Max_Balance_Date" ,
CALCULATE( MAX( Tout[Date calculé pour rapport inventaire] ) ) ) ,
Dates[Date] <= _Min_Date ,
Tout[Type du produit] = "Inventaire" )
VAR _Max_Balance_Dates_With_Lineage =
TREATAS(
_Max_Balance_Date ,
Tout[Type du produit] ,
Dates[Date] )
VAR _Opening_Inventory_Ever =
CALCULATE( SUM( Tout[Quantité calculé pour inventaire] ) ,
_Max_Balance_Dates_With_Lineage )
----- Calculation of Cumulative Entrant -----
VAR _Cumulative_Entrant =
CALCULATE( SUM( Tout[Quantité calculé pour inventaire] ) ,
Tout[Type du produit] = "Entrant" ,
FILTER( ALLSELECTED( Dates[Date] ) ,
Dates[Date] <= MAX( Dates[Date] ) ) )
----- Calculation of Cumulative Sortant -----
VAR _Cumulative_Sortant =
CALCULATE( SUM( Tout[Quantité calculé pour inventaire] ) ,
Tout[Type du produit] = "Sortant" ,
FILTER( ALLSELECTED( Dates[Date] ) ,
Dates[Date] <= MAX( Dates[Date] ) ) )
----- Calculation of Results -----
VAR _Results =
IF(
ISINSCOPE( Dates[MM-YYYY] ) ,
_Opening_Inventory_Ever + _Cumulative_Entrant + _Cumulative_Sortant )
RETURN
_Results
I’m also attaching the working of the PBIX file for the reference purposes.
Hoping you find this helpful and meets your requirements that you’ve been looking for.
Thanks and Warm Regards,
Harsh
Inventory Calculation - Harsh.pbix (236.4 KB)