Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Need a measure to return minimum dates for each rows

I am trying to return the first minimum date based on the group of -ve and -ve value for all the rows .

 

I need a measure to calculate the minimum date here because stock is calculated based on two different tables.

 

See the examples as below. 

 

datematerialstock
6/1/2022101-200
7/1/2022101-100
8/1/2022101100
9/1/2022101200
10/1/2022101-100
11/1/2022101-50

Expected Result :

datematerialstockresult
6/1/2022101-2006/1/2022
7/1/2022101-1006/1/2022
8/1/20221011008/1/2022
9/1/20221012008/1/2022
10/1/2022101-10010/1/2022
11/1/2022101-5010/1/2022

 

 

Can someone help me on this please?

amitchandak Ashish_Mathur 

 

4 Replies

  • Hi,

    I am not sure if I understood your question correctly, but I assumed the expected outcome shows minimum date for the groups of minus stocks or plus stocks.

    Please check the below picture and the attached pbix file.

    All measures are in the attached pbix file, and each of them are numbered as a step to follow.

     

     

    step one plus minus change index: = 
    VAR _currentstock =
        SUM ( Data[stock] )
    VAR _currentdate =
        MAX ( Data[date] )
    VAR _currentmaterial =
        MAX ( Data[material] )
    VAR _previousdate =
        MAXX (
            FILTER (
                ALL ( Data ),
                Data[material] = _currentmaterial
                    && Data[date] < _currentdate
            ),
            Data[date]
        )
    VAR _previousstock =
        SUMX (
            FILTER (
                ALL ( Data ),
                Data[material] = _currentmaterial
                    && Data[date] = _previousdate
            ),
            Data[stock]
        )
    RETURN
    IF( HASONEVALUE( Data[date]),
        IF ( _currentstock * _previousstock < 0, 1, 0 )
    )

     

    step two Group of change index: = 
    IF (
        HASONEVALUE ( Data[date] ),
        SUMX (
            FILTER (
                ALL ( Data ),
                Data[material] = MAX ( Data[material] )
                    && Data[date] <= MAX ( Data[date] )
            ),
            [step one plus minus change index:]
        )
    )

     

    step three group by step two: = 
    VAR _currentgroup = [step two Group of change index:]
    VAR _newtable =
        FILTER (
            ADDCOLUMNS (
                FILTER ( ALL ( Data ), Data[material] = MAX ( Data[material] ) ),
                "@group", [step two Group of change index:],
                "@stock", CALCULATE ( SUM ( Data[stock] ) )
            ),
            [@group] = _currentgroup
        )
    VAR _findminstock =
        MAXX (
            GROUPBY ( _newtable, [@group], "@minstock", MINX ( CURRENTGROUP (), [@stock] ) ),
            [@minstock]
        )
    RETURN
        IF (
            HASONEVALUE ( Data[date] ),
            MAXX ( FILTER ( _newtable, Data[stock] = _findminstock ), Data[date] )
        )